代码改变世界

排序常用的算法

2013-08-19 09:18  北漂男孩  阅读(216)  评论(0编辑  收藏  举报

排序常用的算法有:插入算法(直接插入算法、折半插入算法、希尔算法)、选择算法(简单选择算法、堆排序算法)、快速算法(冒泡排序、快速排序算法)

以下程序给出了各种算法的实现,其接口为void sort(int *array,int len),每个文件实现一个算法, 最后和main.c文件编译实现。

1、直接插入算法:

//direct_insert_sort.c

 

  1. void sort(int *array,int len)  
  2. {  
  3.     int tmp,i,j;  
  4.     for(i=1;i<len;i++)  
  5.     {  
  6.         if(array[i]<array[i-1])  
  7.         {  
  8.             tmp = array[i];  
  9.             for(j=i-1;j>=0;j--)  
  10.             {  
  11.                 if(array[j]>tmp)  
  12.                     array[j+1]=array[j];  
  13.                 else  
  14.                     break;  
  15.             }  
  16.             array[j+1]=tmp;  
  17.         }  
  18.     }      
  19. }  



 

 

2、折半插入排序

//binary_insert_sort.c

 

  1. void sort(int *array,int len)  
  2. {  
  3.     int low,m,high;  
  4.     int i,tmp,j;  
  5.     for(i=1;i<len;i++)  
  6.     {  
  7.         if(array[i]<array[i-1])  
  8.         {  
  9.             tmp = array[i];  
  10.             low = 0;  
  11.             high = i-1;  
  12.             while(low <= high)  
  13.             {  
  14.                 m=(low+high)/2;  
  15.                 if(array[m]>=tmp)  
  16.                     high = m-1;  
  17.                 else  
  18.                     low = m +1;  
  19.             }  
  20.             for(j=i-1;j>=low;j--)  
  21.             {  
  22.                 array[j+1]=array[j];                      
  23.             }  
  24.             array[low]=tmp;  
  25.         }  
  26.     }  
  27. }  



 

3、希尔排序

//shell_sort.c

 

  1. void sort(int *array,int len)  
  2. {  
  3.     int tmp,i,j,gap;  
  4.     gap = len ;  
  5.     do  
  6.     {  
  7.         gap = gap / 3 + 1;  
  8.         for(i=0+gap;i<len;i++)  
  9.         {  
  10.             if(array[i]<array[i-gap])  
  11.             {  
  12.                 tmp = array[i];  
  13.                 for(j=i-gap;j>=0;j=j-gap)  
  14.                     if(array[j]>tmp)  
  15.                         array[j+gap]=array[j];  
  16.                     else  
  17.                         break;  
  18.                 array[j+gap]=tmp;  
  19.             }  
  20.         }  
  21.       
  22.     }while(gap > 1);  
  23. }  



 

 

4、简单选择排序

//simple_select_sort

 

  1. void sort(int *array,int len)  
  2. {  
  3.     int i,j,min,tmp;  
  4.     for(i=0;i<len-1;i++)  
  5.     {  
  6.         min= i;  
  7.         for(j=i+1;j<len;j++)  
  8.             if(array[j]<array[min])  
  9.                 min=j;  
  10.         if(i != min)  
  11.         {  
  12.             tmp=array[i];  
  13.             array[i]=array[min];  
  14.             array[min]=tmp;  
  15.         }  
  16.     }  
  17. }  



 

5、堆排序

//heap_sort.c

 

  1. static void heapAdjust(int * array,int start,int end);  
  2. void sort(int *array,int len)  
  3. {  
  4.     int i,j;  
  5.     for(i=len/2;i>=0;i--)  
  6.         heapAdjust(array,i,len-1);  
  7.     for(i=len-1;i>0;i--)  
  8.     {  
  9.         int tmp=array[i];  
  10.         array[i]=array[0];  
  11.         array[0]=tmp;  
  12.           
  13.         heapAdjust(array,0,i-1);  
  14.     }  
  15. }  
  16. static void heapAdjust(int * array,int start,int end)  
  17. {  
  18.     int i;  
  19.     int tmp = array[start];  
  20.     for(i=2*start+1;i<=end;i=2*i+1)  
  21.     {  
  22.         if(array[i]<array[i+1]&& i<end)  
  23.             i++;  
  24.         if(tmp > array[i])  
  25.             break;  
  26.         array[start]=array[i];  
  27.         start = i;  
  28.     }  
  29.     array[start]=tmp;  
  30. }  



 

6、冒泡排序

//bubble_sort.c

 

  1. void sort(int * array,int len)  
  2. {  
  3.     int i,j,tmp;  
  4.     for(i=1;i<len;i++)  
  5.     {  
  6.         for(j=0;j<len-1;j++)  
  7.             if(array[j]>array[j+1])  
  8.             {  
  9.                 tmp = array[j+1];  
  10.                 array[j+1]=array[j];  
  11.                 array[j]=tmp;  
  12.             }  
  13.     }      
  14. }  



 

7、快速排序

//quick_sort.c

 

  1. static int partition(int *array,int low,int high);  
  2. static void quickSort(int *array,int start,int end);  
  3. void sort(int *array,int len)  
  4. {  
  5.     quickSort(array,0,len-1);  
  6. }  
  7. static void quickSort(int *array,int start,int end)  
  8. {  
  9.     if(start < end)  
  10.     {  
  11.         int pivotloc = partition(array,start,end);  
  12.         quickSort(array,start,pivotloc-1);  
  13.         quickSort(array,pivotloc+1,end);  
  14.     }  
  15. }  
  16. static int partition(int *array,int low,int high)  
  17. {  
  18.     int i,j,tmp;  
  19.     tmp = array[low];  
  20.     while(low < high)  
  21.     {  
  22.         while(low<high && array[high]>tmp)  
  23.             high--;  
  24.         array[low]=array[high];  
  25.         while(low<high && array[low]<tmp)  
  26.             low++;  
  27.         array[high]=array[low];  
  28.     }  
  29.     array[low]=tmp;  
  30.     return low;  
  31. }      




 

 

8、主函数main.c上面的文件分别单独和main.c文件一起编译即可生成目标文件

 

  1. #include<stdio.h>  
  2. extern void sort(int *array,int len);  
  3. void print(const int *array,int len)  
  4. {  
  5.     int i;  
  6.     for(i=0;i<5;i++)  
  7.         printf("%d    ",array[i]);  
  8.     putchar('\n');  
  9. }  
  10. int main()  
  11. {  
  12.     int i;  
  13.     printf("please input 5 integer numbers\n");  
  14.     int array[5];  
  15.     fflush(stdin);  
  16.     for(i=0;i<5;i++)  
  17.         scanf("%d",array+i);  
  18.     printf("\nold order is:\n");  
  19.     print(array,5);  
  20.     sort(array,5);  
  21.     printf("\n new order is:\n");  
  22.     print(array,5);  
  23. }