JAVA并发处理经验(四)并行模式与算法5:并行排序模式-希尔排序
来源:互联网 发布:java开发的好玩的游戏 编辑:程序博客网 时间:2024/06/07 00:26
一、前言
前面有冒泡排序引入奇偶性的冒泡排序
这里由插入排序,到希尔分组插入排序
二、插入排序
插入排序:将数据插入有序数列,默认第一个数据位有序数列。大值后移----小值找到合适位置
希尔排序:跟插入排序一样只是插入时候中间有一个间断值,例如n;没分割多少个,几个为一组;package pattern.sort;import java.util.Arrays;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * Created by ycy on 16/1/18. * 插入排序,假设将无序的数据插入有序的数据.从此一个排序就完成了 * 一般我将第一个数据定义为已经排序好的数据 */public class insertSort { static int[] arr = {1, 4, 2, 5, 6, 3}; public static void insertSort(int[] arr) { int length = arr.length; int j, i, key; for (i = 0; i < length; i++) { //为key 准备插入的元素 key = arr[i]; j = i - 1; //如果前置大于后值:大值后移 while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } //找到合适的位置,插入key arr[j + 1] = key; } } /** * 希尔排序(分组排序) * * @param arr 排序真谛: * 1'h为子组的大小,表示每次与之对比的值,在几个h之后,最后h为1 * 2'将a[i]<a[i-h] 值,前值小于后值,执行大值放入a[] */ public static void shellSort(int[] arr) { //计算出最大的h值 int h = 1; while (h <= arr.length / 3) { h = h * 3 + 1; } while (h > 0) { for (int i = h; i < arr.length; i++) { //后值大于前值 if (arr[i] < arr[i - h]) { //将较小值放入tmp int tmp = arr[i]; //用现在i值减去初始i值,得到j int j = i - h; //如果j大于零,a[j]>tmp while (j >= 0 && arr[j] > tmp) { //小值放入a[j+h] arr[j + h] = arr[j]; j -= h; } //将大值放入j+h arr[j + h] = tmp; } } //计算出下一个h值 h = (h - 1) / 3; } } ////////////////希尔算法的并行程序 static ExecutorService pool = Executors.newCachedThreadPool(); public static class ShellSortTask implements Runnable { int i = 0; int h = 0; CountDownLatch l; public ShellSortTask(int i, int h, CountDownLatch lathc) { this.i = i; this.h = h; this.l = lathc; } public void run() { if (arr[i] < arr[i - h]) { int tmp = arr[i]; int j = i - h; while (j >= 0 && arr[j] > tmp) { arr[j + h] = arr[j]; j -= h; } arr[j + h] = tmp; } l.countDown(); } } public static void pShellSort(int[] arr) throws InterruptedException { //计算出最大的n值 int h = 1; CountDownLatch lathc = null; while (h <= arr.length / 3) { h = h * 3 + 1; } while (h > 0) { System.out.println("h=" + h); if (h >= 4) { lathc = new CountDownLatch(arr.length - h); } for (int i = h; i < arr.length; i++) { //控制线程数量 if (h >= 4) { pool.submit(new ShellSortTask(i, h, lathc)); } else { if (arr[i] < arr[i - h]) { int tmp = arr[i]; int j = i - h; while (j >= 0 && arr[j] > tmp) { arr[j + h] = arr[j]; j -= h; } arr[j + h] = tmp; } System.out.println(Arrays.toString(arr)); } } //等等线程排序完成,进入下一次排序 lathc.await(); //计算下一个h值 h = (h - 1) / 3; } } public static void main(String[] args) throws InterruptedException { System.out.println(Arrays.toString(arr)); pShellSort(arr); for (int i : arr ) { System.out.println(i); } }}三、预告
本来准备讲解矩阵乘法的,但是是根据我看资料的结果需要适用jMatriees 开源东西,就算了。因为目前矩阵适用,在我 的工作中基本没有,作为应用工程师,还是很少得。
0 0
- JAVA并发处理经验(四)并行模式与算法5:并行排序模式-希尔排序
- JAVA并发处理经验(四)并行模式与算法5:并行排序模式-奇偶性排序
- JAVA并发处理经验(四)并行模式与算法3:并行流水线模式
- JAVA并发处理经验(四)并行模式与算法4:并行搜索模式
- JAVA并发处理经验(四)并行模式与算法1:生产者消费与单例模式
- JAVA并发处理经验(四)并行模式与算法2:Future模式
- JAVA并发处理经验(四)并行模式与算法6:socket的服务端多线程
- JAVA并发处理经验(四)并行模式与算法6:NIO网络编程
- JAVA并发处理经验(四)并行模式与算法7:AIO网络编程
- 《Java高并发程序设计》总结--5. 并行模式与算法
- 并行模式与算法
- 《Java高并发程序设计》学习 --5.9 并行模式之并行算法:矩阵乘法
- Java并行模式:并行流水线
- java高并发-并行模式(上).md
- java高并发-并行模式(下).md
- 并行排序算法
- Java排序算法(四):希尔排序
- 第五章 并行模式与算法
- 6、模型Bean:Model Bean
- 决策树系列之一决策树的入门教程
- JS 一定要放在 Body 的最底部么?聊聊浏览器的渲染机制
- Yii 常量的轻松管理
- java160116StringMethodDemo
- JAVA并发处理经验(四)并行模式与算法5:并行排序模式-希尔排序
- Hrbust 1041 Chocolate Auction
- java160116StringTest
- ceph学习笔记
- java160116StringTest2
- Android中的事件处理机制
- Java、Android超精确测量代码执行时间差
- UVA 543 Goldbach's Conjecture
- Myeclipse 10 Maven 构建 Java Web 项目