利用Java进行希尔排序(元素中有0会有问题),步长经过调试length/2+1最合适。

 import java.util.Scanner;

 public class HierSort {
private static int array[] = new int[100000]; private static void setArray(int length){
Scanner scanner = new Scanner(System.in);
System.out.println("Please entry Array elements:");
for (int i = 0; i < length; i++) {
int num = scanner.nextInt();
array[i] = num;
}
} private static void show(int length){
System.out.println("Output this array:");
for (int i = 0; i < length; i++) {
System.out.print(array[i] + " ");
}
} private static void Hier(int length){
// define a flag as this HieSort of path
int flag;
flag = length / 2 + 1;
while (flag > 0){
for (int i = 0; i < length; i++) {
// judge of edge because the length less than the length of array
if (array[i] > array[i+flag] && array[i+flag] != array[length]){
int swap;
swap = array[i+flag];
array[i+flag] = array[i];
array[i] = swap;
}
}
// System.out.println(flag);
// sub this flag util the f lag equals one
flag--;
}
} public static void main(String[] args){
int length;
Scanner scanner = new Scanner(System.in);
System.out.println("Please the length of array(0<x<10000):");
length = scanner.nextInt();
setArray(length);
Hier(length);
show(length);
}
}

HierSort(希尔)————Java的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 希尔排序(java)

    时间复杂度为O( n^(3/2) )不是一个稳定的排序算法 如何看一个算法是否稳定:{("scala",12),("python",34),("c++ ...

  3. 希尔排序及希尔排序java代码

    原文链接:http://www.orlion.ga/193/ 由上图可看到希尔排序先约定一个间隔(图中是4),然后对0.4.8这个三个位置的数据进行插入排序,然后向右移一位对位置1.5.9进行插入排序 ...

  4. java实现的排序(插入/希尔/归并)

    java实现三种简单的排序,以下是代码: /*插入排序*/ public static void insertionSort(int[] a) { int j; for(int p = 1; p &l ...

  5. Java实现希尔排序(增量递减排序)

    package Insert.sort; import java.util.Scanner; /*又叫缩小增量排序,本质是插入排序,将待排的序列增量分成几个子序列,分别对每个子序列进行直接插入排序 * ...

  6. Java实现希尔排序

            华杰让我看了一道面试题:现有一段程序S,可以对任意n个数进行排序.如果现在需要对n^2个数进行排序,最少需要调用S多少次?(只允许调用S,不可以做别的操作).         看到了这 ...

  7. 希尔排序java

    希尔排序简述 希尔排序是基于插入排序的以下两点性质而提出改进方法的: 插入排序在对几乎已经排好序的数据操作时,效率高,即可以达到线性排序的效率.(希尔排序先将部分数据进行排序,相当于已经部分排好序) ...

  8. 希尔排序----java实现

    思路:希尔排序是分组基础上的直接插入排序,给定的一个步长数组,每个小组先直接插入排序.虽然有四次循环,但是每次循环次数少. package com.sheepmu.text; import java. ...

  9. Java排序算法之希尔(Shell)排序

    基本思想: 希尔排序就是对直接插入排序的一个优化.现在有一个array,希尔排序就是设定一个增量incrementNum(0<incrementNum<array.length).先从ar ...

随机推荐

  1. 像素 转换 px dp

    public static int dip2px(Context context, float dpValue){ final float scale = context.getResources() ...

  2. JS函数入门

    一. 函数的声明及调用 * 1函数的格式:function 函数名(参数1,参数2......){ * //函数体 * return 结果: * * } * 函数调用的格式: * 直接调用:函数名(参 ...

  3. Netty实践二(心跳检测)

    我们使用Socket通信一般经常会处理多个服务器之间的心跳检测,一般来讲,我们去维护服务器集群,肯定要有一台或几台服务器主机(Master),然后还应该有N台(Slave),那么我们的主机肯定要时时刻 ...

  4. 问题2:css图片、文字居中

    1. 文本或图片水平对齐:父元素中添加以下样式     text-align : center;2. 单行文字垂直对齐:父元素中添加以下样式     line-height : 父元素高度; 3.图片 ...

  5. TZOJ 2588 Bad Grass(DFS)

    描述 Bessie was munching on tender shoots of grass and, as cows do, contemplating the state of the uni ...

  6. HDU 1754 I Hate It(线段树区间查询,单点更新)

    描述 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...

  7. 常用的key和oid

    1.FortiGate Template-Network-Office-Fortigate-Session Count:key,fgSysSesCount   oid,.1.3.6.1.4.1.123 ...

  8. 使用mybatis-generator-core工具自动生成mybatis实体

    我们可以使用mybatis-generator-core这个工具将数据库对象转换成mybatis对象,具体步骤如下. 1.mybatis-generator-core下载 下载地址:http://do ...

  9. swift - 自定义tabbar按钮的操作

    1.自定义tabbar按钮 只能 present出来VC 或者 nav. 因为它本身 没有导航控制器, 只有在tabbar 的根导航控制器的 VC 才能push

  10. cloudera-hdfs 告警处理

    2018-03-13 11:15:17,215 WARN [org.apache.flume.sink.hdfs.HDFSEventSink] - HDFS IO error org.apache.h ...