Java: Sort

 

 

希尔排序

复制代码
public class Sort{
  public static void main(String[] args){
    Random random = new Random(System.currentTimeMillis());
    int[] ints = new int[10];
    for(int i = 0; i < ints.length; i++){
      ints[i] = random.nextInt(10) + 1;
    }

    int[] copy = Arrays.copyOfRange(ints, 0, ints.length);
    Arrays.sort(copy);
    System.out.println("\033[37;7m>>>>>> " + Arrays.toString(copy) + " <<<<<<\033[0m");
    System.out.println("\033[37;7m>>>>>> " + Arrays.toString(ints) + " <<<<<<\033[0m");
    shellSort(ints);

    System.out.println("\033[37;7m>>>>>> " + Arrays.toString(ints) + " <<<<<<\033[0m");
  }

  private static void shellSort(int[] array){
    if(array.length < 2)
      return;

    int key, cursor;
    // step不断变小, 分组随之变少, 分组数 = step
    for(int step = array.length / 2; step > 0; step /= 2){
      // i++: 才能保证所有分组比较, i+=step: 只进行了一个分组迭代
      /**
       * 迭代过程(设有三个分组0,1,2): 迭代过程每个分组的有序区不断交替变大
       * 第一轮: 0,1,2 每个分组有序区+1 => 2
       * 第二轮: 0,1,2 每个分组有序区+1 => 3
       * 第三轮: 0,1,2 每个分组有序区+1 => 4
       * ...
       */
      for(int i = step; i < array.length; i++){
        key = array[i];
        cursor = i - step;
        while(cursor >= 0){
          if(key < array[cursor]){
            array[cursor + step] = array[cursor];
          }else{
            break;
          }
          cursor -= step;
        }
        array[cursor + step] = key;
      }
    }
  }
}
复制代码

 

单边快排

复制代码
private static void quickSort(int[] array, int low, int high){
    if(low > high){  // low==high: 分区只有一个值,  low>high: pivot为分区极小值,  pivot为分区极大值: 此时右分区low为high+1, 不满足条件
      return;
    }
    int base = partition(array, low, high);
    quickSort(array, low, base - 1);  // 左分区
    quickSort(array, base + 1, high);  // 右分区

  }

  private static int partition(int[] array, int low, int high){
    int pivot = array[high];
    int base = low;
    int tmp;
    for(int cursor = base; cursor < high; cursor++){  // base左边的值小于pivot
      if(array[cursor] < pivot){
        if(cursor != base){
          tmp = array[cursor];
          array[cursor] = array[base];
          array[base] = tmp;
        }
        base++;  // base右移
      }
    }

    if(base != high && array[base] != array[high]){  // base==high, pivot为分区最大值
      tmp = array[base];
      array[base] = pivot;
      array[high] = tmp;
    }

    return base;
  }
复制代码

  

双边快排

复制代码
private static void doubleQuickSort(int[] array, int low, int high){
    // System.out.printf("\033[37;7m low: %d, high: %d \033[0m\n", low, high);
    if(low >= high)
      return;
    int partition = partition(array, low, high);
    doubleQuickSort(array, low, partition - 1);
    doubleQuickSort(array, partition + 1, high);
  }

  private static int partition(int[] array, int low, int high){
    int pivot = array[low];
    int lowCursor = low + 1;
    int highCursor = high;
    int tmp;
    while(lowCursor < highCursor){
      while(pivot < array[highCursor]){
        highCursor--;
      }
      while(lowCursor < highCursor && array[lowCursor] <= pivot){
        lowCursor++;
      }
      if(lowCursor == highCursor){
        System.out.printf("\033[37;7m Cursor: %d, lowCursor == highCursor \033[0m\n", lowCursor);
        break;
      }else{
        tmp = array[lowCursor];
        array[lowCursor] = array[highCursor];
        array[highCursor] = tmp;
      }
    }
    tmp = array[highCursor];
    array[highCursor] = pivot;
    array[low] = tmp;

    return lowCursor;
  }
复制代码

 

归并排序

复制代码
public class D {
    public static void main(String[] args) {
        Random random = new Random(System.currentTimeMillis());
        int[] ints = new int[10];
        for (int v = 0; v < ints.length; ++v)
            ints[v] = random.nextInt(1, 11);  // [1, 10]
        System.out.println(Arrays.toString(ints));

        mergeSort(ints, 0, ints.length - 1);

        System.out.println(Arrays.toString(ints));

    }

    protected static void mergeSort(int[] array, int low, int high) {
        if (low < high) {
            int mid = (low + high) / 2;
            mergeSort(array, low, mid);  // divide
            mergeSort(array, mid + 1, high);  // divide
            merge(array, low, mid, high);  // merge two sorted array
        }
    }

    private static void merge(int[] array, int low, int mid, int high) {
        int[] ints = new int[high - low + 1];
        int lowCursor = low, highCursor = mid + 1, intsCursor = 0;
        while (lowCursor <= mid && highCursor <= high) {
            if (array[lowCursor] <= array[highCursor]) {  // ! use <= to make sort stable
                ints[intsCursor++] = array[lowCursor++];
            } else {
                ints[intsCursor++] = array[highCursor++];
            }
        }
        while (lowCursor <= mid) {  // push rest elements
            ints[intsCursor++] = array[lowCursor++];
        }
        while (highCursor <= high) {  // append rest elements
            ints[intsCursor++] = array[highCursor++];
        }
        System.arraycopy(ints, 0, array, low, high - low + 1);
    }
}
复制代码

 

posted @   ascertain  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-06-05 spec文件汇总
2020-06-05 rpm用法指南
2020-06-05 rpmbuild制作rpm包
2020-06-05 fpm制作rpm包
点击右上角即可分享
微信分享提示