重学数据结构

#冒泡排序

int bubleSort2(int R[],int length)
{
    /*
    来自高端大气上档次的学习. 我要写一百遍.http://blog.csdn.net/morewindows/article/details/6657829
    */
    for(int i=0;i<length;++i)
    {
        for(int j=1;j<length-i;++j)
        {
            if(R[j-1]>R[j])
                swap(R[j-1],R[j]);
        }
    }
}
int bubleSort3(int R[],int length)
{
    /*
    来自高端大气上档次的学习. 我要写一百遍.http://blog.csdn.net/morewindows/article/details/6657829
    */
    bool flags;
    int j=0;
    while(flags)
    {
        flags=false;
        for(int i=1;i<length;++i)
        {
            if(R[i-1]>R[i])
            {
                swap(R[i-1],R[i]);
                flags=true;
            }

        }
        cout<<"case "<<++j<<": ";//这两句是自己加的,想看看排序的过程
        printArray(R,length);//
    }
    return 0;
}
35 86 73 47 89 15 64 87 46 11
case 1: 35 73 47 86 15 64 87 46 11 89
case 2: 35 47 73 15 64 86 46 11 87 89
case 3: 35 47 15 64 73 46 11 86 87 89
case 4: 35 15 47 64 46 11 73 86 87 89
case 5: 15 35 47 46 11 64 73 86 87 89
case 6: 15 35 46 11 47 64 73 86 87 89
case 7: 15 35 11 46 47 64 73 86 87 89
case 8: 15 11 35 46 47 64 73 86 87 89
case 9: 11 15 35 46 47 64 73 86 87 89
case 10: 11 15 35 46 47 64 73 86 87 89
Sort...done..0
11 15 35 46 47 64 73 86 87 89

Process returned 0 (0x0)   execution time : 0.030 s 


代码中可以看到其实即使后面已经有序了, 但是还是要进行比较.所以可以换一种写法.

int bubleSort4(int R[],int length)
{
    bool flags;
    for(int i=0;i<length;++i)//其实这里的循环截止条件可以是length-1, 因为最后只有一个元素的时候已经不再需要比较了
    {                           //这里的循环指的是, 需要排序多少个元素的意思.
        flags=false;
        for(int j=1;j<length-i;++j)//length-i-1是最后一个元素的下标这里循环到length-i-1就截止了
        {
            if(R[j-1]>R[j])
            {
                swap(R[j-1],R[j]);
                flags=true;
            }
        }
        if(!flags) break;//在上面的循环中没有发生交换.
        cout<<"case "<<i<<": ";
        printArray(R,length);
    }
}

35 86 73 47 89 15 64 87 46 11
case 0: 35 73 47 86 15 64 87 46 11 89
case 1: 35 47 73 15 64 86 46 11 87 89
case 2: 35 47 15 64 73 46 11 86 87 89
case 3: 35 15 47 64 46 11 73 86 87 89
case 4: 15 35 47 46 11 64 73 86 87 89
case 5: 15 35 46 11 47 64 73 86 87 89
case 6: 15 35 11 46 47 64 73 86 87 89
case 7: 15 11 35 46 47 64 73 86 87 89
case 8: 11 15 35 46 47 64 73 86 87 89
Sort...done..1
11 15 35 46 47 64 73 86 87 89

Process returned 0 (0x0)   execution time : 0.040 s



#直接插入排序法,

void SortCollection::DirectInsertSortInt(int intArray[],int length)
{
	int temp=0;
	if(length<=1)
	{
		cout<<getSelfName<<"::"<<length<<" = "<<length<<endl;
	}
	for(int i=1;i<length;i++)
	{
		temp=intArray[i];
		int j=i-1;
		while (intArray[j]<temp)
		{
			intArray[j+1]=intArray[j];
		}
	}
}

当我写到这里的时候,发现书本的想法是很好的,在数组的开头设置一个相同的值可以有效简化循环条件.

int bubleSort5(int R[],int length)//增加下标,减少循环次数
{
    int position=0;
    int pass_location=length;
    for(int i=0;i<length;++i)
    {
        position = pass_location;
        for(int j=1;j<position;++j)
        {
            if(R[j-1]>R[j])
            {
                swap(R[j-1],R[j]);
                pass_location=j;
            }
        }
        cout<<"case "<<i<<": ";
        printArray(R,length);
    }
}


