/**
* 插入排序
*/
public class InsertSort {
public static void main(String[] args){
int[] arr = {5,5,2,6,3,4};
int length = arr.length;
int a,b,key;
for (a = 1; a < length; a++) {
key = arr[a];
b = a - 1; //1
while (b>=0&&arr[b]>key){
arr[b+1] = arr[b];
b--;
}
//while不执行arr[b+1]->arr[a]
arr[b+1] = key;
System.out.println();
for (Integer integer : arr) {
System.out.print(integer+" ");
}
}
//5 5 2 6 3 4
//2 5 5 6 3 4
//2 5 5 6 3 4
//2 3 5 5 6 4
//2 3 4 5 5 6
}
}

/**
* 希尔排序
*/
public class ShellSort1 {
public static void main(String[] args) {
int[] arr = {5, 5, 2, 6, 3, 4};
for (Integer integer : arr) {
System.out.print(integer+" ");
}
int h = 1;
while (h <= arr.length / 3) {
h = h * 3 + 1;
}
System.out.println();
System.out.println("h:"+h);
while (h > 0) {
for (int a = h; a < arr.length; a++) {
if (arr[a] < arr[a - h]) {
int tmp = arr[a];
int b = a - h;
while (b >= 0 && arr[b] > tmp) {
arr[b + h] = arr[b];
b -= h;
}
arr[b + h] = tmp;
System.out.println();
for (Integer integer : arr) {
System.out.print(integer+" ");
}
}
}
h = (h - 1) / 3;
System.out.println();
System.out.println("h:"+h);
}
//5 5 2 6 3 4
//h:4
//
//3 5 2 6 5 4
//3 4 2 6 5 5
//h:1
//
//2 3 4 6 5 5
//2 3 4 5 6 5
//2 3 4 5 5 6
//h:0
}
}

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* 希尔排序 并行
*/
public class ShellSort{
static int[] arr = {5,5,2,6,3,4};
static ExecutorService pool = Executors.newCachedThreadPool();
public static class ShellSortTask implements Runnable {
int i = 0, h = 0;
CountDownLatch l;
public ShellSortTask(int i, int h, CountDownLatch l) {
this.i = i;
this.h = h;
this.l = l;
} @Override
public void run() {
if (arr[i]<arr[i -h]){
int tmp = arr[i];
int b = i - h;
while (b>=0 && arr[b]>tmp){
arr[b+h] = arr[b];
b -= h;
}
arr[b+h] = tmp;
System.out.println();
for (Integer integer : arr) {
System.out.print(integer+" ");
}
}
l.countDown();
}
}
public static void pShellSort() throws InterruptedException{
int h = 1;
CountDownLatch latch = null;
while (h<=arr.length/3){
h = h * 3 + 1;
}
while (h>0){
System.out.println("h:"+h);
if (h>=4){
latch = new CountDownLatch(arr.length-h);
}
for (int a = h; a < arr.length; a++) {
if (h >= 4) {
pool.execute(new ShellSortTask(a,h,latch));
} else {
if (arr[a] < arr[a - h]) {
int tmp = arr[a];
int b = a - h;
while (b >= 0 && arr[b] > tmp) {
arr[b + h] = arr[b];
b -= h;
}
arr[b + h] = tmp;
System.out.println();
for (Integer integer : arr) {
System.out.print(integer+" ");
}
}
}
}
latch.await();
h = (h-1)/3;
}
pool.shutdown();
}
public static void main(String[] args) throws InterruptedException{
pShellSort();
}
//h:4
//
//3 5 2 6 5
//3 4 2 6 5 5 5 h:1
//
//2 3 4 6 5 5
//2 3 4 5 6 5
//2 3 4 5 5 6
}

