常用排序算法(希尔排序)

先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量dt=1(dt<dt-l<…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
更多信息请参考:http://baike.baidu.com/view/178698.htm

C语言代码:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <memory.h>
 4 #include <time.h>
 5 
 6 void init(int *array, int count)
 7 {
 8     int n = 0;
 9     srand((unsigned int)time(NULL));
10 
11     for (n=0; n<count; n++)
12     {
13         array[n] = rand()%100 + 1;
14     }
15 }
16 
17 void output(int *array, int count)
18 {
19     int n = 0;
20     for (n=0; n<count; n++)
21     {
22         printf("%5d", array[n]);
23     }
24 
25     printf("\n");
26 }
27 
28 void shellsort(int *array,int count)
29 {
30     int i = 0;
31     int j = 0;
32     int t = 0;
33     int k = count/2;
34     while(k>0)
35     {
36         for(i = k;i< count;i++)
37         {
38             t = array[i];
39             j = i;
40             while(j >= k && t < array[j - k])
41             {
42                 array[j] = array[j-k];
43                 j = j-k;
44             }
45 
46             array[j] = t;
47         }
48 
49         k /= 2;
50     }
51 }
52 
53 int main()
54 {
55     const int count = 10;
56     int array[count];
57 
58     memset(array, 0, sizeof(int)*count);
59 
60     init(array, count);
61     
62     printf("data before sort: ");
63     output(array, count);
64 
65     shellsort(array, count);
66 
67     printf("data after sort:  ");
68     output(array, count);
69 
70     return 0;
71 }


排序结果如下:
data before sort:    32   20   84   34   88   30   16   26    4   23
data after sort:      4   16   20   23   26   30   32   34   84   88


Java代码:

 1 import java.util.Random;
 2 
 3 /**
 4 * 先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组。
 5 * 所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序;
 6 * 然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量dt=1(dt<dt-l<…<d2<d1),
 7 * 即所有记录放在同一组中进行直接插入排序为止。
 8 */
 9 
10 public class Sort 
11 {
12     /*
13      * 输出数组中的数据
14      */
15     public static void OutputArray(int[] array) 
16     {
17         for (int data : array) 
18         {
19             System.out.print(data + "\t");
20         }
21         
22         System.out.println();
23     }
24     
25     /*
26      * 生成需要排序的数组
27      */
28     public static int[] createArray(int count) 
29     {
30         int array[] = new int[count];
31         Random r = new Random();
32         
33         for (int i = 0; i < count; i++) 
34         {
35             array[i] = r.nextInt(100);
36         }
37         
38         System.out.println("");
39         System.out.print("data before sort:\t");
40         
41         OutputArray(array);
42         
43         return array;
44     }
45     
46     public static void ShellSort(int[] array) 
47     {
48         int n = array.length;
49         int k = n/2;
50         while(k>0)
51         {
52             for(int i = k; i< n; i++)
53             {
54                 int t = array[i];
55                 int j = i;
56                 while(j >= k && t < array[j - k])
57                 {
58                     array[j] = array[j-k];
59                     j = j-k;
60                 }
61 
62                 array[j] = t;
63             }
64 
65             k /= 2;
66         }
67     }
68     
69     public static void main(String[] args) 
70     {
71         int[] arr1=createArray(10);
72         
73         System.out.print("data after sort:\t");
74         
75         ShellSort(arr1);
76         OutputArray(arr1);
77     }
78 }


排序结果如下:
data before sort:    74    99    8    36    86    37    59    45    42    90    
data after sort:    8    36    37    42    45    59    74    86    90    99    

posted @ 2012-12-27 21:41  Yuan Ping  阅读(244)  评论(0编辑  收藏  举报