1.【插入排序】
- voidInsertSort(int*p,intsize)
- {
- for(inti=1;i<size;i++)
- {
- intj=i;
- intt=p[i];
- for(;j>0&&p[j-1]>t;j--)
- p[j]=p[j-1];
- p[j]=t;
- }
- }
2.【选择排序】
- voidSelectSort(int*arr,intsize)
- {
- for(inti=0;i<size;i++)
- {
- intpsn=i;
- for(intj=i;j<size;j++)
- {
- if(arr[j]<arr[psn])
- psn=j;
- }
- swap(arr[psn],arr[i]);
- }
- }
3.【冒泡排序】
- voidBubbleSort(int*arr,intsize)
- {
- boolcg=true;
- intn=0;
- do
- {
- cg=false;
- for(inti=0;i<size-1-n;i++)
- {
- if(arr[i]>arr[i+1])
- {
- cg=true;
- swap(arr[i],arr[i+1]);
- }
- }
- n++;
- }while(cg);
- }
4.【快速排序】
- voidQuickSort(int*arr,intsize)
- {
- int*L,*R;
- if(size<=1)return;
- if(2==size)
- {
- if(arr[0]>arr[1])
- swap(arr[0],arr[1]);
- return;
- }
- L=&arr[1];
- R=&arr[size-1];
- while(L<R)
- {
- while(L<R&&*L<*arr)++L;
- while(R>arr&&!(*R<*arr))--R;
- if(L<R)swap(*R,*L);
- }
- swap(*R,*arr);
- QuickSort(arr,R-arr);
- QuickSort(R+1,size-1-(R-arr));
- }
5.【堆排序】
- voidShift(int*heap,intstart,intend)
- {
- inttmp=heap[start];
- intparent=start;
- intchild=parent*2+1;
- while(child<=end)
- {
- if(child<end&&
- heap[child]<heap[child+1])
- child++;
- if(tmp<heap[child])
- {
- heap[parent]=heap[child];
- parent=child;
- child=2*parent+1;
- }elsebreak;
- }
- heap[parent]=tmp;
- }
- voidBuildHeap(int*heap,intsize)
- {
- for(inti=size/2-1;i>=0;i--)
- Shift(heap,i,size-1);
- }
- voidHeapSort(int*heap,intsize)
- {
- BuildHeap(heap,size);
- for(inti=size-1;i>0;i--)
- {
- swap(heap[0],heap[i]);
- Shift(heap,0,i-1);
- }
- }
6.【归并排序】
- voidMerge(int*arr,intfirst,intmid,intlast)
- {
- intsize=last-first+1;
- int*tmp=newint[size];
- intb1=first;
- inte1=mid;
- intb2=mid+1;
- inte2=last;
- intk=0;
- while(b1<=e1&&b2<=e2)
- if(arr[b1]<arr[b2])
- tmp[k++]=arr[b1++];
- elsetmp[k++]=arr[b2++];
- while(b1<=e1)tmp[k++]=arr[b1++];
- while(b2<=e2)tmp[k++]=arr[b2++];
- for(inti=0;i<size;i++)
- arr[first+i]=tmp[i];
- deletetmp;
- }
- voidMergeSort(int*arr,intfirst,intlast)
- {
- if(first<last)
- {
- intmid=(first+last)/2;
- MergeSort(arr,first,mid);
- MergeSort(arr,mid+1,last);
- Merge(arr,first,mid,last);
- }
- }
7.【希尔排序】
- voidShellSort(int*p,intsize)
- {
- intiTemp;
- for(intiStep=size/2;iStep>0;iStep=iStep/2){
- for(inti=iStep;i<size;++i){
- iTemp=p[i];
- intj=i;
- for(;j>=iStep;j-=iStep){
- if(iTemp<p[j-iStep]){
- p[j]=p[j-iStep];
- }else{
- break;
- }
- }
- p[j]=iTemp;
- }
- }
- }
【代码测试】
- #include<iostream>
- #include<string>
- usingnamespacestd;
- typedefvoid(*SortFunc)(int*,int,int*,int*);
- //插入排序
- voidInsertSort(int*p,intsize,int*move_cnt,int*compare_cnt)
- {
- for(inti=1;i<size;i++)
- {
- intj=i;
- intt=p[i];
- ++*move_cnt;
- ++*compare_cnt;
- for(;j>0&&p[j-1]>t;j--){
- ++*compare_cnt;
- p[j]=p[j-1];
- ++*move_cnt;
- }
- p[j]=t;
- ++*move_cnt;
- }
- }
- //希尔排序
- voidShellSort(int*p,intsize,int*move_cnt,int*compare_cnt)
- {
- intiTemp;
- for(intiStep=size/2;iStep>0;iStep=iStep/2)
- {
- for(inti=iStep;i<size;++i)
- {
- iTemp=p[i];
- ++*move_cnt;
- intj=i;
- for(;j>=iStep;j-=iStep)
- {
- ++*compare_cnt;
- if(iTemp<p[j-iStep])
- {
- p[j]=p[j-iStep];
- ++*move_cnt;
- }
- else
- {
- break;
- }
- }
- p[j]=iTemp;
- ++*move_cnt;
- }
- }
- }
- //冒泡排序
- voidBubbleSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
- {
- boolcg=true;
- intn=0;
- do
- {
- cg=false;
- for(inti=0;i<size-1-n;i++)
- {
- ++*compare_cnt;
- if(arr[i]>arr[i+1])
- {
- cg=true;
- swap(arr[i],arr[i+1]);
- *move_cnt+=3;
- }
- }
- n++;
- }while(cg);
- }
- //快速排序
- voidQuickSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
- {
- int*L,*R;
- if(size<=1)return;
- if(2==size)
- {
- ++*compare_cnt;
- if(arr[0]>arr[1]){
- swap(arr[0],arr[1]);
- *move_cnt+=3;
- }
- return;
- }
- L=&arr[1];
- R=&arr[size-1];
- while(L<R)
- {
- ++*compare_cnt;
- while(L<R&&*L<*arr){
- ++*compare_cnt;
- ++L;
- }
- ++*compare_cnt;
- while(R>arr&&!(*R<*arr)){
- ++*compare_cnt;
- --R;
- }
- ++*compare_cnt;
- if(L<R){
- swap(*R,*L);
- *move_cnt+=3;
- }
- }
- swap(*R,*arr);
- *move_cnt+=3;
- QuickSort(arr,R-arr,move_cnt,compare_cnt);
- QuickSort(R+1,size-1-(R-arr),move_cnt,compare_cnt);
- }
- //简单选择排序
- voidSelectSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
- {
- for(inti=0;i<size;i++)
- {
- intpsn=i;
- for(intj=i;j<size;j++)
- {
- ++*compare_cnt;
- if(arr[j]<arr[psn]){
- psn=j;
- }
- }
- swap(arr[psn],arr[i]);
- *move_cnt+=3;
- }
- }
- voidShift(int*heap,intstart,intend,int*move_cnt,int*compare_cnt)
- {
- inttmp=heap[start];
- ++*move_cnt;
- intparent=start;
- intchild=parent*2+1;
- while(child<=end)
- {
- ++*compare_cnt;
- if(child<end&&heap[child]<heap[child+1]){
- ++child;
- }
- ++*compare_cnt;
- if(tmp<heap[child])
- {
- heap[parent]=heap[child];
- ++*move_cnt;
- parent=child;
- child=2*parent+1;
- }elsebreak;
- }
- heap[parent]=tmp;
- ++*move_cnt;
- }
- voidBuildHeap(int*heap,intsize,int*move_cnt,int*compare_cnt)
- {
- for(inti=size/2-1;i>=0;i--)
- Shift(heap,i,size-1,move_cnt,compare_cnt);
- }
- voidHeapSort(int*heap,intsize,int*move_cnt,int*compare_cnt)
- {
- BuildHeap(heap,size,move_cnt,compare_cnt);
- for(inti=size-1;i>0;i--)
- {
- swap(heap[0],heap[i]);
- Shift(heap,0,i-1,move_cnt,compare_cnt);
- }
- }
- voidMerge(int*arr,intfirst,intmid,intlast,int*move_cnt,int*compare_cnt)
- {
- intsize=last-first+1;
- int*tmp=newint[size];
- intb1=first;
- inte1=mid;
- intb2=mid+1;
- inte2=last;
- intk=0;
- while(b1<=e1&&b2<=e2){
- ++*compare_cnt;
- if(arr[b1]<arr[b2])
- tmp[k++]=arr[b1++];
- elsetmp[k++]=arr[b2++];
- ++*move_cnt;
- }
- while(b1<=e1){
- tmp[k++]=arr[b1++];
- ++*move_cnt;
- ++*compare_cnt;
- }
- while(b2<=e2){
- tmp[k++]=arr[b2++];
- ++*move_cnt;
- ++*compare_cnt;
- }
- for(inti=0;i<size;i++){
- arr[first+i]=tmp[i];
- ++*move_cnt;
- }
- deletetmp;
- }
- voidMergeSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
- {
- if(size>1)
- {
- intmid=size/2;
- MergeSort(arr,mid,move_cnt,compare_cnt);
- MergeSort(arr+mid,size-mid,move_cnt,compare_cnt);
- Merge(arr,0,mid-1,size-1,move_cnt,compare_cnt);
- }
- }
- //输出数组
- voidShowArray(int*p,intsize)
- {
- for(inti=0;i<size;i++)
- {
- cout<<p[i];
- if(i<size-1)
- {
- cout<<",";
- }
- }
- cout<<endl;
- }
- voidCopyToTmp(int*arr,int*temp,intlength){
- for(inti=0;i<length;++i)
- {
- temp[i]=arr[i];
- }
- }
- intmain()
- {
- intarr[4][20]={{-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100},
- {100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20},
- {99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79},
- {0}};
- conststringarr_name[3]={"顺序的数组","逆序的数组","随机的数组"};
- SortFuncsort_functions[7]={InsertSort,SelectSort,BubbleSort,QuickSort,ShellSort,HeapSort,MergeSort};
- conststringsort_func_name[7]={"插入排序","选择排序","冒泡排序","快速排序","希尔排序","堆排序","归并排序"};
- intcompare_cnt=0,move_cnt=0;
- cout<<"========================================================"<<endl;
- for(inti=0;i<7;++i){
- cout<<"****"<<sort_func_name[i]<<"****"<<"测试结果如下:"<<endl;
- cout<<"========================================================"<<endl;
- for(intj=0;j<3;++j){
- compare_cnt=0;
- move_cnt=0;
- CopyToTmp(arr[j],arr[3],20);
- cout<<endl<<arr_name[j]<<":";
- ShowArray(arr[3],20);
- sort_functions[i](arr[3],20,&move_cnt,&compare_cnt);
- cout<<sort_func_name[i]<<"后:";
- ShowArray(arr[3],20);
- cout<<sort_func_name[i]<<"对"<<arr_name[j]<<"排序,共进行"<<compare_cnt<<"次比较和"<<move_cnt<<"次移动"<<endl;
- }
- cout<<endl<<"========================================================"<<endl;
- }
- system("pause");
- return0;
- }
【测试结果】
- ========================================================
- ****插入排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 插入排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 插入排序对顺序的数组排序,共进行19次比较和38次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 插入排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 插入排序对逆序的数组排序,共进行209次比较和228次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 插入排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 插入排序对随机的数组排序,共进行132次比较和151次移动
- ========================================================
- ****选择排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 选择排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 选择排序对顺序的数组排序,共进行210次比较和60次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 选择排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 选择排序对逆序的数组排序,共进行210次比较和60次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 选择排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 选择排序对随机的数组排序,共进行210次比较和60次移动
- ========================================================
- ****冒泡排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 冒泡排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 冒泡排序对顺序的数组排序,共进行19次比较和0次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 冒泡排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 冒泡排序对逆序的数组排序,共进行190次比较和570次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 冒泡排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 冒泡排序对随机的数组排序,共进行189次比较和339次移动
- ========================================================
- ****快速排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 快速排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 快速排序对顺序的数组排序,共进行244次比较和54次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 快速排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 快速排序对逆序的数组排序,共进行235次比较和57次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 快速排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 快速排序对随机的数组排序,共进行140次比较和54次移动
- ========================================================
- ****希尔排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 希尔排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 希尔排序对顺序的数组排序,共进行62次比较和124次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 希尔排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 希尔排序对逆序的数组排序,共进行80次比较和160次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 希尔排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 希尔排序对随机的数组排序,共进行88次比较和163次移动
- ========================================================
- ****堆排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 堆排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 堆排序对顺序的数组排序,共进行126次比较和119次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 堆排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 堆排序对逆序的数组排序,共进行108次比较和101次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 堆排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 堆排序对随机的数组排序,共进行118次比较和108次移动
- ========================================================
- ****归并排序****测试结果如下:
- ========================================================
- 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 归并排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 归并排序对顺序的数组排序,共进行88次比较和176次移动
- 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
- 归并排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 归并排序对逆序的数组排序,共进行88次比较和176次移动
- 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
- 归并排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
- 归并排序对随机的数组排序,共进行88次比较和176次移动
原创文章,转载请注明: 转载自IIcyZhao’s Road
本文链接地址:http://blog.youkuaiyun.com/iicy266/article/details/11905851