每日一个小算法。快速排序

来源:互联网 发布:手机壁纸软件may 编辑:程序博客网 时间:2024/05/21 22:41
快速排序就是特别版的冒泡排序。最差情况下时间复杂度为O(N*N),平均复杂度为O(N*logN)。基于二分思想。下面public class Sort {
public class Sort {static int[] sortArray = new int[] {6,6,6,6,61,2,7,9,3,4,5,10,8 };static int n = sortArray.length;public static void main(String[] args) {sort(0, n-1);for (int i1 = 0; i1 < sortArray.length; i1++) {System.out.print(sortArray[i1] + " ");}}public static void sort(int left, int right) {int temp, i, j, t;if (left > right) {return;}temp = sortArray[left];i = left;j = right;while (i != j) {while (sortArray[j] >= temp && i < j)j--;while (sortArray[i] <= temp && i < j)i++;if (i < j) {t = sortArray[i];sortArray[i] = sortArray[j];sortArray[j] = t;}}sortArray[left] = sortArray[i];sortArray[i] = temp;sort(left, i - 1);sort(i + 1, right);}}

输出结果:2 3 4 5 6 6 6 6 7 8 9 10 61


0 0