标签:style blog http io ar os sp for on
#include <iostream> using namespace std; void shellSort(int a[], int n) { int i, j, gap; for(gap = n/2; gap > 0; gap /= 2)//间隔,逐次递减一半 { for(i = 0; i < gap; i++)//从49到76,逐步递增,也就是分组数。每次循环对一组数完成排序 { for(j = i + gap; j < n; j += gap)//对这个一组内的,间隔为gap,完成插入排序 { if(a[j] < a[j - gap]) { int temp = a[j]; int k = j - gap; while(k >= 0 && a[k] > temp)//插入排序 { a[k + gap] = a[k]; k -= gap; } a[k + gap] = temp; } } } } } int main() { int a[10] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 04}; shellSort(a, 10); for(int i =0; i < 10; i++) { cout<<a[i]<< " "; } cout<<endl; }
标签:style blog http io ar os sp for on
原文地址:http://blog.csdn.net/yapian8/article/details/41620967