1. Quicksort is honored as one of top 10 algorithms of 20th century in science and engineering. It's prevalent in practice, takes O(nlogn) time "on average" and works in place.
2. Partition around a pivot :
a) pick an element of the array
b) rearrage the array so that:
- those elements left to pivot are less than pivot
- those elements right to pivot are greater than pivot
result : the pivot is put in its "rightful position"
cool facts: linear (O(n)) time, no extra memory & it reduces problem size
3. Quick Sort High-Level Description: QuickSort(array A, length n)
a) if n=1, return
b) p = choosePivot(A, n)
c) partition A around p
d) recursively sort 1st part
e) recursively sort 2nd part
4. Algorithm of partition:
1) The easy way out is to scan the array put all those elements greater than pivot to the right most available position of a new array and all those elements less than pivot to the left most available position of the new array.
2) In-Place Implementation :
a) pivot = the 1st element of the array ( if not, just swap the pivot with the 1st element)
b) maintain the following invariant :
5. In-Place Partition Algorithm: Partition( A, l, r) ( input = A[l...r] )
a) p = A[l];
b) i = l + 1;
c) for j = l + 1 to r :
if ( A[j] < p )
-- swap A[i] and A[j]
-- i ++
d) swap A[l] and A[i -1]
e) return i-1
Running Time is O(n) , where n = r-l + 1 is the length of the subarray.
Correctness : the loop maintain the invariant :
a) A[l+1], ... , A[i-1] are all less than pivot
b) A[i] , ... , A[j-1] are all greater than pivot
c) A[j] is not checked
6. Induction Review :
Let P(n) = Assertion parameterized by positive integer n.
To prove P(n) for all n>= 1 by induction:
a) base case : directly prove that P(1) holds
b) inductive step: for every n >=2, prove that : if P(k) holds for all k<n , then P(n) holds as well.
7. Correctness of Quick Sort:
P(n) = "Quick Sort correctly sorts every input array of length n for every n >=1".
Proof by induction:
a) base case : every input array of length 1 is already sorted. Quick Sort returns the input array, which is correct. (P(1) holds)
b) inductive step : Fix n >= 2, Fix some input array A of length n , if P(k) holds for any k < n:
because pivot winds up in correct position, the pivot is greater than left subarray and less than right subarray. while the sizes of left subarray and right subarray are less than n, by induction afer recursive calls , they are sorted. So the entire array of length n is sorted.
相关推荐
Use QuickSort algorithm to sort the array that has n elements that are constructed by the random() function. Requirements: The template should be used for all kinds of data type, such as: integer, ...
快速排序(Quick Sort)是由C.A.R. Hoare在1960年提出的,它是一种非常高效的排序算法,其基本思想是分治法。快速排序的基本步骤如下: 1. **选择枢轴元素**:在待排序的数组中选取一个元素作为枢轴,通常选择第一...
快速排序(Quick Sort)是一种高效的排序算法,由C.A.R. Hoare在1960年提出。其基本思想是采用分治法(Divide and Conquer),通过一趟排序将待排记录分割成独立的两部分,其中一部分的所有记录都比另一部分的所有...
在这个名为"Quick sort Analysis.zip"的压缩包中,重点是分析快速排序的确定性与随机化实现。确定性快速排序通常是指每次选取固定的基准元素,如选择第一个或最后一个元素,这样对于相同的输入,排序过程完全可预测...
快速排序(Quick Sort)是一种高效的排序算法,由C. A. R. Hoare在1960年提出。它采用分治法(Divide and Conquer)的策略来把一个序列分为较小和较大的两个子序列,然后递归地排序两个子序列。快速排序在实现时,...
数据结构,排序算法,快速排序算法的C语言实现, quick sort C qsort.c an c implementation of quick sort
快速排序算法(Quick Sort)是一种高效的排序算法,由计算机科学家托尼·霍尔(Tony Hoare)在1960年提出。它的基本思想是分治法(Divide and Conquer),通过一个基准值(pivot)将数组分为两部分,其中一部分的...
python编写 快速排序 Quick Sort
各种数据结构、算法及实用的C#源代码.C#,单向链表(Simply Linked List)快速排序(Quick Sort)算法与源代码.单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部...
C#,双向链表(Doubly Linked List)快速排序(Quick Sort)算法与源代码。双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始...
快速排序(Quick Sort)是一种高效的排序算法,由C.A.R. Hoare在1960年提出。它的基本思想是采用分治法(Divide and Conquer),将一个大问题分解为两个或多个相同或相似的子问题,直到最后子问题可以简单的直接求解...
python 一行代码实现的快速排序 quick sort
### 快速排序(Quick Sort) #### 算法原理 快速排序是一种高效的排序算法,其基本思想是采用分治法(divide and conquer)来解决问题。对于待排序的数组A[0]...A[N-1],快速排序通过选择一个基准元素(pivot),通常...
快速排序(Quick Sort)源码及运行示例
快速排序(Quick Sort)作者原版论文,快速排序的作者C.A.R Hoare 发表的原著论文。
算法分析与设计教学课件:Chapter 7 Quick Sort.pptx
快速排序是一种高效的排序算法,由英国计算机科学家C.A.R. Hoare在1960年提出。它的基本思想是分治法(Divide and Conquer)。在这个C++实现的快速排序中,我们将深入理解其原理、步骤以及如何用C++语言进行编码。...
private static void sort(Comparable[] a, Comparable[] aux, int l, int r) { if (r ) return; int m = l + (r - l) / 2; sort(a, aux, l, m); // 排序左半部分 sort(a, aux, m, r); // 排序右半部分 ...
return quick_sort(left) + middle + quick_sort(right) # 示例 arr = [3, 6, 8, 10, 1, 2, 1] print(quick_sort(arr)) # 输出: [1, 1, 2, 3, 6, 8, 10] ``` 6. **时间复杂度与空间复杂度**: - **时间复杂度**...