常见排序算法:shell排序

 shell排序是对插入排序的一个改装,它每次排序把序列的元素按照某个增量分成几个子序列,对这几个子序列进行插入排序,然后不断的缩小增量扩大每个子序列的元素数量,直到增量为一的时候子序列就和原先的待排列序列一样了,此时只需要做少量的比较和移动就可以完成对序列的排序了。

 1 /// <summary>
 2         /// default to sort assending
 3         /// execute the shell sorting;
 4         /// </summary>
 5         /// <param name="array"></param>
 6         public static void SortByAssending(int[] array)
 7         {
 8             // check argument;
 9             
10             int tmp = 0;
11             int j = 0;
12             int i = 0;
13             forint increment = array.Length/2;increment >0; increment/=2)
14             {
15                 for(i = increment ; i< array.Length; i++)
16                 {
17                     tmp = array[i];
18                     for(j = i;j>=increment;j-=increment)
19                     {
20                         if(array[j-increment] > tmp)
21                         {
22                             array[j] = array[j-increment];
23                         }
24                         else
25                         {
26                             break;
27                         }
28                     }
29                     array[j] = tmp;
30                 }
31             }
32         }

 

 

posted @ 2010-03-25 10:53  Freedom  阅读(243)  评论(0编辑  收藏  举报