zoukankan      html  css  js  c++  java
  • 用荷兰国旗 改进的 快排

    速记快排 时间复杂度  O(N * logN)  额外空间 O(logN)

      1 package my_basic;
      2 
      3 import java.util.Arrays;
      4 
      5 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
      6 
      7 /**
      8  * 用荷兰国旗改进快排
      9  */
     10 public class QuickSort {
     11     public static void quickSort(int[] arr) {
     12         if (arr == null || arr.length < 2) {
     13             return;
     14         }
     15         quicksort(arr, 0, arr.length-1);
     16     }
     17     
     18     public static void quicksort (int[] arr,int l,int r) {
     19         if (l < r) {
     20             
     21             swap(arr, l + (int) (Math.random() * (r - l + 1)), r);   //随机快排
     22             int[] p = partition(arr, l, r);
     23             quicksort(arr, l, p[0]-1);   //经典快排每次只搞定一个  这里做了优化
     24             quicksort(arr, p[1]+1, r);
     25             
     26         }
     27         
     28     }
     29     
     30     public static int[] partition(int[] arr, int l, int r) {
     31         
     32         int less= l-1;
     33         int more = r;
     34         while (l < more) {
     35             if (arr[l] < arr[r]) {
     36                 swap(arr, ++less, l++);
     37             }else if (arr[l]>arr[r]) {
     38                 swap(arr,--more,l);
     39             }else {
     40                 l++;
     41             }
     42         }
     43         swap(arr, more, r);
     44         
     45         return new int[] {less + 1 , more };  // =num 的左右边界
     46     }
     47     public static void swap(int[] arr, int i, int j) {
     48         
     49         int tmp = arr[i];
     50         arr[i]=arr[j];
     51         arr[j]=tmp;        
     52     }
     53     
     54     //对数器
     55     public static int[] generateRandomArray(int maxSize,int maxValue) {
     56         int[] arr = new int[(int) ((maxSize+1)*Math.random())];
     57         for (int i = 0; i < arr.length; i++) {
     58 //            arr[i] = (int) ((maxValue+1)* Math.random() - (int)(maxValue*Math.random()));
     59             arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
     60         }
     61         return arr;
     62     }
     63     public static void printArray(int[] arr) {
     64         if (arr == null) {
     65             return;
     66         }
     67         for (int i = 0; i < arr.length; i++) {
     68             System.out.print(arr[i]+" ");
     69         }
     70         System.out.println();
     71     }
     72     public static int[] copyArray(int[] arr) {
     73         if (arr == null) {
     74             return null;
     75         }
     76         int[] res = new int[arr.length];
     77         for (int i = 0; i < arr.length; i++) {
     78             res[i] = arr[i];
     79         }
     80         return res;
     81     }
     82     
     83 
     84     public static void comparator(int[] arr) {
     85         Arrays.sort(arr);        
     86     }
     87     public static boolean isEqual(int[] arr1, int[] arr2) {
     88         if ((arr1==null && arr2!=null) || (arr1!=null && arr2==null) ) {
     89             return false;
     90         }
     91         if (arr1.length != arr2.length) {
     92             return false;
     93         }
     94         for (int i = 0; i < arr2.length; i++) {
     95             if (arr1[i] != arr2[i]) {
     96                 return false;
     97             }
     98         }
     99         return true;
    100     }
    101     
    102     public static void main(String[] args) {
    103         int testTime = 500000;
    104         int maxSize = 6;
    105         int maxValue = 100;
    106         boolean succeed = true;
    107         for (int i = 0; i < testTime; i++) {
    108             int[] arr1 = generateRandomArray(maxSize, maxValue);
    109             int[] arr2 = copyArray(arr1);
    110             quickSort(arr1);
    111             comparator(arr2);
    112             if (!isEqual(arr1, arr2)) {
    113                 succeed = false;
    114                 printArray(arr1);
    115                 printArray(arr2);
    116                 break;
    117             }
    118         }
    119         System.out.println(succeed ? "Nice!" : "Fucking fucked!");
    120     }
    121 }
  • 相关阅读:
    024.Kubernetes掌握Pod-部署MongoDB
    023.Kubernetes掌握Pod-Pod扩容和缩容
    附010.Kubernetes永久存储之GlusterFS超融合部署
    附009.Kubernetes永久存储之GlusterFS独立部署
    022.Kubernetes掌握Pod-Pod升级和回滚
    021.Kubernetes掌握Pod-Pod调度策略
    020.Kubernetes掌握Pod-Pod基础使用
    018.Kubernetes二进制集群插件metrics-dashboard
    016.Kubernetes二进制集群插件coredns
    .NET Core 3.0之深入源码理解ObjectPool(二)
  • 原文地址:https://www.cnblogs.com/lihuazhu/p/10753172.html
Copyright © 2011-2022 走看看