Java使用DualPivotQuicksort排序

目录
  • 1. 插入排序(insertion sort)
  • 2. 计数排序(counting sort)
  • 3. 快速排序(Quicksort)
    • 3.1 对数组做近似7等分
    • 3.2 对五个切割点进行插入排序
    • 3.3 创建两个变量作为下标记录值
    • 3.4 五个切割点的值都不相同的情况
      • 3.4.1 第一块和第三块处理
      • 3.4.2 第二块处理
    • 3.5 五个切割点的值有相同的情况(单轴分区 Partitioning with one pivot)
    • 4. 合并排序(merge sort)

      Java排序 - DualPivotQuicksort

      这里描述 leftmost = true 的情况,也就是会从数组的开始一直排序到数组的结尾。

      数组类型:int[]long[]short[]char[]float[]double[],还有比较特殊的 byte[]

      1. 插入排序(insertion sort)

      适合长度短的数组排序,对于byte[] 长度小于等于30其它数组长度小于47 的情况,会使用这种排序

      代码以 int[] a 为例:

      // 第一次循环i=j=0,之后每次循环j=i.
      // j = ++i相当于在每次循环的最后执行 {i++; j = i;}
      // j = i++相当于在每次循环的最后执行 {j = i; i++;}
      for (int i = 0, j = i; i < (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 <= (length - 1); count[a[i] - (-128)]++);
      
      // 第二次循环:给 < byte[] a > 赋值
      // 循环结束条件以k为标准,k<=0就会停止;
      // 因为i和k没有固定关系,所以没有增量表达式,但在方法体中利用--i和--k进行增量。
      for (int i = 256, k = length; 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. 快速排序(Quicksort)

      适合长度短的数组排序,插入排序也是快速排序的一种。
      对于byte[] 长度大于30的情况会使用 计数排序,不是这种排序。
      而对于其它数组长度大于等于47并且小于286 的情况,会使用这种排序。

      3.1 对数组做近似7等分

      // 7等分一段的长度近似值
      int seventh = (length >> 3) + (length >> 6) + 1;
      // 一个数组分为7段,则有五个切割点,如下为五个切割点的下标
      int e3 = (left + right) >>> 1; // The midpoint
      int e2 = e3 - seventh;
      int e1 = e2 - seventh;
      int e4 = e3 + seventh;
      int e5 = e4 + seventh;

      3.2 对五个切割点进行插入排序

      // Sort these elements using insertion sort
      if (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 五个切割点的值都不相同的情况

      这种情况会将排序分三块,变量 pivot1pivot2 作为三块区域值的区分:
      第一块区域所有的值都 < pivot1
      第二块区域所有的值都 >= pivot1 并且 <= pivot2
      第三块区域所有的值都 > pivot2

      3.4.1 第一块和第三块处理

      // 取两个值作为分区值
      int pivot1 = a[e2];
      int pivot2 = a[e4];
      
      // 要排序的第一个和最后一个元素被移动到以前由枢轴占据的位置。
      // 当分区完成时,轴心点被交换回它们的最终位置,并从随后的排序中排除。
      a[e2] = a[left];
      a[e4] = a[right];
      
      // less一开始等于left, great一开始等于right。
      // 跳过小于或大于分割值的元素。
      while (a[++less] < pivot1); // 没有判断第一个
      while (a[--great] > pivot2); // 没有判断最后一个
      
      // 循环带outer:,`break outer;`会跳出整个循环,也就是结束整个下面的for循环。
      // less不参与循环,只是一开始给k赋值,less的变化始终是`++less`,用来交换数组中的值。
      outer:
      for (int k = less - 1; ++k <= great; ) {
          int ak = a[k];
          if (ak < pivot1) { // Move a[k] to left part
              a[k] = a[less];
              /*
               * Here and below we use "a[i] = b; i++;" instead
               * of "a[i++] = b;" due to performance issue.
               */
              a[less] = ak;
              ++less;
          } else if (ak > pivot2) { // Move a[k] to right part
              while (a[great] > pivot2) {
                  if (great-- == k) {
                      break outer;
                  }
              }
              if (a[great] < pivot1) { // a[great] <= pivot2
                  a[k] = a[less];
                  a[less] = a[great];
                  ++less;
              } else { // pivot1 <= a[great] <= pivot2
                  a[k] = a[great];
              }
              /*
               * Here and below we use "a[i] = b; i--;" instead
               * of "a[i--] = b;" due to performance issue.
               */
              a[great] = ak;
              --great;
          }
      }
      
      // 循环结束,交换left和(less - 1)的值,也就是处理循环前`a[e2] = a[left];`导致的分区错误
      a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
      // 循环结束,交换right和(great + 1)的值,也就是处理循环前`a[e4] = a[right];`导致的分区错误
      a[right] = a[great + 1]; a[great + 1] = pivot2;
      
      // 分为三部分后,嵌套排序第一部分和第三部分
      sort(a, left, less - 2, leftmost);
      sort(a, great + 2, right, false);

      3.4.2 第二块处理

      分两种情况:
      如果第二块剩余项超过数组要排序总长度的4/7,会将等于pivot1和等于pivot2的值取出来,再次缩减less和great中间的部分,然后进行排序。
      否则直接排序。

      if (less < e1 && e5 < great) { // 剩余的中间部分超过4/7
          /*
           * Skip elements, which are equal to pivot values.
           */
          while (a[less] == pivot1) {
              ++less;
          }
      
          while (a[great] == pivot2) {
              --great;
          }
      
          outer:
          for (int k = less - 1; ++k <= great; ) {
              int ak = a[k];
              if (ak == pivot1) { // Move a[k] to left part
                  a[k] = a[less];
                  a[less] = ak;
                  ++less;
              } else if (ak == pivot2) { // Move a[k] to right part
                  while (a[great] == pivot2) {
                      if (great-- == k) {
                          break outer;
                      }
                  }
                  if (a[great] == pivot1) { // a[great] < pivot2
                      a[k] = a[less];
                      a[less] = pivot1;
                      ++less;
                  } else { // pivot1 < a[great] < pivot2
                      a[k] = a[great];
                  }
                  a[great] = ak;
                  --great;
              }
          }
      }
      
      // Sort center part recursively
      sort(a, less, great, false);
      

      3.5 五个切割点的值有相同的情况(单轴分区 Partitioning with one pivot)

      这种情况也可以理解为将排序分三块,但只需要一个变量 pivot 作为三块区域值的区分:
      第一块区域所有的值都 < pivot
      第二块区域所有的值都 = pivot,因为这块区域的值都相等,最后就可以不用排序
      第三块区域所有的值都 > pivot

      // 取下标在中间的值做一个临时变量,该变量是中值的廉价近似值,作为分割值
      int pivot = a[e3];
      
      // less一开始等于left, great一开始等于right。
      // 方法体内部不断修改great的值,使循环执行的次数不断的缩减,一次循环great可以减少0,可以减少1,可以减少n。
      // less并不影响循环,只是作为临时变量进行数组中值的交换,始终小于等于k,一次循环只能加1或不加。
      for (int k = less; k <= great; ++k) {
          if (a[k] == pivot) { // 如果a[k]的值等于分割值,跳过
              continue;
          }
          int ak = a[k]; // 取出a[k]值赋给临时变量ak
          if (ak < pivot) { // Move a[k] to left part
              a[k] = a[less];
              a[less] = ak;
              ++less;
          } else { // a[k] > pivot - Move a[k] to right part
              while (a[great] > pivot) {
                  --great;
              }
              if (a[great] < pivot) { // a[great] <= pivot
                  a[k] = a[less];
                  a[less] = a[great];
                  ++less;
              } else { // a[great] == pivot
                  /*
                   * Even though a[great] equals to pivot, the
                   * assignment a[k] = pivot may be incorrect,
                   * if a[great] and pivot are floating-point
                   * zeros of different signs. Therefore in float
                   * and double sorting methods we have to use
                   * more accurate assignment a[k] = a[great].
                   */
                  a[k] = pivot;
              }
              a[great] = ak;
              --great;
          }
      }
      
      // 分为三部分后,嵌套排序第一部分和第三部分
      sort(a, left, less - 1, leftmost);
      sort(a, great + 1, right, false);

      4. 合并排序(merge sort)

      长度很长的数组排序,对于其它数组长度大于等于286 的情况,会使用这种排序。

      两个关键常量,起控制作用

      // 合并排序中的最大运行次数
      static final int MAX_RUN_COUNT = 67;
      
      // 合并排序中运行的最大长度
      static final int MAX_RUN_LENGTH = 33;

      排序方法

      /**
       * 长度大于等于286的int数组排序
       * 
       * @param a
       *            要排序int数组
       * @param left
       *            起始下标
       * @param right
       *            结束下标
       * @param work
       *            null
       * @param workBase
       *            0
       * @param workLen
       *            0
       */
      private static void largeSort(int[] a, int left, int right, int[] work,
      			int workBase, int workLen) {
      
          /*
           * Index run[i] is the start of i-th run (ascending or descending
           * sequence).
           */
          int[] run = new int[MAX_RUN_COUNT + 1];
          int count = 0;
          run[0] = left;
      
          // Check if the array is nearly sorted
          for (int k = left; k < right; run[count] = k) {
              if (a[k] < a[k + 1]) { // ascending
                  while (++k <= right && a[k - 1] <= a[k]);
              } else if (a[k] > a[k + 1]) { // descending
                  while (++k <= right && a[k - 1] >= a[k]);
                  for (int lo = run[count] - 1, hi = k; ++lo < --hi;) {
                      int t = a[lo];
                      a[lo] = a[hi];
                      a[hi] = t;
                  }
              } else { // equal
                  for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k];) {
                      if (--m == 0) {
                          sort(a, left, right, true);
                          return;
                      }
                  }
              }
      
              /*
               * The array is not highly structured, use Quicksort instead of
               * merge sort.
               */
              if (++count == MAX_RUN_COUNT) {
                  sort(a, left, right, true);
                  return;
              }
          }
      
          // Check special cases
          // Implementation note: variable "right" is increased by 1.
          if (run[count] == right++) { // The last run contains one element
              run[++count] = right;
          } else if (count == 1) { // The array is already sorted
              return;
          }
      
          // Determine alternation base for merge
          byte odd = 0;
          for (int n = 1; (n <<= 1) < count; odd ^= 1);
      
          // Use or create temporary array b for merging
          int[] b; // temp array; alternates with a
          int ao, bo; // array offsets from 'left'
          int blen = right - left; // space needed for b
          if (work == null || workLen < blen || workBase + blen > work.length) {
              work = new int[blen];
              workBase = 0;
          }
          if (odd == 0) {
              System.arraycopy(a, left, work, workBase, blen);
              b = a;
              bo = 0;
              a = work;
              ao = workBase - left;
          } else {
              b = work;
              ao = 0;
              bo = workBase - left;
          }
      
          // Merging
          for (int last; count > 1; count = last) {
              for (int k = (last = 0) + 2; k <= count; k += 2) {
                  int hi = run[k], mi = run[k - 1];
                  for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
                      if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
      					b[i + bo] = a[p++ + ao];
                      } else {
      				    b[i + bo] = a[q++ + ao];
      				}
      			}
      			run[++last] = hi;
      		}
      		if ((count & 1) != 0) {
      			for (int i = right, lo = run[count - 1]; --i >= lo;
      			    b[i + bo] = a[i + ao]
      			);
      			run[++last] = right;
      		}
      		int[] t = a;
      		a = b;
      		b = t;
      		int o = ao;
      		ao = bo;
      		bo = o;
      	}
      }

      以上就是Java使用DualPivotQuicksort排序的详细内容,更多关于DualPivotQuicksort排序的资料请关注其它相关文章!

      本文转自网络,如有侵权请联系客服删除。