# 1 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Quick Sort Another method which is...

date post

17-Dec-2015Category

## Documents

view

213download

0

Embed Size (px)

### Transcript of 1 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Quick Sort Another method which is...

- Slide 1
- 1 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Quick Sort Another method which is different from Merge Sort in which, the divided sub files are not need to be merged later. This method uses a procedure known as PARTITION (C.A.R. Hoare).
- Slide 2
- 2 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Partitioning To partition data is to divide it into two groups, so that all the items with a key value higher than a specified value are in one group, and all the item with a lower key value are in another. Data could be partitioned more than 2-ways The value used to determine in which of two groups an item is placed is called a pivot value
- Slide 3
- 3 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Basically the quicksort algorithm operates by partitioning an array into two subarrays, and then calling it self to quicksort each of these subarrays. Algorithm Partition the array into left and right subarrays Call itself to sort left subarray Call itself to sort right subarray If array has only one element it is sorted How is the pivot selected
- Slide 4
- 4 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Global n: integer; A:array [1..n] of items; procedure PARTITION(m:integer {index}, var p:integer{index+1}); {within A[m], A[m+1],..., A[p-1] the elements are rearranged in such a way that if initially t = A[m], then after completion A[q] = t, for some q between m and p-1, final value of p is q} var i: integer; v: item; begin v := A[m]; i:= m; {A[m] is a partition element} while (i < p) do
- Slide 5
- 5 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer begin i := i+1; while(A[i] < v) do i := i+1; {i moves left to right} p := p-1; while(A[p] > v) do p := p-1; {p moves right to left} if (i < p) then swap(A[i], A[p]); end A[m] := A[p]; A[p] := v; {the partition element belongs at p} end.
- Slide 6
- 6 Algorithms: Rajeev Wankar Chapter 3: Divide and Conquer Global n: integer; A:array [1..n] of items; procedure QUICKSORT(p, q:integer); {sort elements A[p]..A[q] which resides in the A[1..n], in ascending order. A[n+1] >= all elements in A[p..q] and set to infinity} begin if (p < q) then begin j := q+1; PARTITION(p,j); QUICKSORT(p, j-1); QUICKSORT(j+1, q); end end.

Recommended

*View more*