• Arrays的排序算法sort及方法使用


      Java工具包中的Arrays工具类里面有数组的快速排序算法。

    源码如下:

      1 /**
      2      * Sorts the specified range of the array using the given
      3      * workspace array slice if possible for merging
      4      *
      5      * @param a the array to be sorted
      6      * @param left the index of the first element, inclusive, to be sorted
      7      * @param right the index of the last element, inclusive, to be sorted
      8      * @param work a workspace array (slice)
      9      * @param workBase origin of usable space in work array
     10      * @param workLen usable size of work array
     11      */
     12     static void sort(int[] a, int left, int right,
     13                      int[] work, int workBase, int workLen) {
     14         // Use Quicksort on small arrays
     15         if (right - left < QUICKSORT_THRESHOLD) {
     16             sort(a, left, right, true);
     17             return;
     18         }
     19 
     20         /*
     21          * Index run[i] is the start of i-th run
     22          * (ascending or descending sequence).
     23          */
     24         int[] run = new int[MAX_RUN_COUNT + 1];
     25         int count = 0; run[0] = left;
     26 
     27         // Check if the array is nearly sorted
     28         for (int k = left; k < right; run[count] = k) {
     29             if (a[k] < a[k + 1]) { // ascending
     30                 while (++k <= right && a[k - 1] <= a[k]);
     31             } else if (a[k] > a[k + 1]) { // descending
     32                 while (++k <= right && a[k - 1] >= a[k]);
     33                 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
     34                     int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
     35                 }
     36             } else { // equal
     37                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
     38                     if (--m == 0) {
     39                         sort(a, left, right, true);
     40                         return;
     41                     }
     42                 }
     43             }
     44 
     45             /*
     46              * The array is not highly structured,
     47              * use Quicksort instead of merge sort.
     48              */
     49             if (++count == MAX_RUN_COUNT) {
     50                 sort(a, left, right, true);
     51                 return;
     52             }
     53         }
     54 
     55         // Check special cases
     56         // Implementation note: variable "right" is increased by 1.
     57         if (run[count] == right++) { // The last run contains one element
     58             run[++count] = right;
     59         } else if (count == 1) { // The array is already sorted
     60             return;
     61         }
     62 
     63         // Determine alternation base for merge
     64         byte odd = 0;
     65         for (int n = 1; (n <<= 1) < count; odd ^= 1);
     66 
     67         // Use or create temporary array b for merging
     68         int[] b;                 // temp array; alternates with a
     69         int ao, bo;              // array offsets from 'left'
     70         int blen = right - left; // space needed for b
     71         if (work == null || workLen < blen || workBase + blen > work.length) {
     72             work = new int[blen];
     73             workBase = 0;
     74         }
     75         if (odd == 0) {
     76             System.arraycopy(a, left, work, workBase, blen);
     77             b = a;
     78             bo = 0;
     79             a = work;
     80             ao = workBase - left;
     81         } else {
     82             b = work;
     83             ao = 0;
     84             bo = workBase - left;
     85         }
     86 
     87         // Merging
     88         for (int last; count > 1; count = last) {
     89             for (int k = (last = 0) + 2; k <= count; k += 2) {
     90                 int hi = run[k], mi = run[k - 1];
     91                 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
     92                     if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
     93                         b[i + bo] = a[p++ + ao];
     94                     } else {
     95                         b[i + bo] = a[q++ + ao];
     96                     }
     97                 }
     98                 run[++last] = hi;
     99             }
    100             if ((count & 1) != 0) {
    101                 for (int i = right, lo = run[count - 1]; --i >= lo;
    102                     b[i + bo] = a[i + ao]
    103                 );
    104                 run[++last] = right;
    105             }
    106             int[] t = a; a = b; b = t;
    107             int o = ao; ao = bo; bo = o;
    108         }
    109     }

      java.util.Arrays类能方便的操作数组,它所有的方法都是静态的。

      1.filll方法 :给数组中的某段元素附上相同值。

      2.sort方法:对数组中某段元素排序。

      3.equals方法:比较两个数组,判断的是数组中元素值是否相等。

      4.binarySearch方法:对排过序的数组进行二分法查找。

    测试用例:

     1 package recursion;
     2 
     3 import java.util.Arrays;
     4 
     5 /**
     6  * @author zsh
     7  * @company wlgzs
     8  * @create 2019-02-17 9:33
     9  * @Describe Arrays方法测试
    10  */
    11 public class TestForArrays {
    12 
    13     public static void main(String[] args) {
    14         //填充数组,将arr[]中所有元素的值初始为0
    15         int[] arr = new int[5];
    16         Arrays.fill(arr,12);
    17         System.out.println(Arrays.toString(arr));
    18         //将arr中的第2个到第三个元素的值赋为8
    19         Arrays.fill(arr,1,3,8);
    20         System.out.println(Arrays.toString(arr));
    21         //对数组进行排序
    22         int[] arr1 = new int[]{7,6,8,5,2,9,8,1,3,5};
    23         //对数组的第二个到第6个元素进行排序
    24         Arrays.sort(arr1,1,6);
    25         System.out.println(Arrays.toString(arr1));
    26         //对整个数组进行排序
    27         Arrays.sort(arr1);
    28         System.out.println(Arrays.toString(arr1));
    29         //比较数组元素是否相等
    30         System.out.println(Arrays.equals(arr,arr1));
    31         //使用二分法在数组中查找指定元素所在的下标
    32         //数组必须是先排好序的
    33         System.out.println(Arrays.binarySearch(arr1,5));
    34         //如果不存在,就返回负数
    35         System.out.println(Arrays.binarySearch(arr1,20));
    36     }
    37 
    38 
    39 }

    控制台输出:

  • 相关阅读:
    ios web 媒体查询兼容
    Linux python 虚拟环境管理
    three.js 纹理动画实现
    three.js 在模型上移动相机
    three.js 模型拖动之DragControls控制器
    three.js 添加html内容、文本
    微信公众号对接记录
    事务的日志
    事务的隔离级别
    事务中的锁
  • 原文地址:https://www.cnblogs.com/zsh-blogs/p/10390207.html
Copyright © 2020-2023  润新知