插入排序->希尔排序
/**
* 插入排序
*/
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 时间复杂度:数学家正在勤劳的探索! 适用条件: 直接插入排序的改进,主要针对移动次数的减少,这取决于"增量队列"的取值 ...
随机推荐
- yum源更换
折腾了半天,怀疑自己能力 的时候,发现原来不是我的错.树莓派换源国内的aliyun,163都不能用,最好找到这个 # CentOS-Base.repo # # The mirror system us ...
- DELPHI 把数据库中的数据转换成XML格式
function ReplaceString(AString: string): string; begin Result := StringReplace(AString, '&', '&a ...
- Nginx 禁止IP访问 只允许域名访问
今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案: Nginx的默认虚拟主机在用户 ...
- 分页框架pager-taglib学习笔记
说到分页其实可以研究一下我自己项目里面的分页框架的使用. 下面的笔记来自于孔浩老师的视频教程和我自己的开发实践. 使用Pager-taglib可以帮助我们快速开发分页处理. 下载:pager-tagl ...
- 获取系统的documents路径
获取路径 https://superuser.com/questions/1132288/windows-command-prompt-get-relocated-users-documents-fo ...
- 跨域、this指向
不要再问我跨域的问题了: https://segmentfault.com/a/1190000015597029?utm_source=tag-newest 不要再问我this的指向问题了: http ...
- Linux 用户和组信息
linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号.在使用linux操作系统时候,通常我们会遇到对多用户进行管理.比如: 用户账号的添加. ...
- 三层for循环求解组成三角形边的组合
假设a.b.c是三角形的三条边,当三条边符合勾股定理时,即,a2+b2=c2 ,为直角三角形.若a.b.c均为小于等于50的整数,求能够组成直角三角形的所有组合.请显示边的各种可能组合情况,显示总的组 ...
- 获取小程序accessToken
private static String getAccessToken(){ String url = "https://api.weixin.qq.com/cgi-bin/token? ...
- vue封装分页组件
element提供的分页是已经封装好的组件,在这里再次封装是为了避免每个用到分页的页面点击跳转时都要写一遍跳转请求 分页组件 <!--分页组件--> <template> &l ...