首页 > 代码库 > Sort 整理

Sort 整理

 

留坑,未完。。。

 1 #include <bits/stdc++.h> 2 using namespace std; 3 void InsertSort(int na[],int n) 4 { 5     int tem,i,j; 6     for(i=1; i<n; i++) 7     { 8         tem=na[i]; 9         j=i-1;10         while(j>=0&&tem<na[j])11         {12             na[j+1]=na[j];13             j--;14         }15         na[j+1]=tem;16     }17 }18 void ShellSort(int na[],int n)19 {20     int i,j,d,tem;21     d=n/2;22     while(d>0)23     {24         for(i=d; i<n; i++)25         {26             j=i-d;27             while(j>=0&&na[j]>na[j+d])28             {29                 tem=na[j];30                 na[j]=na[j+d];31                 na[j+d]=tem;32                 j=j-d;33             }34         }35         d=d/2;36     }37 }38 void BubbleSort(int na[],int n)39 {40     int i,j;41     for(i=0; i<n-1; i++)42     {43         for(j=n-1; j>i; j--)44         {45             if(na[j]<na[j-1])46             {47                 na[j] ^= na[j-1];48                 na[j-1] ^= na[j];49                 na[j] ^= na[j-1];50             }51         }52     }53 }54 void Display(int na[],int n,string s)55 {56     cout<<s<<": ";57     for(int i=0; i<n; i++)58         cout<<na[i]<<" ";59     cout<<endl;60 }61 #define n 1062 int main()63 {64     int na[n]= {9,8,7,6,5,4,3,2,1,0};65     InsertSort(na,n);66     Display(na,n,"InsertSort");67 68     int na1[n]={11,22,0,33,44,55,66,77,88,99};69     ShellSort(na1,n);70     Display(na1,n,"ShellSort");71 72     int na2[n]={11,12,10,13,14,15,16,17,18,19};73     BubbleSort(na2,n);74     Display(na2,n,"BubbleSort");75     return 0;76 }

 

Sort 整理