各种排序算法

C代码   收藏代码
  1. #include <stdio.h>  
  2. #define N 5  
  3.   
  4. //从小到大  
  5. void bubbleSort(int p[]){  
  6.     int i=0,j=0,min=0;  
  7.     for(i=0;i<N;i++)  
  8.         for(j=N-1;j>i;j--){  
  9.             if(p[j]<p[j-1]){  
  10.                 min=p[j-1];  
  11.                 p[j-1]=p[j];  
  12.                 p[j]=min;  
  13.             }  
  14.         }  
  15. }  
  16. //从大到小  
  17. void bubbleSort2(int p[]){  
  18.     int i=0,j=0,min=0;  
  19.     for(i=0;i<N-1;i++)  
  20.         for(j=i;j<N-1;j++){  
  21.             if(p[j]<p[j+1]){  
  22.                 min=p[j];  
  23.                 p[j]=p[j+1];  
  24.                 p[j+1]=min;  
  25.             }  
  26.         }  
  27. }  
  28. //从小到大  
  29. void selectSort(int *p){  
  30.     int i=0,j=0,temp=0,min=0;  
  31.     for(i=0;i<N-1;i++){  
  32.         min=i;  
  33.         for(j=i+1;j<N;j++){  
  34.             if(p[j]<p[min]){  
  35.                 min=j;  
  36.             }  
  37.         }  
  38.         if(min!=i){  
  39.             temp=p[i];  
  40.             p[i]=p[min];  
  41.             p[min]=temp;  
  42.         }  
  43.     }  
  44. }  
  45.   
  46. //从大到小  
  47. void selectSort2(int *p){  
  48.     int i=0,j=0,temp=0,max=0;  
  49.     for(i=0;i<N-1;i++){  
  50.         max=i;  
  51.         for(j=i+1;j<N;j++){  
  52.             if(p[j]>p[max]){  
  53.                 max=j;  
  54.             }  
  55.         }  
  56.         if(max!=i){  
  57.             temp=p[i];  
  58.             p[i]=p[max];  
  59.             p[max]=temp;  
  60.         }  
  61.     }  
  62. }  
  63.   
  64. void insertSort(int p[]){  
  65.     int i=0,j=0,temp=0;  
  66.     for(i=1;i<N;i++){  
  67.         if(p[i]<p[i-1])  
  68.         {  
  69.             temp=p[i];  
  70.             for(j=i-1;temp<p[j]&&j>=0;j--)  
  71.             {  
  72.                     p[j+1]=p[j];  
  73.             }  
  74.             p[j+1]=temp;  
  75.         }  
  76.     }  
  77. }  
  78.   
  79. void shellSort(int * p){  
  80.     int i=0,j=0,temp=0;  
  81.     int increment=N-1;  
  82.     while(increment>1){  
  83.         increment=increment/3+1;  
  84.         for(i=increment;i<N;i++){  
  85.             if(p[i]<p[i-increment]){  
  86.                 temp=p[i];  
  87.                 for(j=i-increment;j>=0&&temp<p[j];j-=increment)  
  88.                     p[j+increment]=p[j];  
  89.                 p[j+increment]=temp;  
  90.             }  
  91.         }  
  92.     }  
  93. }  
  94.   
  95. void heapAdjust(int *p,int i,int j){  
  96.     int lchild=2*i+1;  
  97.     int rchild=2*i+2;  
  98.     int max=i;  
  99.     int temp;  
  100.     if(i<=(N-1)/2){  
  101.         if(lchild<=N-1&&p[lchild]>max)  
  102.             max=lchild;  
  103.         if(rchild<=N-1&&p[rchild>max])  
  104.             max=rchild;  
  105.         if(max!=i)  
  106.         {  
  107.             temp=p[i];  
  108.             p[i]=p[max];  
  109.             p[max]=temp;  
  110.             heapAdjust(p,max,N-1);  
  111.         }  
  112.     }  
  113. }  
  114.   
  115. void heapSort(int *p){  
  116.     int i=0,temp=0;  
  117.     for(i=(N-1)/2;i>=0;i--){  
  118.         heapAdjust(p,i,N-1);  
  119.     }  
  120.     for(i=N-1;i>=0;i--){  
  121.         temp=p[i];  
  122.         p[i]=p[0];  
  123.         p[0]=temp;  
  124.         heapAdjust(p,0,i-1);  
  125.     }  
  126. }  
  127.   
  128. void merge(int *p,int low,int mid,int high){  
  129.     int i=low,j=mid+1,k=0;  
  130.     int array[10];  
  131.     while(i<=mid&&j<=high){  
  132.         array[k++]=p[i]<=p[j]?p[i++]:p[j++];  
  133.     }  
  134.     while(i<=mid)  
  135.         array[k++]=p[i++];  
  136.     while(j<=high)  
  137.         array[k++]=p[j++];  
  138.     for(i=low,k=0;i<=high;k++,i++)  
  139.         p[i]=array[k];  
  140. }  
  141.   
  142. void mergeSort(int *p,int low,int high){  
  143.     int mid;  
  144.     if(low<high){  
  145.         mid=(low+high)/2;  
  146.         mergeSort(p,low,mid);  
  147.         mergeSort(p,mid+1,high);  
  148.         merge(p,low,mid,high);  
  149.     }  
  150. }  
  151.   
  152. void quickSort(int p[],int left,int right){  
  153.     int i,j,x,temp,k;  
  154.     if(left<right){  
  155.         i=left-1;  
  156.         x=p[right];  
  157.         for(j=left;j<right;j++){  
  158.             if(p[j]<=x){  
  159.                 i++;  
  160.                 temp=p[i];p[i]=p[j];p[j]=temp;  
  161.             }  
  162.         }  
  163.         temp=p[i+1];p[i+1]=x;p[right]=temp;  
  164.         quickSort(p,left,i);  
  165.         quickSort(p,i+2,right);  
  166.     }     
  167. }  
  168. //位图排序,时间复杂度O(n)  
  169. void bitmapSort(){  
  170.     int unsort[]={3,5,4,2,1,7,6,8,10,9};  
  171.     int x=0,i=0;  
  172.     for(i=0;i<10;i++){  
  173.         x=x|(0x01<<(unsort[i]%32));  
  174.     }  
  175.     for(i=0;i<32;i++){  
  176.         if((x&(0x01<<i))==(0x01<<i))  
  177.             printf("%d ",i);  
  178.     }  
  179. }  
  180.   
  181. void main(){  
  182.     int unsort[]={4,3,5,6,1,2},i;  
  183.     quickSort(unsort,0,5);    
  184.     for(i=0;i<6;i++)  
  185.     printf("%d ",unsort[i]);  
  186.     bitmapSort();  
  187. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值