• 快排排序


    快排:排序不稳定。每当两次分割的区域都均匀大小时,为最好情况。空间复杂度O(logn)~O(n)之间。时间复杂度一般和最好情况为O(nlogn),最坏为O(n*n)。

     1 package datasort;
     2 //快排排序O(nlogn)
     3 public class QuickSort {
     4     public static void QuickSort(int[] array){
     5         if(array != null){
     6             quickSort(array, 0, array.length-1);
     7         }
     8     }
     9     
    10     private static void quickSort(int[] array,int beg,int end){
    11         if(beg >= end || array == null)
    12             return;
    13         int p = partition(array, beg, end);
    14         quickSort(array, beg, p-1);
    15         quickSort(array, p+1, end);
    16     }
    17 
    18 private static int partition(int[] array, int beg, int end) {
    19         int first = array[beg];
    20         int i = beg, j = end;
    21         while (i < j) {
    22             while (array[i] <= first && i < end) {
    23                 i++;
    24             }
    25             while (array[j] > first && j >= beg) {
    26                 j--;
    27             }
    28             if (i < j) {
    29                 System.out.print("array["+i+"],array["+j+"]("+array[i]+","+array[j]+")--->");
    30                 array[i] = array[i] ^ array[j];               
    31                 array[j] = array[i] ^ array[j];
    32                 array[i] = array[i] ^ array[j];
    33                 System.out.println("array["+i+"],array["+j+"]("+array[i]+","+array[j]+")");
    34             }
    35         }
    36         if (j != beg) {
    37             array[j] = array[beg] ^ array[j];
    38             array[beg] = array[beg] ^ array[j];
    39             array[j] = array[beg] ^ array[j];
    40         }
    41         return j;
    42     }
    43     public static void main(String[] args) {
    44         int[] a={28,4,36,2,65,14,55,17};
    45         QuickSort(a);
    46         System.out.println();
    47         for(int i=0;i<a.length;i++){
    48             System.out.print(a[i]+" ");
    49         }
    50     }
    51 }

  • 相关阅读:
    线程池和进程池
    初识数据库
    线程q
    event事件
    死锁和递归锁
    信号量
    PythonStudy——线程中的几种消息队列
    PythonStudy——GIL Global Interpreter Lock 全局解释器锁
    PythonStudy——异步回调
    PythonStudy——日志模块 logging
  • 原文地址:https://www.cnblogs.com/yunger/p/5751888.html
Copyright © 2020-2023  润新知