0% found this document useful (0 votes)
70 views10 pages

373 Lecture 4

The document discusses the quicksort algorithm. Quicksort uses a divide and conquer approach to sort arrays. It works by partitioning the array into two subarrays such that elements in one subarray are less than the partition element and elements in the other subarray are greater than it. It then recursively sorts the two subarrays. The key part is the partition operation which rearranges the array elements, putting the partition element in its final position and organizing the rest of the array around it.

Uploaded by

api-3696125
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views10 pages

373 Lecture 4

The document discusses the quicksort algorithm. Quicksort uses a divide and conquer approach to sort arrays. It works by partitioning the array into two subarrays such that elements in one subarray are less than the partition element and elements in the other subarray are greater than it. It then recursively sorts the two subarrays. The key part is the partition operation which rearranges the array elements, putting the partition element in its final position and organizing the rest of the array around it.

Uploaded by

api-3696125
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

CSC 373

Lecture 4
By
Javed Siddique
Quick Sort
• Quicksort, like merge sort, is based on the divide-and-conquer paradigm introduced
• in Section 2.3.1. Here is the three-step divide-and-conquer process for sorting a
• typical subarray A[p . . r].
• Divide: Partition (rearrange) the array A[p . . r] into two (possibly empty) subarrays
• A[p . . q −1] and A[q +1 . . r] such that each element of A[p . . q −1] is
• less than or equal to A[q], which is, in turn, less than or equal to each element
• of A[q + 1 . . r]. Compute the index q as part of this partitioning procedure.
• Conquer: Sort the two subarrays A[p . . q−1] and A[q +1 . . r] by recursive calls
• to quicksort.
• Combine: Since the subarrays are sorted in place, no work is needed to combine
• them: the entire array A[p . . r] is now sorted.
Quick Sort
QUICKSORT(A, p, r)
1 if p < rs
2 then q ← PARTITION(A, p, r)
3 QUICKSORT(A, p, q − 1)
4 QUICKSORT(A, q + 1, r)
To sort an entire array A, the initial call is
QUICKSORT(A, 1, length[A]).
Quick Sort
The key to the algorithm is the PARTITION procedure,
which rearranges the subarray A[p . . r] in place.
PARTITION(A, p, r)
1 x ← A[r]
2i←p−1
3 for j ← p to r − 1
4 do if A[ j ] ≤ x
5 then i ←i + 1
6 exchange A[i ] ↔ A[ j ]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1
Balanced Partitioning
• T (n) ≤ T (9n/10) + T (n/10) + cn

You might also like