常用排序算法

文章出处: http://blog.csdn.net/weixingstudio/article/details/8947003


1. 直接插入排序

2. 折半插入排序

3. 冒泡排序

4. 简单选择排序

5. 希尔排序

6. 快速排序

7. 堆排序

8. 二路归并排序


  1. // Sort.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "stdio.h"  
  6. #include "iostream"  
  7.   
  8. using namespace std;  
  9. // Straight insert sort  
  10. void InsertSort(int *arr,int length);  
  11. void InsertSort2(int arr[], int length);  
  12.   
  13. // Binary Insert Sort  
  14. void BinInsertSort(int arr[], int length);  
  15.   
  16. // Shell Sort  
  17. void ShellSort(int *arr,int length);  
  18. // Shell Sort 2  
  19. void ShellSort2(int *arr,int length);  
  20.   
  21. // Bubble Sort  
  22. void BubbleSort(int *arr,int length);  
  23.   
  24. // Quick Sort  
  25. void QuickSort(int *arr, int low, int high);  
  26. // Partition  
  27. int Partition(int *arr, int low, int high);  
  28.   
  29. // Simple Selection Sort  
  30. void SimpleSelectionSort(int *arr, int length);  
  31.   
  32. // HeapAdjust  
  33. void HeapAdjust(int *arr, int s,int end);  
  34.   
  35. // Heap Sort,小根堆  
  36. void HeapSort(int *arr,int length);  
  37. // Merge Array  
  38. void Merge(int *arr, int *arr2, int left, int mid, int right);  
  39. // Merge Sort  
  40. void MergeSort(int *arr, int *arr2, int left, int right);  
  41.   
  42. int _tmain(int argc, _TCHAR* argv[])  
  43. {  
  44.     int arr[]={29,5,44,28,36,42,7,5,88,9,10};  
  45.     int length=sizeof(arr)/sizeof(*arr);  
  46.     InsertSort(arr,length);  
  47.     printf("Straight Insert Sort:\n");  
  48.     printf("total number:%4d\n",length);  
  49.     printf("sorted result:\n");  
  50.     for (int i=0;i<length;i++)  
  51.     {  
  52.         printf("%5d",arr[i]);  
  53.     }  
  54.     cout<<endl;  
  55.   
  56.     // test2  
  57.     int arr2[]={29,5,44,28,36,42,7,5,88,9,10};  
  58.     length=sizeof(arr2)/sizeof(*arr2);  
  59.     InsertSort2(arr2,length);  
  60.     printf("Straight Insert Sort:\n");  
  61.     printf("total number:%4d\n",length);  
  62.     printf("sorted result:\n");  
  63.     for (int i=0;i<length;i++)  
  64.     {  
  65.         printf("%5d",arr2[i]);  
  66.     }  
  67.     cout<<endl;  
  68.   
  69.     // test3  
  70.     int arr3[]={29,5,44,28,36,42,7,5,88,9,10};  
  71.     length=sizeof(arr3)/sizeof(*arr3);  
  72.     BinInsertSort(arr3,length);  
  73.     printf("Binary Insert Sort:\n");  
  74.     printf("total number:%4d\n",length);  
  75.     printf("sorted result:\n");  
  76.     for (int i=0;i<length;i++)  
  77.     {  
  78.         printf("%5d",arr3[i]);  
  79.     }  
  80.     cout<<endl;  
  81.   
  82.     // test4  
  83.     int arr4[]={29,5,44,28,36,42,7,5,88,9,10};  
  84.     length=sizeof(arr4)/sizeof(*arr4);  
  85.     ShellSort(arr4,length);  
  86.     printf("Shell Sort:\n");  
  87.     printf("total number:%4d\n",length);  
  88.     printf("sorted result:\n");  
  89.     for (int i=0;i<length;i++)  
  90.     {  
  91.         printf("%5d",arr4[i]);  
  92.     }  
  93.     cout<<endl;  
  94.   
  95.     // test5  
  96.     int arr5[]={29,5,44,28,36,42,7,5,88,9,10};  
  97.     length=sizeof(arr5)/sizeof(*arr5);  
  98.     ShellSort2(arr5,length);  
  99.     printf("Shell Sort 2:\n");  
  100.     printf("total number:%4d\n",length);  
  101.     printf("sorted result:\n");  
  102.     for (int i=0;i<length;i++)  
  103.     {  
  104.         printf("%5d",arr5[i]);  
  105.     }  
  106.     cout<<endl;  
  107.   
  108.     // test6  
  109.     int arr6[]={29,5,44,28,36,42,7,5,88,9,10};  
  110.     length=sizeof(arr6)/sizeof(*arr6);  
  111.     BubbleSort(arr6,length);  
  112.     printf("Bubble Sort:\n");  
  113.     printf("total number:%4d\n",length);  
  114.     printf("sorted result:\n");  
  115.     for (int i=0;i<length;i++)  
  116.     {  
  117.         printf("%5d",arr6[i]);  
  118.     }  
  119.     cout<<endl;  
  120.   
  121.       
  122.   
  123.     // test7  
  124.     int arr7[]={29,5,44,28,36,42,7,5,88,9,10};  
  125.     length=sizeof(arr7)/sizeof(*arr7);  
  126.     QuickSort(arr7,0,length-1);  
  127.     printf("Quick Sort:\n");  
  128.     printf("total number:%4d\n",length);  
  129.     printf("sorted result:\n");  
  130.     for (int i=0;i<length;i++)  
  131.     {  
  132.         printf("%5d",arr7[i]);  
  133.     }  
  134.     cout<<endl;  
  135.   
  136.     // test8  
  137.     int arr8[]={29,5,44,28,36,42,7,5,88,9,10};  
  138.     length=sizeof(arr8)/sizeof(*arr8);  
  139.     SimpleSelectionSort(arr8,length);  
  140.       
  141.     printf("Simple Selection Sort:\n");  
  142.     printf("total number:%4d\n",length);  
  143.     printf("sorted result:\n");  
  144.     for (int i=0;i<length;i++)  
  145.     {  
  146.         printf("%5d",arr8[i]);  
  147.     }  
  148.     cout<<endl;  
  149.   
  150.     // test9, 对于堆的应用,数组arr[0]暂时不用,降低处理复杂度  
  151.     int arr9[]={0,29,5,44,28,36,42,7,5,88,9,10};  
  152.     length=sizeof(arr9)/sizeof(*arr9)-1;  
  153.     HeapSort(arr9,length);  
  154.     printf("///\n");  
  155.     printf("Heap Sort:\n");  
  156.     printf("total number:%4d\n",length);  
  157.     printf("sorted result:\n");  
  158.     for (int i=length;i>=1;i--)  
  159.     {  
  160.         printf("%5d",arr9[i]);  
  161.     }  
  162.     cout<<endl;  
  163.   
  164.     // test10  
  165.     int arr10[]={29,5,44,28,36,42,7,5,88,9,10};  
  166.     length=sizeof(arr10)/sizeof(*arr10);  
  167.     int * arr10_2=new int[length];  
  168.     MergeSort(arr10,arr10_2,0,length-1);  
  169.     printf("///\n");  
  170.     printf("Merge Sort:\n");  
  171.     printf("total number:%4d\n",length);  
  172.     printf("sorted result:\n");  
  173.     for (int i=0;i<length;i++)  
  174.     {  
  175.         printf("%5d",arr10[i]);  
  176.     }  
  177.     cout<<endl;  
  178.   
  179.     system("pause");  
  180.     return 0;  
  181. }  
  182.   
  183. // 由小到大排序  
  184. // Straight Insert Sort  
  185. void InsertSort(int *arr,int length)  
  186. {  
  187.     for (int i=1;i<length;i++)  
  188.     {  
  189.         if (arr[i]<arr[i-1]) // 需要将arr[i]插入到已排序的列表中   
  190.         {  
  191.             int temp=arr[i];  
  192.             int j=0;  
  193.             for (j=i-1;j>=0;j--)  
  194.             {  
  195.                 if (temp<arr[j])  
  196.                 {  
  197.                     arr[j+1]=arr[j];  
  198.                 }  
  199.                 else  
  200.                 {  
  201.                     break;  
  202.                 }  
  203.             }  
  204.             arr[j+1]=temp;  
  205.         }  
  206.     }  
  207. }  
  208.   
  209. // Straight Insert Sort 2  
  210. void InsertSort2(int arr[], int length)  
  211. {  
  212.     for (int i=1;i<length;i++)  
  213.     {  
  214.         if (arr[i]<arr[i-1]) // 需要进行插入  
  215.         {  
  216.             int temp=arr[i];  
  217.             int j=i-1;  
  218.             do   
  219.             {  
  220.                 arr[j+1]=arr[j];  
  221.                 j--;  
  222.             } while (j>=0&&temp<arr[j]);  
  223.             arr[j+1]=temp;  
  224.         }  
  225.     }  
  226. }  
  227.   
  228. // Binary Insert Sort  
  229. void BinInsertSort(int arr[], int length)  
  230. {  
  231.     for (int i=1;i<length;i++)  
  232.     {  
  233.         if (arr[i]<arr[i-1]) // 需要插入到有序序列中  
  234.         {  
  235.             int temp=arr[i];  
  236.             int low=0;  
  237.             int high=i-1;  
  238.             while(low<=high)  
  239.             {  
  240.                 int mid=(low+high)/2;  
  241.                 if (temp<mid)  
  242.                 {  
  243.                     high=mid-1;  
  244.                 }  
  245.                 else if (temp>mid)  
  246.                 {  
  247.                     low=mid+1;  
  248.                 }  
  249.                 else  
  250.                 {  
  251.                     low=mid+1;  
  252.                     break;  
  253.                 }  
  254.             }  
  255.             for(int j=i-1;j>=low;j--)  
  256.             {  
  257.                 arr[j+1]=arr[j];  
  258.             }  
  259.             arr[low]=temp;  
  260.         }  
  261.     }  
  262. }  
  263.   
  264. // Shell Sort  
  265. void ShellSort(int *arr,int length)  
  266. {  
  267.     // Shell Sort  
  268.     int gap=length; // 初始化子序列的间隔  
  269.     do   
  270.     {  
  271.         gap=gap/3+1;  
  272.         for (int i=0+gap;i<length;i++) //判断每个子序列   
  273.         {  
  274.             if (arr[i]<arr[i-gap]) // 需要插入到已排序的子序列  
  275.             {  
  276.                 int temp=arr[i];  
  277.                 int j=0;  
  278.                 for (j=i-gap;j>=0;j=j-gap)  
  279.                 {  
  280.                     if (temp<arr[j])  
  281.                     {  
  282.                         arr[j+gap]=arr[j];  
  283.                     }  
  284.                     else  
  285.                     {  
  286.                         break;  
  287.                     }  
  288.                 }  
  289.                 arr[j+gap]=temp;  
  290.             }  
  291.         }  
  292.     } while (gap>1);  
  293. }  
  294.   
  295. // Shell Sort 2  
  296. void ShellSort2(int *arr,int length)  
  297. {  
  298.     int gap=length;  
  299.     do   
  300.     {  
  301.         gap=gap/3+1;  
  302.         for (int i=0+gap;i<length;i++)  
  303.         {  
  304.             if (arr[i]<arr[i-gap])  
  305.             {  
  306.                 int temp=arr[i];  
  307.                 int j=i-gap;  
  308.                 do   
  309.                 {  
  310.                     arr[j+gap]=arr[j];  
  311.                     j=j-gap;  
  312.                 } while (j>=0&&temp<arr[j]);  
  313.                 arr[j+gap]=temp;  
  314.             }  
  315.         }  
  316.     } while (gap>1);  
  317. }  
  318.   
  319. // Bubble Sort  
  320. void BubbleSort(int *arr,int length)  
  321. {  
  322.     bool exchange=false;  
  323.     for (int i=0;i<length-1;i++)  
  324.     {  
  325.         exchange=false;  
  326.         for (int j=length-1;j>=i+1;j--)  
  327.         {  
  328.             if (arr[j]<arr[j-1]) // 需要交换  
  329.             {  
  330.                 exchange=true;  
  331.                 int temp=arr[j-1];  
  332.                 arr[j-1]=arr[j];  
  333.                 arr[j]=temp;  
  334.             }  
  335.         }  
  336.         if (exchange==false)  
  337.         {  
  338.             return;  
  339.         }  
  340.     }  
  341. }  
  342.   
  343. // Quick Sort  
  344. void QuickSort(int *arr, int low, int high)  
  345. {  
  346.     if (low<high)  
  347.     {  
  348.         int pivocLoc=Partition(arr,low,high);  
  349.         QuickSort(arr,low,pivocLoc-1);  
  350.         QuickSort(arr,pivocLoc+1,high);  
  351.     }  
  352. }  
  353.   
  354. // Partition  
  355. int Partition(int *arr, int low, int high)  
  356. {  
  357.     int pivotKey=arr[low]; // 记录枢轴  
  358.     while(low<high)  
  359.     {  
  360.         while(low<high&&arr[high]>=pivotKey) --high;  
  361.         int temp=arr[high];arr[high]=arr[low];arr[low]=temp;  
  362.         while(low<high&&arr[low]<pivotKey) ++low;  
  363.         temp=arr[high];arr[high]=arr[low];arr[low]=temp;  
  364.     }  
  365.     return low;  
  366. }  
  367.   
  368. // Simple Selection Sort  
  369. void SimpleSelectionSort(int *arr, int length)  
  370. {  
  371.     //  
  372.     for (int i=0;i<length;i++)  
  373.     {  
  374.         int index=i;  
  375.         for(int j=i+1;j<length;j++)  
  376.         {  
  377.             if (arr[j]<arr[index])  
  378.             {  
  379.                 index=j;  
  380.             }  
  381.         }  
  382.         if(index!=i)  
  383.         {  
  384.             // exchange  
  385.             int temp=arr[i];  
  386.             arr[i]=arr[index];  
  387.             arr[index]=temp;  
  388.         }  
  389.     }  
  390. }  
  391.   
  392. // Heap Sort,小根堆  
  393. void HeapSort(int *arr,int length)  
  394. {  
  395.     // 调整数组生成小根堆  
  396.     for (int i=length/2;i>=1;i--)  
  397.     {  
  398.         HeapAdjust(arr,i,length);  
  399.     }  
  400.     for (int i=length;i>1;--i)  
  401.     {  
  402.         // 交换堆头的元素和最后一个  
  403.         int temp=arr[1];  
  404.         arr[1]=arr[i];  
  405.         arr[i]=temp;  
  406.   
  407.         // 重新调整堆为小根堆  
  408.         HeapAdjust(arr,1,i-1);  
  409.     }  
  410. }  
  411.   
  412. // HeapAdjust  
  413. void HeapAdjust(int *arr, int s,int end)  
  414. {  
  415.     int rloc=arr[s];  
  416.     for (int j=2*s;j<=end;j=j*2)  
  417.     {  
  418.         if (j<end&&arr[j]>arr[j+1])  
  419.         {  
  420.             j++; // 找到两个兄弟节点中最小的节点  
  421.         }  
  422.         if (!(arr[j]<rloc))  
  423.         {  
  424.             break;  
  425.         }  
  426.         arr[s]=arr[j];  
  427.         s=j;  
  428.     }  
  429.     arr[s]=rloc;  
  430. }  
  431.   
  432. // Merge Sort  
  433. void MergeSort(int *arr, int *arr2, int left, int right)  
  434. {  
  435.     if (left==right)  
  436.     {  
  437.         arr2[left]=arr[left];  
  438.         return;  
  439.     }  
  440.     if (left<right)  
  441.     {  
  442.         int mid=(left+right)/2;  
  443.         MergeSort(arr,arr2,left,mid);  
  444.         MergeSort(arr,arr2,mid+1,right);  
  445.         Merge(arr,arr2,left,mid,right);  
  446.     }  
  447. }  
  448.   
  449. // Merge Array  
  450. void Merge(int *arr, int *arr2, int left, int mid, int right)  
  451. {  
  452.     // 将数组保存到暂存空间中  
  453.     for (int k=left;k<=right;k++)  
  454.     {  
  455.         arr2[k]=arr[k];  
  456.     }  
  457.     int s1=left;  
  458.     int s2=mid+1;  
  459.     int t=left;  
  460.     while(s1<=mid&&s2<=right)  
  461.     {  
  462.         if (arr2[s1]<=arr2[s2])  
  463.         {  
  464.             arr[t++]=arr2[s1++];  
  465.         }  
  466.         else  
  467.         {  
  468.             arr[t++]=arr2[s2++];  
  469.         }  
  470.     }  
  471.     while(s1<=mid) arr[t++]=arr2[s1++];  
  472.     while(s2<=right) arr[t++]=arr2[s2++];  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值