美文网首页
快速排序 Quick sort

快速排序 Quick sort

作者: PerfectStranger | 来源:发表于2017-06-19 23:14 被阅读0次

最差情况,序列刚好是有序的,复杂度为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

      本文链接:https://www.haomeiwen.com/subject/rxinqxtx.html