#include "SortCollection.h"
#include<stdio.h>
#include<iostream>
using namespace std;
string SortCollection::getSelfName()
{
	return "SortCollectio";
}
void SortCollection::directInsertSortInt(int intArray[],int length)
{
	int temp=0;
	if(length<=1)
	{
		cout<<selfName<<"::"<<length<<" = "<<length<<endl;
	}
	for(int i=0;i<length;i++)
	{
		cout<<intArray[i]<<endl;
	}
	for(int i=1;i<length;i++)
	{
		temp=intArray[i];
		int j=i-1;
		while ((intArray[j]<temp)&&(j>=0))
		{
			intArray[j+1]=intArray[j];
			j--;
		}
		intArray[j+1]=temp;
	}
}
当我写到这里的时候, 又开始蛋疼了.

main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall SortCollection::~SortCollection(void)" (??1SortCollection@@QAE@XZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall SortCollection::directInsertSortInt(int * const,int)" (?directInsertSortInt@SortCollection@@QAEXQAHH@Z),该符号在函数 _main 中被引用
1>f:\visual studio 2012\Projects\AllSort\Debug\AllSort.exe : fatal error LNK1120: 2 个无法解析的外部命令

解决

其实书本(数据结构与算法,电子科技大学出版社, 吴越)写的代码太恶心了, 思维完全不在一个维度上.

我写成这样,

void SortC::directInsertSortInt(int IntArray[],int length)
{
	int temp=0;
	if(length<1)
	{
		cout<<ClassName<<" length = "<<length<<endl;
	}
	for(int i=1;i<length;i++)
	{
		temp=IntArray[i];
		int j=i-1;
		while (IntArray[j]>temp&&j>=0)
		{
			IntArray[j+1]=IntArray[j];
			j--;
		}
		IntArray[j+1]=temp;
	}
}

当然,他写了7行,我写了10多行

#归并排序

void merge(int a[],int start,int middle,int last,int temp[])
{
    int i=start;
    int j=middle+1;
    int k=0;
    while(i<=middle&&j<=last)
    {
        if(a[i]<a[j])
            temp[k++]=a[i++];
        else
            temp[k++]=a[j++];
    }
    while(i<=middle)
         temp[k++]=a[i++];
    while(j<=last)
        temp[k++]=a[j++];

    for(int m=0;m<k;++m)
        a[start+m]=temp[m];
}
void mergeSort(int a[],int start,int last,int temp[])
{
    if(start<last)
    {
         int middle=(start+last)/2;
         mergeSort(a,start,middle,temp);
         mergeSort(a,middle+1,last,temp);
         merge(a,start,middle,last,temp);
         printArray(a,20);
    }

}

void heapAdjust(int *my,int i,int size)
{
    /*http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html*/
    /*堆排序涉及到很多求下标的操作, 数组从1开始是比较好的.*/
    int parent=i;//记住,parent只是一个下标而已
    int lChild=2*i;
    int rChild=2*i+1;
    if(i<=size/2)//假如没有这一句,下面的递归调用就会出现问题
    {
        if(lChild<=size&&my[lChild]>my[parent])//left_child is not a leaf
            parent=lChild;
        if(rChild<=size&&my[rChild]>my[parent])
            parent=rChild;
        if(parent!=i)
        {
            swap(my[i],my[parent]);
            heapAdjust(my,parent,size);//当把左孩子或者右孩子和父亲节点交换之后,要判断现在左孩子或者是右孩子是不是平衡的
        }
    }
}
void heapBuild(int *my,int size)
{
    for(int i=size/2; i>=1; --i) //这里用到了一个知识,就是在二叉树中非叶子节点的最大学号是size/2,向下取整,并且注意到根节点是从1开始编号的
    {
        heapAdjust(my,i,size);
    }
}
void heapSort(int *my,int size)//size的含义是,数组的最大下标
{
    heapBuild(my,size);
    for(int i=size; i>1; --i)
    {
        swap(my[1],my[i]);
        heapAdjust(my,1,i-1);
    }
}



#堆排序,讲得很好,一看就明白

http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值