zoukankan      html  css  js  c++  java
  • 【数据结构与算法】希尔排序

    希尔排序的时间复杂度是O(n^1.3)~O(n^2),空间复杂度是O(1)。

    代码例如以下:

    /**
     * 源代码名称: ShellSort.java 
     * 日期:2014-08-11 
     * 程序功能:希尔排序 
     * 版权:CopyRight@A2BGeek 
     * 作者:A2BGeek
     */
    public class ShellSort {
    	public void shellSort(int[] in) {
    		int length = in.length;
    		int span = length / 2;
    		int i, j;
    		while (span >= 1) {
    			// Selection Sort begin
    			for (i = span; i < length; i++) {
    				int tmp = in[i];
    				for (j = i - span; j >= 0 && tmp < in[j]; j -= span) {
    					in[j + span] = in[j];
    				}
    				in[j + span] = tmp;
    			}
    			// Selection Sort end
    			span /= 2;
    			printArray(in);
    		}
    	}
    
    	public void printArray(int[] in) {
    		for (int i : in) {
    			System.out.print(i + " ");
    		}
    		System.out.println();
    	}
    
    	public static void main(String[] args) {
    		int[] testCase = { 1, 3, 4, 10, 2, 5, 6, 7, 9, 11 };
    		ShellSort mShellSort = new ShellSort();
    		mShellSort.printArray(testCase);
    		mShellSort.shellSort(testCase);
    		mShellSort.printArray(testCase);
    	}
    }
    


  • 相关阅读:
    Kafka中的数据清理(logdeletion)
    genymotion虚拟器笔记
    layui hover显示图片
    vue 导出到excel
    el-tree知识积累
    js 含有对象的数组去重
    苏宁易购价格爬取(golang)
    vue+elementui+beego笔记
    vue笔记
    beego笔记
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5227619.html
Copyright © 2011-2022 走看看