排序算法之希尔排序的思想以及Java实现

1 基本思想
shell排序又称之为缩小增量排序,基本思想是,先将待排序序列分割成若干个特殊的子表,分别进行插入排序,当整个表中元素”基本有序”时,再对全体记录进行一次直接插入排序。该方法实质上是一个分组插入方法。

2,算法的实现(Java)

package Algorithm;

public class ShellSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] data = new int[] {11,10,55,78,100,111,45,56,79,90,345,1000};
        System.out.println("排序之前:");
        ShellSort.output(data);
        System.out.println();
        System.out.println("排序之后:");
        ShellSort.Shell_Sort(data);
        ShellSort.output(data);
    }

    //带增量的插入排序
    public static  void Shell_Sort(int[] arr){
            int s = 1;
            while (s < arr.length)
              s = s * 3 + 1;
            while (s >= 1) {
              for (int i = 1; i < arr.length; i++) {
                for (int j = i; j >= s; j = j - s) {
                  if (arr[j] < arr[j - s]) {
                      int temp = arr[j];
                      arr[j]=  arr[j-s];
                      arr[j-s] = temp;
                  } else{
                      break;
                  }
                }
              }
              s = s / 3;
            }
        }

    //输出打印
        public static void output(int[] arr){
            for(int i=0;i<arr.length;i++){
                System.out.print(arr[i]+"\t");
            }
        }

}

最终结果显示如下:
这里写图片描述

3,性能分析
shell排序需要一个记录的辅助空间,它是一种不稳定的排序。

转载于:https://www.cnblogs.com/cmderq/p/9130856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值