插入排序->希尔排序
时间:2019-09-09
本文章向大家介绍插入排序->希尔排序,主要包括插入排序->希尔排序使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
/**
* 插入排序
*/
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
}
原文地址:https://www.cnblogs.com/fly-book/p/11491280.html
- 常见.NET功能代码汇总 (3) 33,彻底关闭Excel进程
- Vue.js 入门指南之“前传”(含sublime text 3 配置) 1,下载安装Node.js2,配置Vue环境3,Vue初探4,配置sublime Text
- JavaScript的“原型甘露”
- JSP开发过程遇到的中文乱码问题及解决方法
- 求连续操作(登录)数量(次数)最大的记录(用户)
- 使用Topshelf创建自宿主的Windows服务程序
- 如何用ORM支持SQL语句的CASE WHEN?
- 条件表达式的短路求值与函数的延迟求值
- 使用CTE解决复杂查询的问题
- WinDbg调试.NET程序入门
- Release编译模式下,事件是否会引起内存泄漏问题初步研究 疑问:
- 10种简单的Java性能优化学习
- 巧用枚举类型,实现项目的多语言切换
- 影响Java调用性能有哪些因素
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一个价值百万的思路,如何下载去水印视频
- Go语言入门(十) Mysql与Redis操作
- 【Vue进阶】——如何实现组件属性透传?
- Go语言入门(九) 文件操作
- zookeeper完整详细版
- redis学习(十九)
- Android开发6年,互联网寒冬公司倒闭后,耗时3个月北上广求职,终拿到头条Offer!
- 直播软件开发如何使用FFMPEG推流并保存在本地
- react-router学习笔记
- 尤大 3 天前发在 GitHub 上的 vue-lit 是啥?
- BFE.dev前端刷题 23. 实现一个sum()方法
- 彻底深刻理解js原型链之prototype,proto以及constructor(一)
- SAP Spartacus取cart的HTTP请求
- 记一次Netty连接池FixedChannelPool连接未释放问题的排查总结
- 神经网络中的蒸馏技术,从Softmax开始说起