冒泡排序、快速排序、选择排序、插入排序、shell排序C代码

来源:互联网 发布:php相册管理系统 编辑:程序博客网 时间:2024/06/07 12:39
  1. #include <stdio.h>
  2. void bubbleSort(int *list,int index)
  3. {
  4.     int i,j;
  5.     int temp;
  6.     
  7.     for(j=index;j>0;j--)
  8.     {
  9.        for(i=0;i<j-1;i++)
  10.        {
  11.           if(list[i]>list[i+1])
  12.           {
  13.              temp=list[i+1];//较小值保存在暂存变量里 
  14.              list[i+1] = list[i];// 较大值后移 
  15.              list[i] = temp;//较小值前移             
  16.           }
  17.        }
  18.     }
  19. }
  20. void quicksort(int *list,int left,int right)
  21. {
  22.      int i,j,k;
  23.      int pivot;//分割指针
  24.      int temp;//交换时的暂存变量
  25.      
  26.      i=left;j=right+1;
  27.      pivot=list[left];
  28.      
  29.      if(i<j)
  30.      {
  31.         do
  32.         {
  33.             do
  34.             {
  35.               i++;  
  36.             }while(list[i]<=pivot&&i<=right);//从左往右找大于pivot的值 
  37.             do
  38.             {
  39.               j--;
  40.             }while(list[j]>=pivot&&j>left);//从右往左找小于pivot的值 
  41.             
  42.             if(i<j)
  43.             {
  44.                    temp=list[i];
  45.                    list[i] = list[j];
  46.                    list[j] = temp;                   
  47.             }
  48.         }while(i<j);//1st do
  49.         
  50.         temp = list[left];
  51.         list[left] = list[j];
  52.         list[j] = temp;
  53.         
  54.        quicksort(list,left,j-1);
  55.        quicksort(list,j+1,right); 
  56.      } 
  57. }
  58. void selectionsort(int *list,int index)
  59. {
  60.      int i,j,minat,min;
  61.      for(i=0;i<(index-1);i++)
  62.      {
  63.           minat=i;
  64.           min = list[i];
  65.           for(j=i+1;j<index;j++)//select the min of the rest of array
  66.           {
  67.              if(min>list[j])
  68.              {
  69.                minat = j;//position of the min element
  70.                min=list[j];
  71.              }
  72.           }
  73.           int temp=list[i];
  74.           list[i]=list[minat];
  75.           list[minat] = temp;
  76.      }
  77. }
  78. void insertionsort(int *list,int index)
  79. {
  80.      int i,j;
  81.      int insertnode;
  82.      
  83.      for(i=1;i<index;i++)
  84.      {
  85.         insertnode=list[i];//设置欲插入的数值
  86.         j=i-1;
  87.         while(j>=0&&insertnode<list[j])
  88.         {
  89.            list[j+1] = list[j];
  90.            j--;
  91.         }
  92.         list[j+1] = insertnode; 
  93.      }     
  94. }
  95. void shellsort(int *list,int index)
  96. {
  97.    int i,j,d;
  98.    int temp;
  99.    d=index/2;//初始集合间隔长 
  100.    
  101.    while(d>0)
  102.    {
  103.              for(i=d;i<index;i++)
  104.              {
  105.                 j=i-d;
  106.                 while(j>=0)
  107.                 {
  108.                   if(list[j]>list[j+d])
  109.                   {
  110.                      temp=list[j];
  111.                      list[j]= list[j+d];
  112.                      list[j+d]= temp;
  113.                      j=j-d;
  114.                   }
  115.                   else
  116.                   j=-1;
  117.                 }   
  118.              }
  119.    d/=2;
  120.    }    
  121. }
  122. int main()
  123. {    
  124.     int bsort[64];
  125.     int i,node,index=0;
  126.     printf("Please input number to sort:/n");
  127.     scanf("%d",&node);
  128.     while(node)
  129.     {
  130.                bsort[index++]=node;
  131.                scanf("%d",&node);
  132.     }
  133.     
  134.     /*-------------Bubble Sort-------------------*/
  135.     //bubbleSort(bsort,index);
  136.     /*------------------------------------------*/
  137.     /*----------quick sort----------------------*/
  138.     //quicksort(bsort,0,index-1);
  139.     /*------------------------------------------*/
  140.     /*----------selection sort------------------*/
  141.     //selectionsort(bsort,index);
  142.     /*------------------------------------------*/
  143.     /*----------Insertion sort------------------*/
  144.     //insertionsort(bsort,index);    
  145.     /*------------------------------------------*/
  146.     /*-----------Shell sort---------------------*/
  147.     shellsort(bsort,index);
  148.     /*------------------------------------------*/
  149.     printf("/nFinal sorting result:/n");
  150.     for(i=0;i<index;i++)
  151.     {
  152.         printf("%d ",bsort[i]);
  153.     }
  154.     
  155.     return 0;
  156. }
原创粉丝点击