插入排序->希尔排序
/**
* 插入排序
*/
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
}
插入排序->希尔排序的更多相关文章
- 学习C#之旅 冒泡排序,选择排序,插入排序,希尔排序[资料收集]
关于冒泡排序,选择排序,插入排序,希尔排序[资料收集] 以下资料来源与网络 冒泡排序:从后到前(或者从前到后)相邻的两个两两进行比较,不满足要求就位置进行交换,一轮下来选择出一个最小(或最大)的放到 ...
- 冒泡排序 & 选择排序 & 插入排序 & 希尔排序 JavaScript 实现
之前用 JavaScript 写过 快速排序 和 归并排序,本文聊聊四个基础排序算法.(本文默认排序结果都是从小到大) 冒泡排序 冒泡排序每次循环结束会将最大的元素 "冒泡" 到最 ...
- 数组排序-冒泡排序-选择排序-插入排序-希尔排序-快速排序-Java实现
这五种排序算法难度依次增加. 冒泡排序: 第一次将数组相邻两个元素依次比较,然后将大的元素往后移,像冒泡一样,最终最大的元素被移到数组的最末尾. 第二次将数组的前n-1个元素取出,然后相邻两个元素依次 ...
- 冒泡排序 选择排序 插入排序希尔排序 java
双向冒泡 package com.huang; public class _014_bubb_sort { int[] b={1,2}; static int a[]={12,4,35,65,43,6 ...
- 内部排序->插入排序->希尔排序
文字描述 希尔排序又称缩小增量排序,也属于插入排序类,但在时间效率上较之前的插入排序有较大的改进. 从之前的直接插入排序的分析得知,时间复杂度为n*n, 有如下两个特点: (1)如果待排序记录本身就是 ...
- 插入排序:直接插入排序&希尔排序
一.直接插入排序 1. 思想 直接排序法, 可以分为两个部分, 一部分是有序的, 一部分是无序的. 从这个图上, 应该是能看清楚直接插入排序的思想了. 将无序部分的第一个与有序部分进行比较. 从有序部 ...
- 插入排序—希尔排序(Shell`s Sort)原理以及Java实现
希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...
- 直接插入排序&希尔排序
1.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...
- PHP 插入排序 -- 希尔排序
1.希尔排序 -- Shell Insertion Sort 时间复杂度:数学家正在勤劳的探索! 适用条件: 直接插入排序的改进,主要针对移动次数的减少,这取决于"增量队列"的取值 ...
随机推荐
- MVC 无法将带 [] 的索引应用于“System.Dynamic.DynamicObject”类型的表达式
无法将带 [] 的索引应用于“System.Dynamic.DynamicObject”类型的表达式 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代 ...
- 【CF1257D】Yet Another Monster Killing Problem【贪心】
题意:给定一些怪物,每天可以选一个勇士进去打怪,每个勇士每天只能打不超过si个怪物,每个勇士只能打能力值≤pi的怪物,问最少多少天打完所有怪物 题解:贪心,每天尽可能多的去打怪,那么存一个对于长度为i ...
- sqlserver安装-1
原文地址: https://blog.csdn.net/qq_41432123/article/details/79053486 下载:(免费使用安装dev版) ed2k://|file|cn_sql ...
- OpenCV笔记:pyrDown()函数和pryUp()函数的使用
OpenCV实现了用于创建图像金字塔的两个函数pyrDown()和pryUp(). 图像金字塔是一种经典的图像多尺寸描述方法,它将降采样和平滑滤波结合在一起,对图像进行多尺度表示.图像金字塔由不同尺寸 ...
- 麦子lavarel---16、日志
麦子lavarel---16.日志 一.总结 一句话总结: 一定要养成打印日志查看日志的好习惯,非常节约时间和便于查错 1.console.Log(“获取类别数据:"):console.Lo ...
- .NETFramework:System.Collections.Generic.KeyValuePair.cs
ylbtech-.NETFramework:System.Collections.Generic.KeyValuePair.cs 定义可设置或检索的键/值对 1.返回顶部 1. #region 程序集 ...
- Visual Studio Code-使用Chrome Debugging for VS Code调试JS
准备工作 安装 Debugger for Chrome 插件 按 F5(或选择菜单栏的 Debug->Start Debuging),然后选择 Chrome,就会自动创建默认的配置文件 &quo ...
- hdu1159Common Subsequence——动态规划(最长公共子序列(LCS))
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- poj3253Fence Repair (Huffman)
Huffman树:具有n个外部节点(叶子节点)的二叉树 每个外部节点都有一个对应的权值Wi 叶节点带权外部路径长度总和WPL=Wi*Li(i从1到n)最小(权越大的节点里根越进) 构造Huffman树 ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...