publicstaticvoidshellSort(int[]a,int[]d,int numOfD){int i, j, k, m, span;int temp;int n = a.length;for(m =0; m < numOfD; m++){
span = d[m];for(k =0; k < span; k++){for(i = k; i < n - span; i = i + span){
temp = a[i + span];
j = i;while(j >-1&& temp <= a[j]){
a[j + span]= a[j];
j = j - span;}
a[j + span]= temp;}}}}
voidshellsort(int*k,int n){int i, j, temp, gap = n;while(gap >1){
gap = gap /2;/*增量缩小,每次减半*/for(i =0; i < n - gap; i++)// n-gap 是控制上限不让越界{
j = i + gap;//相邻间隔的前后值进行比较if(k[i]> k[j]){
temp = k[i];
k[i]= k[j];
k[j]= temp;}}}}
折半插入排序算法
//折半插入排序typedefint ElemType;
ElemType arr[]={0,9,8,7,6,5,4,3,2,1};
ElemType n =10, i, j;
ElemType low, high, mid;voidBinSort(ElemType r[], ElemType n){for(i =2; i <= n; i++){
r[0]= r[i];
low =1;
high = i -1;while(low <= high){
mid =(low + high)/2;if(r[0]< r[mid])
high = mid -1;else
low = mid +1;}for(j = i -1; j >= low; j--){
r[i]= r[j];
i--;}
r[low]= r[0];}}voidput(ElemType r[], ElemType n){for(j =1; j < n; j++)printf("%d\t", r[j]);printf("\n");}
选择排序
直接选择排序
publicstaticvoidselectSort(int[] a){int i ,j ,small;int temp;int n =a.length;for(i=0;i<n-1;i++){
small=i;for(j=i+1;j<n;j++)if(a[j]<a[small]){small =j;}if(small!=i){
temp=a[i];
a[i]=a[small];
a[small]=temp;}}}
稳定的直接选择
publicstaticvoidselectSort2(int[] a){int i ,j ,small;int temp;int n =a.length;for(i=0;i<n-1;i++){
small=i;for(j=i+1;j<n;j++){if(a[j]<a[small])
small =j;}if(small!=i){
temp=a[small];for(j=small;j>i;j--)
a[j]=a[j-1];
a[i]=temp;}}}
交换排序
冒泡排序
publicstaticvoiddoubleSort(int[]a){int i ,j ,flag=1;int temp;int n =a.length;for(i=1;i<n&&flag==1;i++){
flag=0;for(j=0;j<n-i;j++){
flag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;}}}
///冒泡排序voidbubbleSort(elemType arr[],int len){
elemType temp;int i, j;for(i =0; i < len -1; i++)for(j =0; j < len - i; j++){if(arr[j]> arr[j +1])
temp = arr[j];
arr[j]= arr[j +1];
arr[j +1]= temp;}}