0% found this document useful (0 votes)
19 views

A (P - . Q 1) and A (Q +1 - . R) Such That Each Element of A (P - . Q 1) Is

Quicksort is a divide-and-conquer algorithm that works by partitioning an array around a pivot value and recursively sorting the subarrays. It divides the array into left and right subarrays based on element values, conquers the subarrays by recursively sorting them, and combines the sorted subarrays into a fully sorted array.

Uploaded by

Matthew Wagner
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

A (P - . Q 1) and A (Q +1 - . R) Such That Each Element of A (P - . Q 1) Is

Quicksort is a divide-and-conquer algorithm that works by partitioning an array around a pivot value and recursively sorting the subarrays. It divides the array into left and right subarrays based on element values, conquers the subarrays by recursively sorting them, and combines the sorted subarrays into a fully sorted array.

Uploaded by

Matthew Wagner
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Quicksort is based on the divide-and-conquer paradigm .

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 . . q1] 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. QUICKSORT(A, p, r) 1 if p < r 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]). Partitioning the array 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] 2ip1 3 for j p to r 1 4 5 6 8 return i + 1 do if A[ j ] x then i i + 1 exchange A[i ] A[ j ]

7 exchange A[i + 1] A[r]

The merge sort algorithm closely follows the divide-and-conquer paradigm. Intuitively, it operates as follows. Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted answer.

MERGE-SORT(A, p, r) 1 if p < r 2 3 4 5 then q _(p + r)/2_ MERGE-SORT(A, p, q) MERGE-SORT(A, q + 1, r) MERGE(A, p, q, r)

MERGE(A, p, q, r) 1 n1 q p + 1 2 n2 r q 3 create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1] 4 for i 1 to n1 5 do L[i ] A[p + i 1] do R[ j ] A[q + j ] 6 for j 1 to n2 7 8 L[n1 + 1] 9 R[n2 + 1] 10 i 1 11 j 1 12 for k p to r 13 14 15 16 17 do if L[i ] R[ j ] then A[k] L[i ] ii+1 else A[k] R[ j ] jj+1

You might also like