插入排序->希尔排序的更多相关文章

  1. 学习C#之旅 冒泡排序,选择排序,插入排序,希尔排序[资料收集]

    关于冒泡排序,选择排序,插入排序,希尔排序[资料收集]  以下资料来源与网络 冒泡排序:从后到前(或者从前到后)相邻的两个两两进行比较,不满足要求就位置进行交换,一轮下来选择出一个最小(或最大)的放到 ...

  2. 冒泡排序 & 选择排序 & 插入排序 & 希尔排序 JavaScript 实现

    之前用 JavaScript 写过 快速排序 和 归并排序,本文聊聊四个基础排序算法.(本文默认排序结果都是从小到大) 冒泡排序 冒泡排序每次循环结束会将最大的元素 "冒泡" 到最 ...

  3. 数组排序-冒泡排序-选择排序-插入排序-希尔排序-快速排序-Java实现

    这五种排序算法难度依次增加. 冒泡排序: 第一次将数组相邻两个元素依次比较,然后将大的元素往后移,像冒泡一样,最终最大的元素被移到数组的最末尾. 第二次将数组的前n-1个元素取出,然后相邻两个元素依次 ...

  4. 冒泡排序 选择排序 插入排序希尔排序 java

    双向冒泡 package com.huang; public class _014_bubb_sort { int[] b={1,2}; static int a[]={12,4,35,65,43,6 ...

  5. 内部排序->插入排序->希尔排序

    文字描述 希尔排序又称缩小增量排序,也属于插入排序类,但在时间效率上较之前的插入排序有较大的改进. 从之前的直接插入排序的分析得知,时间复杂度为n*n, 有如下两个特点: (1)如果待排序记录本身就是 ...

  6. 插入排序:直接插入排序&希尔排序

    一.直接插入排序 1. 思想 直接排序法, 可以分为两个部分, 一部分是有序的, 一部分是无序的. 从这个图上, 应该是能看清楚直接插入排序的思想了. 将无序部分的第一个与有序部分进行比较. 从有序部 ...

  7. 插入排序—希尔排序(Shell`s Sort)原理以及Java实现

    希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...

  8. 直接插入排序&希尔排序

    1.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...

  9. PHP 插入排序 -- 希尔排序

    1.希尔排序 -- Shell Insertion Sort 时间复杂度:数学家正在勤劳的探索! 适用条件: 直接插入排序的改进,主要针对移动次数的减少,这取决于"增量队列"的取值 ...

随机推荐

  1. 【Java架构:进阶技术】——一篇文章搞掂:JVM调优

    Sun官方定义的Java技术体系: Java程序设计语言 各种硬件平台上的Java虚拟机 Class文件格式 Java API类库 来自商业机构和开源社区的第三方Java类库 JDK(Java Dev ...

  2. 第九届ECNU Coder F.蚂蚁(栈)

    题目链接:http://acm.ecnu.edu.cn/contest/16/problem/F/ 题目: F. 蚂蚁 Time limit per test: 0.5 seconds Time li ...

  3. WebGIS常用代码集锦

    一.普通代码 1.坐标转换 ol.proj.transform(coordinate, source, destination) ol.proj.transform(coordinate, 'EPSG ...

  4. MySQL部分索引

    部分索引 char/varchar2太长,全部做索引的话,效率低,浪费存储空间 select avg(length(username)) from 索引统计: show index from tabl ...

  5. Jsoup代码示例、解析网页+提取文本

    使用Jsoup解析HTML 那么我们就必须用到HttpClient先获取到html 同样我们引入HttpClient相关jar包 以及commonIO的jar包 我们把httpClient的基本代码写 ...

  6. StringTokenizer拆分字符串

    今天要做一个过滤特殊字符的需求, 看了下公司以前过滤特俗字符代码, 用的居然是 StringTokenizer, 完全不熟悉啊, 于是恶补了一下, 把StringTokenizer在JDK中的文档也翻 ...

  7. Nginx 模块 - ngx_core_module

    原文地址 示例配置 指令 accept_mutex accept_mutex_delay daemon debug_connection debug_points env error_log even ...

  8. 【Unity知识点】安卓游戏如何在切后台后继续运行

    解决方法很简单,在android项目AndroidManifest.xml文件中的activity中添加如下内容: android:configChanges="fontScale|keyb ...

  9. 【Unity渲染】Camera RenderToCubemap 渲染到立方体纹理

    Unity圣典 传送门:http://www.ceeger.com/Script/Camera/Camera.RenderToCubemap.html Camera.RenderToCubemap 有 ...

  10. PHP 是怎么接收到请求的?

    本篇文章主要描述一下几点 ● nginx 怎么转发请求 给 PHPFPM? ● CGI 和 FastCGI 到底是个什么玩意? ● PHPFPM 是什么?有什么作用? 简单场景描述 在浏览器上访问一个 ...