几种常用排序算法的C语言实现

重温数据结构,顺手写下几种常用的排序算法,以备查询。

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <assert.h> 
  4. #include <malloc.h> 
  5. #include <memory.h> 
  6. #include <time.h> 
  7.  
  8. void print(int a[], int n) 
  9.     int i = 1; 
  10.     while(i<=n) 
  11.     { 
  12.         printf("%d,", a[i]); 
  13.         i++; 
  14.     } 
  15.     printf("/n"); 
  16.  
  17.     return
  18.  
  19. //直接插入排序,时间复杂度O(n^2),空间复杂度O(1),稳定排序 
  20. //a[0]不参与排序,实际排序空间为a[1]~a[n] 
  21. void InsertSort(int a[], int n) 
  22.     for(int i=2; i<=n; i++) 
  23.     { 
  24.         if(a[i]<a[i-1]) 
  25.         { 
  26.             a[0] = a[i]; 
  27.             int j = i - 1; 
  28.             //扫描有序空间, 
  29.             while(a[0]<a[j]) 
  30.             { 
  31.                 a[j+1] = a[j]; 
  32.                 j--; 
  33.             } 
  34.             a[j+1] = a[0]; 
  35.         } 
  36.     } 
  37.     printf("InsertSort : "); 
  38.     print(a, n); 
  39.  
  40.     return
  41.  
  42. //一次Shell排序过程 
  43. void ShellPass(int a[], int n, int increment) 
  44.     for(int i=increment+1; i<=n; i++) 
  45.     { 
  46.         if(a[i]<a[i-increment]) 
  47.         { 
  48.             a[0] = a[i]; 
  49.             int j=i-increment; 
  50.             while(j>0 && a[0]<a[j]) 
  51.             { 
  52.                 a[j+increment] = a[j]; 
  53.                 j -= increment; 
  54.             } 
  55.             a[j+increment] = a[0]; 
  56.         } 
  57.     } 
  58.  
  59.     return
  60.  
  61. //Shell排序,时间复杂度依赖于增量序列,应避免互为倍数的增量序列, 
  62. //目前较好时间复杂度为n^1.25~1.6n^1.25,空间复杂度为O(1),不稳定排序 
  63. void ShellSort(int a[], int n) 
  64.     int increment = 20; 
  65.     do 
  66.     { 
  67.         increment = increment/3 + 1; 
  68.         ShellPass(a, n, increment); 
  69.     }while(increment>1); 
  70.  
  71.     printf("ShellSort : "); 
  72.     print(a, n); 
  73.  
  74.     return
  75.  
  76. //冒泡排序,时间复杂度O(n^2),空间复杂度O(1),稳定排序 
  77. void BubbleSort(int a[], int n) 
  78.     for(int i=1; i<n; ) 
  79.     { 
  80.         int lastExchangePos = n;//记录一次排序中最后一次交换的位置,此位置之前的所有数据都已有序 
  81.         for(int j=n-1; j>=i; j--) 
  82.         { 
  83.             if(a[j+1]<a[j]) 
  84.             { 
  85.                 a[0] = a[j+1]; 
  86.                 a[j+1] = a[j]; 
  87.                 a[j] = a[0]; 
  88.                 lastExchangePos = j+1; 
  89.             } 
  90.         } 
  91.         i = lastExchangePos; 
  92.     } 
  93.  
  94.     printf("BubbleSort : "); 
  95.     print(a, n); 
  96.  
  97.     return
  98.  
  99. 快速排序,平均时间复杂度为O(nlgn),空间复杂度为O(lgn)~O(n),快速排序不稳定。 
  100. //C++中快速排序的实现, 
  101. void QuickSort(int a[], int low, int high) 
  102.     srand(time(0)); 
  103.     a[0] = a[rand()%(high-low+1)+low];//随机选择基准,改善快排的性能 
  104.  
  105.     int tmp = 0; 
  106.     int i = low, j = high; 
  107.     while(i<=j) 
  108.     { 
  109.         while(i<high && a[i]<a[0]) 
  110.             i++; 
  111.  
  112.         while(j>low && a[j]>a[0]) 
  113.             j--; 
  114.  
  115.         if(i<=j) 
  116.         { 
  117.             tmp = a[i]; 
  118.             a[i] = a[j]; 
  119.             a[j] = tmp; 
  120.             i++; 
  121.             j--; 
  122.         } 
  123.     } 
  124.  
  125.     if(j>low) 
  126.         QuickSort(a, low, j); 
  127.     if(i<high) 
  128.         QuickSort(a, i, high); 
  129.  
  130.     return
  131.  
  132. //快速排序的一次划分 
  133. int Partition(int a[], int low, int high) 
  134.     srand(time(0)); 
  135.     int pos = rand()%(high-low+1)+low; 
  136.     a[0] = a[pos]; 
  137.  
  138.     a[pos] = a[low]; 
  139.  
  140.     while(low<high) 
  141.     { 
  142.         while(low<high && a[0]<a[high]) 
  143.             high--; 
  144.         if(low<high) 
  145.             a[low++] = a[high]; 
  146.  
  147.         while(low<high && a[0]>a[low]) 
  148.             low++; 
  149.         if(low<high) 
  150.             a[high--] = a[low]; 
  151.     } 
  152.  
  153.     a[low] = a[0]; 
  154.  
  155.     return low; 
  156.  
  157. //数据结构中标准的快速排序算法 
  158. void QSort(int a[], int low, int high) 
  159.     if(low<high) 
  160.     { 
  161.         int p = Partition(a, low, high); 
  162.         QSort(a, low, p-1); 
  163.         QSort(a, p+1, high); 
  164.     } 
  165.  
  166.     return
  167.  
  168. //直接选择排序,时间复杂度为O(n^2),空间复杂度为O(1),不稳定排序 
  169. void SelectSort(int a[], int n) 
  170.     for(int i=1; i<=n; i++) 
  171.     { 
  172.         int min = i; 
  173.         for(int j=i+1; j<=n; j++) 
  174.         { 
  175.             if(a[j]<a[min]) 
  176.                 min = j; 
  177.         } 
  178.         if(min!=i) 
  179.         { 
  180.             a[0] = a[i]; 
  181.             a[i] = a[min]; 
  182.             a[min] = a[0]; 
  183.         } 
  184.     } 
  185.  
  186.     printf("SelectSort : "); 
  187.     print(a, n); 
  188.     return
  189.  
  190. //调整堆 
  191. void Heapify(int a[], int s, int to) 
  192.     /*
  193.     //Heapify方法的递归算法
  194.     a[0] = a[s];
  195.     int tmp = 2*s;
  196.     if(tmp+1<=to && a[tmp]<a[tmp+1])tmp++;
  197.     if(tmp<=to && a[0]<a[tmp])
  198.     {
  199.         a[s] = a[tmp];
  200.         a[tmp] = a[0];
  201.         Heapify(a, tmp, to);
  202.     }*/ 
  203.  
  204.     //Heapify方法的递推算法 
  205.     a[0] = a[s]; 
  206.     for(int j=2*s; j<=to; j*=2) 
  207.     { 
  208.         if(j+1<=to && a[j]<a[j+1])j++; 
  209.  
  210.         if(j<=to && a[0]>=a[j]) 
  211.             break
  212.  
  213.         a[s] = a[j]; 
  214.     //  a[j] = a[0]; 
  215.         s = j; 
  216.     } 
  217.     a[s] = a[0]; 
  218.  
  219.     return
  220.  
  221. //堆排序,最坏时间复杂度nlgn,空间复杂度为O(1),不稳定排序,适合大量数据的排序 
  222. void HeapSort(int a[], int n) 
  223.     for(int j=n/2; j>0; j--) 
  224.         Heapify(a, j, n); 
  225.  
  226.     for(int j=n; j>1; j--) 
  227.     { 
  228.         a[0] = a[j]; 
  229.         a[j] = a[1]; 
  230.         a[1] = a[0]; 
  231.         Heapify(a, 1, j-1); 
  232.     } 
  233.  
  234.     printf("HeapSort : "); 
  235.     print(a, n); 
  236.  
  237.     return
  238.  
  239. //归并排序的一次合并过程 
  240. void MergePass(int a[], int low, int mid, int high) 
  241.     int *p = (int*)malloc(sizeof(int)*(high-low+1)); 
  242.     assert(p!=NULL); 
  243.  
  244.     int *pi = p; 
  245.     int i = low, j = mid+1; 
  246.     while(i<=mid && j<=high) 
  247.         *(pi++) = (a[i]<=a[j])? a[i++] : a[j++]; 
  248.  
  249.     while(i<=mid) 
  250.         *(pi++) = a[i++]; 
  251.     while(j<=high) 
  252.         *(pi++) = a[j++]; 
  253.  
  254.     pi = p; 
  255.     while(low<=high) 
  256.         a[low++] = *(pi++); 
  257.  
  258.     free(p); 
  259.  
  260.     return
  261.  
  262. //归并排序,时间复杂度O(nlgn),空间复杂度O(n),稳定排序 
  263. void MergeSort(int a[], int low, int high) 
  264.     if(low<high) 
  265.     { 
  266.         int mid = (low+high)/2; 
  267.         MergeSort(a, low, mid); 
  268.         MergeSort(a, mid+1, high); 
  269.         MergePass(a, low, mid, high); 
  270.     } 
  271.  
  272.     return
  273. int main() 
  274.     //source[0]不参与排序,做为辅助空间使用 
  275.     int source[] = {0, 10, 8, 30, 55, 6, 0, 99, 87, -5, 32, 66, 54, 33, 21, 32, 96, 121, 70}; 
  276.     int n = sizeof(source)/sizeof(source[0])-1; 
  277.  
  278.     int a[sizeof(source)/sizeof(source[0])]; 
  279.     memcpy(a, source, sizeof(source)); 
  280.     InsertSort(a, n); 
  281.  
  282.     memcpy(a, source, sizeof(source)); 
  283.     ShellSort(a, n); 
  284.  
  285.     memcpy(a, source, sizeof(source)); 
  286.     BubbleSort(a, n); 
  287.  
  288.     memcpy(a, source, sizeof(source)); 
  289.     QuickSort(a, 1, n); 
  290.     printf("QuickSort : "); 
  291.     print(a, n); 
  292.  
  293.     memcpy(a, source, sizeof(source)); 
  294.     QSort(a, 1, n); 
  295.     printf("QSort : "); 
  296.     print(a, n); 
  297.  
  298.     memcpy(a, source, sizeof(source)); 
  299.     SelectSort(a, n); 
  300.  
  301.     memcpy(a, source, sizeof(source)); 
  302.     HeapSort(a, n); 
  303.  
  304.     memcpy(a, source, sizeof(source)); 
  305.     MergeSort(a, 1, n); 
  306.     printf("MergeSort : "); 
  307.     print(a, n); 
  308.  
  309.     system("pause"); 
  310.     return 0; 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值