如何对一个亿的数组进行快速排序

2023-11-08 20:38

本文主要是介绍如何对一个亿的数组进行快速排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

总结概括:  

      1.数据结构   归并排序 (也是后续排序 LRD)

       2.多线程     ForkJoin框架  繁重任务的并行计算框架,map-reduce思想

计算代码

/****@author dongsheng*@date 2019/1/18 22:58*@Description:*@version 1.0.0*/
public class ArrayMergerSortTask extends RecursiveAction {// implementation details follow:static final int THRESHOLD = 1000;final int[] array;final int lo, hi;ArrayMergerSortTask(int[] array, int lo, int hi) {this.array = array;this.lo = lo;this.hi = hi;}ArrayMergerSortTask(int[] array) {this(array, 0, array.length);}protected void compute() {if (hi - lo < THRESHOLD)		//小于1000,就排序sortSequentially(lo, hi);else {int mid = (lo + hi) >>> 1;		//大于1000,拆分invokeAll(new ArrayMergerSortTask(array, lo, mid),new ArrayMergerSortTask(array, mid, hi));merge(lo, mid, hi);}}void sortSequentially(int lo, int hi) {Arrays.sort(array, lo, hi);		//利用JDK自带的排序进行}void merge(int lo, int mid, int hi) {int[] buf = Arrays.copyOfRange(array, lo, mid);for (int i = 0, j = lo, k = mid; i < buf.length; j++)array[j] = (k == hi || buf[i] < array[k]) ? buf[i++] : array[k++];}public static void main(String[] args) throws Exception {// 这里以一个长度为2千的数组做示例int length = 2_000;int[] array = new int[length];// 填充数值Random random = new Random();for (int i = 0; i < length; i++) {array[i] = random.nextInt();System.out.println(array[i]);}// 利用forkjoinpool来完成多线程快速归并排序ArrayMergerSortTask stask = new ArrayMergerSortTask(array);ForkJoinPool pool = new ForkJoinPool();pool.submit(stask);// 等待任务完成stask.get();System.out.println("----------排序后的结果:");for (int d : array) {System.out.println(d);}}
}

RecursiveAction  
    ForkJoinTask 的子类, 是 ForkJoinTask 的一个子类,它代表了一类最简单的 ForkJoinTask:不需要返回值,当子任务都执行完毕之后,不需要进行中间结果的组合。如果我们从 RecursiveAction 开始继承,那么我们只需要重载 protected void compute() 方法。

源码代码

/******* Written by Doug Lea with assistance from members of JCP JSR-166* Expert Group and released to the public domain, as explained at* http://creativecommons.org/publicdomain/zero/1.0/*/package java.util.concurrent;/*** A recursive resultless {@link ForkJoinTask}.  This class* establishes conventions to parameterize resultless actions as* {@code Void} {@code ForkJoinTask}s. Because {@code null} is the* only valid value of type {@code Void}, methods such as {@code join}* always return {@code null} upon completion.** <p><b>Sample Usages.</b> Here is a simple but complete ForkJoin* sort that sorts a given {@code long[]} array:**  <pre> {@code* static class SortTask extends RecursiveAction {*   final long[] array; final int lo, hi;*   SortTask(long[] array, int lo, int hi) {*     this.array = array; this.lo = lo; this.hi = hi;*   }*   SortTask(long[] array) { this(array, 0, array.length); }*   protected void compute() {*     if (hi - lo < THRESHOLD)*       sortSequentially(lo, hi);*     else {*       int mid = (lo + hi) >>> 1;*       invokeAll(new SortTask(array, lo, mid),*                 new SortTask(array, mid, hi));*       merge(lo, mid, hi);*     }*   }*   // implementation details follow:*   static final int THRESHOLD = 1000;*   void sortSequentially(int lo, int hi) {*     Arrays.sort(array, lo, hi);*   }*   void merge(int lo, int mid, int hi) {*     long[] buf = Arrays.copyOfRange(array, lo, mid);*     for (int i = 0, j = lo, k = mid; i < buf.length; j++)*       array[j] = (k == hi || buf[i] < array[k]) ?*         buf[i++] : array[k++];*   }* }}</pre>** You could then sort {@code anArray} by creating {@code new* SortTask(anArray)} and invoking it in a ForkJoinPool.  As a more* concrete simple example, the following task increments each element* of an array:*  <pre> {@code* class IncrementTask extends RecursiveAction {*   final long[] array; final int lo, hi;*   IncrementTask(long[] array, int lo, int hi) {*     this.array = array; this.lo = lo; this.hi = hi;*   }*   protected void compute() {*     if (hi - lo < THRESHOLD) {*       for (int i = lo; i < hi; ++i)*         array[i]++;*     }*     else {*       int mid = (lo + hi) >>> 1;*       invokeAll(new IncrementTask(array, lo, mid),*                 new IncrementTask(array, mid, hi));*     }*   }* }}</pre>** <p>The following example illustrates some refinements and idioms* that may lead to better performance: RecursiveActions need not be* fully recursive, so long as they maintain the basic* divide-and-conquer approach. Here is a class that sums the squares* of each element of a double array, by subdividing out only the* right-hand-sides of repeated divisions by two, and keeping track of* them with a chain of {@code next} references. It uses a dynamic* threshold based on method {@code getSurplusQueuedTaskCount}, but* counterbalances potential excess partitioning by directly* performing leaf actions on unstolen tasks rather than further* subdividing.**  <pre> {@code* double sumOfSquares(ForkJoinPool pool, double[] array) {*   int n = array.length;*   Applyer a = new Applyer(array, 0, n, null);*   pool.invoke(a);*   return a.result;* }** class Applyer extends RecursiveAction {*   final double[] array;*   final int lo, hi;*   double result;*   Applyer next; // keeps track of right-hand-side tasks*   Applyer(double[] array, int lo, int hi, Applyer next) {*     this.array = array; this.lo = lo; this.hi = hi;*     this.next = next;*   }**   double atLeaf(int l, int h) {*     double sum = 0;*     for (int i = l; i < h; ++i) // perform leftmost base step*       sum += array[i] * array[i];*     return sum;*   }**   protected void compute() {*     int l = lo;*     int h = hi;*     Applyer right = null;*     while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {*       int mid = (l + h) >>> 1;*       right = new Applyer(array, mid, h, right);*       right.fork();*       h = mid;*     }*     double sum = atLeaf(l, h);*     while (right != null) {*       if (right.tryUnfork()) // directly calculate if not stolen*         sum += right.atLeaf(right.lo, right.hi);*       else {*         right.join();*         sum += right.result;*       }*       right = right.next;*     }*     result = sum;*   }* }}</pre>** @since 1.7* @author Doug Lea*/
public abstract class RecursiveAction extends ForkJoinTask<Void> {private static final long serialVersionUID = 5232453952276485070L;/*** The main computation performed by this task.*/protected abstract void compute();/*** Always returns {@code null}.** @return {@code null} always*/public final Void getRawResult() { return null; }/*** Requires null completion value.*/protected final void setRawResult(Void mustBeNull) { }/*** Implements execution conventions for RecursiveActions.*/protected final boolean exec() {compute();return true;}}

 

这篇关于如何对一个亿的数组进行快速排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/372367

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3