# elva

## shellsort之二

http://blog.sina.com.cn/s/blog_61e439e50100mfe8.html

13 14 94 33 82

25 59 94 65 23

45 27 73 25 39

10

10 14 73 25 23

13 27 94 33 39

25 59 94 65 82

45

10 14 73

25 23 13

27 94 33

39 25 59

94 65 82

45

10 14 13

25 23 33

27 25 59

39 65 73

45 94 82

94

#include <stdio.h>

void output_array(int data[], int n)

{

int i;

for(i = 0; i < n; i++)

printf("%d ", data[i]);

printf("\n");

}

void swap(int *a, int *b)

{

int x;

x = *a;

*a = *b;

*b = x;

}

void insertion_sort(int data[], int n, int increment)

{

int i, j;

for(i = increment; i < n; i += increment)

for(j = i; j >= increment && data[j] > data[j - increment]; j -= increment)

swap(&data[j], &data[j - increment]);

}

void shellsort(int data[], int n)

{

int i, j;

for(i = n / 2; i > 2; i /= 2)

for(j = 0; j < i; j++)

insertion_sort(data + j, n - j, i);

insertion_sort(data, n, 1);

}

int main()

{

int data[] = {5, 3, 1, 665, 77, 66, 44, 11, 10, 9, 8, 6};

output_array(data, 12);

shellsort(data, 12);

output_array(data, 12);

return 0;

}

posted on 2010-11-01 18:08 叶子 阅读(329) 评论(0)  编辑 收藏 引用 所属分类: 数据结构