快速排序 Quick sort

最差情况,序列刚好是有序的,复杂度为O(n^2)
最好情况,每次基准刚好是中间值,复杂度为O(nlogn)
平均复杂度为:O(nlogn) 不稳定的排序算法
由于递归,需要消耗运行时栈的空间

static int partition(int[] unsorted, int low, int high) {
            int pivot = unsorted[low];
            while (low < high) {
                while (low < high && unsorted[high] > pivot) high--;
                unsorted[low] = unsorted[high];
                while (low < high && unsorted[low] <= pivot) low++;
                unsorted[high] = unsorted[low];
            }
            unsorted[low] = pivot;
            return low;
        }

        static void quick_sort(int[] unsorted, int low, int high) {
            int loc = 0;
            if (low < high) {
                loc = partition(unsorted, low, high);
                quick_sort(unsorted, low, loc - 1);
                quick_sort(unsorted, loc + 1, high);
            }
        }

你可能感兴趣的:(快速排序 Quick sort)