排序法 | 最差时间分析 | 平均时间复杂度 | 稳定度 | 空间复杂度 |
冒泡排序 | O(n2) | O(n2) | 稳定 | O(1) |
快速排序 | O(n2) | O(n*log2n) | 不稳定 | O(log2n)~O(n) |
选择排序 | O(n2) | O(n2) | 稳定 | O(1) |
二叉树排序 | O(n2) | O(n*log2n) | 不一顶 | O(n) |
插入排序 | O(n2) | O(n2) | 稳定 | O(1) |
堆排序 | O(n*log2n) | O(n*log2n) | 不稳定 | O(1) |
希尔排序 | O | O | 不稳定 | O(1) |
package com.sort.test;
public class QuickSort {
public static void main(String[] args) {
int a[] = {-11,49,38,65,97,76,13,27,49,78,34,12,-1,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
//
quickT(a);
for (int i = 0; i < a.length; i++) {
System.out.print(" "+ a[i]);
}
}
public static void quickT(int[] a){
if(a.length>0){
_quickSortT(a,0,a.length-1);
}
}
private static void _quickSortT(int[] a, int low, int high) {
if(low<high){
int middle = getMiddleT(a,low,high);
//
_quickSortT(a, middle+1, high);
_quickSortT(a, low, middle-1);
}
}
private static int getMiddleT(int[] a, int low, int high) {
//数组第一个作为中轴
int temp = a[low];
while(low<high){
while(low<high && a[high] >= temp){
high -- ;
}
a[low] = a[high];
while(low<high && a[low] <= temp ){
low++;
}
a[high] = a[low];
}
a[low] = temp;
return low;
}
}