现在的位置: 首页 > 综合 > 正文

希尔排序算法知识总结

2013年10月18日 ⁄ 综合 ⁄ 共 3695字 ⁄ 字号 评论关闭

希尔排序算法思想:

 

为了易于理解,把要排序的数当成一系列整数,让这些整数组成一个长度为n(n为这些要排序的整数的个数)的数组为例来说明。取一个小于要排序的数字的个数n的整数a,把要排序的数字分成a个小数组(这些小数组中的元素由原数组中所有距离为a和a的倍数的位置处的整数组成),然后对这些小数组进行直接插入排序。完成之后,再取一个整数a1(a1<a),然后重复上面的操作,即进行分组和直接插入排序,直到at=1(at<at-1<···a2<a1)即所有的整数放在同一个数组中进行直接插入排序为止。简而言之,就是一种分组的直接插入排序方法。

 

 

希尔排序的过程如下:

 

比如对下面的整数进行希尔排序为例来说明:

2,4,1,3

这些整数的个数为n=4,用等于2的整数a1(a1=n/2)把这些数分割成两部分,分割的方法就是:将相隔2个数的距离的整数分配到一个数组。因此可以等到两个数组{2,1}

和{4,3},对这两个数组进行直接插入排序后为{1,2}和{3,4},这样它们原数列中的顺序就变成了:1,3,2,4

 

然后让a2=a1/2=1,继续对当前的数列进行排序,于是就把当前的数列分成了一个新的数组(间隔一个元素的距离组成的数组){1,3,2,4}然后对这个数组进行直接插入排序,于是就变成了1,2,3,4,此时a2=1,排序完毕。

 

希尔排序动画演示

 

希尔排序核心代码如下:

 

public void shellSort(int[] arr){
        int temp;//定义一个临时值
        boolean isChange;//判断数组中元素的排列顺序是否有变化,如果有变化,就把当前数组中的元素打印出来
        int splitLength;//定义数组中要排序的元素的间隔距离
        int point;//找出当前要排序的元素的位置,即该元素在数组中位置的下标值
        splitLength=(int)arr.length/2;//规定间隔长度
        while(splitLength!=0){//如果间隔长度不为0
           
            /**下面的for循环的作用是
             */
            for(int i=splitLength;i<arr.length;i++){
                isChange=false;//设置isChange的初始值为false
                temp=arr[i];//让
                point=i-splitLength;
                while(temp<arr[point]&&point>=0&&point<=arr.length){//如果第一个值比后面的值大
                    arr[point+splitLength]=arr[point];//把这个元素放在后面的位置
                    point=point-splitLength;
                    isChange=true;
                    if(point<0||point>arr.length){
                        break;
                    }
                }
                arr[point+splitLength]=temp;
               
               
               
                if(isChange){
                    System.out.print("排序中:");
                    for(int j=0;j<arr.length;j++){
                        System.out.printf("%3s", arr[j]);
                    }
                    System.out.println("");
                }
               
               
               
               
            }
            splitLength=splitLength/2;
        }
        System.out.println("希尔排序完成!");
       
    }

 

 

举例说明如下:

 

package AlgorithmTest;

/**
 *
 * @author Administrator
 */
public class ShellSortTest {
   
    public static void main(String args[]){
        int[] a={2,4,1,3};
        ShellSortTest sst=new ShellSortTest();
        sst.shellSort(a);
    }
   
   
    public void shellSort(int[] arr){
        int temp;//定义一个临时值
        boolean isChange;//判断数组中元素的排列顺序是否有变化,如果有变化,就把当前数组中的元素打印出来
        int splitLength;//定义数组中要排序的元素的间隔距离
        int point;//找出当前要排序的元素的位置,即该元素在数组中位置的下标值
        splitLength=(int)arr.length/2;//规定间隔长度
        while(splitLength!=0){//如果间隔长度不为0
           
            /**下面的for循环的作用是
             */
            for(int i=splitLength;i<arr.length;i++){
                isChange=false;//设置isChange的初始值为false
                temp=arr[i];//让
                point=i-splitLength;
                while(temp<arr[point]&&point>=0&&point<=arr.length){//如果第一个值比后面的值大
                    arr[point+splitLength]=arr[point];//把这个元素放在后面的位置
                    point=point-splitLength;
                    isChange=true;
                    if(point<0||point>arr.length){
                        break;
                    }
                }
                arr[point+splitLength]=temp;
               
               
               
                if(isChange){
                    System.out.print("排序中:");
                    for(int j=0;j<arr.length;j++){
                        System.out.printf("%3s", arr[j]);
                    }
                    System.out.println("");
                }
               
               
               
               
            }
            splitLength=splitLength/2;
        }
        System.out.println("希尔排序完成!");
        showArray(arr);
        System.out.println();
       
    }
   
    public void showArray(int[] arr){
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+"  ");
        }
    }
   
}

 

 

抱歉!评论已关闭.