Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use DualPivotQuicksort sorting in Java

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use DualPivotQuicksort sorting in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Java sort-DualPivotQuicksort

The case of leftmost = true is described here, that is, it is sorted from the beginning of the array to the end of the array.

Array types: int [], long [], short [], char [], float [], double [], and special byte []

1. Insert sort (insertion sort)

Suitable for array sorting with short length, which will be used when byte [] length is less than or equal to 30 and other array lengths are less than 47

The code takes int [] an as an example:

/ / the first loop i=j=0, and then each loop j=i.// j = + + I equals the execution of {int I at the end of each loop. / / Jaimi + + is equivalent to the execution of {jobi; icycle;} for (jobi I = 0, jigi; I) at the end of each loop

< (length - 1); j = ++i) { int ai = a[i + 1]; // 每次循环的目的是将下一个数排到它应该在的位置,这里ai就是下一个数 while (ai < a[j]) { // while循环的目的是确定j的值 和 把所有比ai大的项向后移一位来腾出ai的位置 a[j + 1] = a[j]; // 把比ai大的项向后移一位 if (j-- == left) { // j-- 确定j的值,也就是确定ai的位置, j 可能等于 -1 break; } } a[j + 1] = ai; // j+1 就是ai的位置}2. 计数排序(counting sort) 只针对byte[] 长度大于30的情况,因为byte的范围是[-128, 127],只有256个数,所以循环会利用这点 int[] count = new int[256];// 第一次循环:计数for (int i = (0 - 1); ++i 赋值// 循环结束条件以k为标准,k 0; ) { while (count[--i] == 0); // 如果计数个数为0,什么也不做,--i byte value = (byte) (i + (-128)); int s = count[i]; do { a[--k] = value; } while (--s >

0); 3. Quick sort (Quicksort)

Suitable for short array sort, insert sort is also a kind of quick sort.

Count sort is used for cases where the length of byte [] is greater than 30, not this sort.

This sort is used for other cases where the array length is greater than or equal to 47 and less than 286.

3.1 approximate length of 7 / 7 equal segments of the array int seventh = (length > > 3) + (length > > 6) + 1 seventh;int seventh;int / an array is divided into 7 segments, then there are five cutting points, as follows: subscript seventh;int e3 = (left + right) > 1 seventh;int / The midpointint e2 = e3-seventh;int e1 = e2-seventh;int e4 = e3 + seventh;int e5 = e4 + seventh 3.2 insert sorting of five cutting points / / Sort these elements using insertion sortif (a [e2]

< a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t; if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }}if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t; if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } }}if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t; if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } } }}3.3 创建两个变量作为下标记录值// 中心部分第一个元素的索引int less = left;// 右部分第一个元素前的索引int great = right;3.4 五个切割点的值都不相同的情况 这种情况会将排序分三块,变量 pivot1 和 pivot2 作为三块区域值的区分: 第一块区域所有的值都 < pivot1 第二块区域所有的值都 >

= pivot1 and pivot2

3.4.1 the first and third blocks process / / take two values as partition values int pivot1 = a [e2]; int pivot2 = a [e4]; / / the first and last elements to be sorted are moved to the position previously occupied by the pivot. / / when the partition is complete, the pivot points are swapped back to their final position and excluded from subsequent sorting. A [e2] = a [left]; a [e4] = a [right]; / / less is equal to left at first, and great is equal to right at first. / / Skip elements that are less than or greater than the split value. While (a [+ + less])

< pivot1); // 没有判断第一个while (a[--great] >

Pivot2); / / it is not determined that the last / / loop with outer:, `break outer; `will jump out of the whole loop, that is, end the entire for loop below. / / less does not participate in the loop, but assigns a value to k at the beginning. The change of less is always `+ + less`, which is used to exchange values in the array. Outer:for (int k = less-1; + + k pivot2) {/ / Move a [k] to right part while (a [great] > pivot2) {if (great-- = = k) {break outer;}} if (a [great] < pivot1) {/ / a [great]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report