zoukankan      html  css  js  c++  java
  • 希尔排序

    package com.donghao.shellSort;

    public class ShellSort {

    public static void main(String[] args){
    int maxSize = 10;
    ArraySh arr;
    arr = new ArraySh(maxSize);

    for(int i=0;i<10;i++){
    long n = (int)(Math.random() * 100);
    arr.insert(n);
    }

    System.out.println("排序前");
    arr.display();
    System.out.println();
    arr.shellSort();
    System.out.println("排序后");
    arr.display();
    }
    }

    package com.donghao.shellSort;

    public class ArraySh {

    private long[] theArray;
    private int nElems;

    public ArraySh(int max){
    theArray = new long[max];
    nElems = 0;
    }

    public void insert(long value){
    theArray[nElems++] = value;
    }
    public void display(){
    for(int i=0;i<nElems;i++)
    System.out.print(theArray[i] + " ");
    }

    public void shellSort(){
    int inner,outer;
    long temp;

    int h=1;
    while(h<=nElems/3)
    h=h*3 +1;

    while(h>0){
    for(outer = h;outer<nElems;outer++){
    temp = theArray[outer];
    inner = outer;
    while(inner>h-1 && theArray[inner-h] >= temp){
    theArray[inner] = theArray[inner -h];
    inner -=h;
    } //end while
    theArray[inner] = temp;
    } //end for
    h = (h-1) /3;
    }
    }
    }

  • 相关阅读:
    ES6模块开发+单文件组件
    Vue路由学习
    Vuex学习
    Vue组件
    Vue事件处理
    Git下载前后端代码步骤
    小黑记事本
    简单计算器
    ubuntu的基础命令
    拓扑排序以及求解关键路径
  • 原文地址:https://www.cnblogs.com/xunmengyoufeng/p/2711321.html
Copyright © 2011-2022 走看看