标签:return blog printf length lin amp clu class bsp
希尔排序也是插入排序的一个方法。希尔排序先将待排序序列分割成若干个子序列,分别进行插入排序。
1 #include <stdio.h> 2 3 void ShellInsert(int r[], int length, int delta) 4 { 5 int i, j, temp; 6 for(i = delta; i < length; i++) 7 { 8 if(r[i] < r[i-delta]) 9 { 10 temp = r[i]; 11 for(j = i - delta; j > 0 && temp < r[j]; j -= delta) //直接插入排序 12 r[j+delta] = r[j]; 13 r[j+delta] = temp; 14 } 15 } 16 } 17 18 void ShellSort(int r[], int length, int delta[], int n) 19 { 20 int i; 21 for(i = 0; i < n; i++) 22 { 23 ShellInsert(r, length, delta[i]); 24 } 25 } 26 27 void print(int r[], int n) 28 { 29 int i; 30 for(i = 0; i < n; i++) 31 printf("%d ", r[i]); 32 puts("\n"); 33 } 34 35 int main() 36 { 37 int a[8] = {5, 2, 7, 8, 9, 4, 6, 0}; 38 int b[3] = {4, 2, 1}; 39 printf("before sort:\n"); 40 print(a, 8); 41 ShellSort(a, 8, b, 3); 42 printf("sort after:\n"); 43 print(a, 8); 44 return 0; 45 }
标签:return blog printf length lin amp clu class bsp
原文地址:http://www.cnblogs.com/hello-lijj/p/7246405.html