Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
50% found this document useful (2 votes)
373 views

Quick Sort: - It Is An Algorithm of Divide and Conquer

Quick Sort is an algorithm that uses divide and conquer to sort a list by picking a pivot element and partitioning the list into two sublists based on whether elements are less than or greater than the pivot, and then recursively applying the same approach to the sublists; the algorithm involves pushing boundary values of sublists onto a stack and popping them off to recursively apply the partitioning.

Uploaded by

Kumar Vidya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
373 views

Quick Sort: - It Is An Algorithm of Divide and Conquer

Quick Sort is an algorithm that uses divide and conquer to sort a list by picking a pivot element and partitioning the list into two sublists based on whether elements are less than or greater than the pivot, and then recursively applying the same approach to the sublists; the algorithm involves pushing boundary values of sublists onto a stack and popping them off to recursively apply the partitioning.

Uploaded by

Kumar Vidya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Quick Sort

• It is an algorithm of divide and conquer.


44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66

• After reduction step, final position of no. is:


22, 33, 11, 40, 44, 90, 77, 60, 99, 55, 88, 66
First sublist Second sublist
• The algorithm begins by pushing boundary values 1 and 12 onto stack to
yield
Lower: 1 Upper: 12
• To apply reduction step, top values are removed from stack, leaving
Lower: (empty) Upper: (empty)
• And then reduction step is applied to list from A[1] to A[12]. Finally 44
reaches its final location A[5].
• Accordingly, algo. Pushes boundary values 1 and 4 to 1 st sublist and 6 and
12 to 2nd sublist onto stack, leaving
Lower: 1, 6 Upper: 4, 12
• Now, apply reduction step again by removing top values from stack,
leaving
Lower: 1 Upper: 4
• Then reduction step is applied to sublist A[6] to A[12], and so on.
Lower: 1, 6 Upper: 4, 10
Quick sort algorithm
QUICK(A, N, BEG, END, LOC)
1. [Initialize] Set LEFT = BEG, RIGHT=END, LOC=BEG
2. [Scan from right to left]
a. Repeat while A[LOC] <=A[RIGHT] and LOC != RIGHT
i. RIGHT = RIGHT-1
b. If LOC = RIGHT then return
c. If A[LOC] > A[RIGHT] then
i. [Interchange A[LOC] and A[RIGHT] ] Temp = A[LOC], A[LOC] =
A[RIGHT], A[RIGHT] = Temp
ii. Set LOC = RIGHT
iii. Go to step 3.
3. [Scan from left to right]
a. Repeat while A[LEFT] <=A[LOC] and LEFT != LOC
i. LEFT = LEFT+1
b. If LOC = LEFT then return
c. If A[LEFT] > A[LOC] then
i. [Interchange A[LEFT] and A[LOC] ] Temp = A[LOC], A[LOC] = A[LEFT],
A[LEFT] = Temp
ii. Set LOC = LEFT
iii. Go to step 2.
Quick sort algorithm
QUICKSORT()
1. [Initialize] TOP = NULL
2. [Push boundary value of A onto stacks when A has 2 or more elements]
If N >1 then TOP = TOP + 1, LOWER[1] = 1, UPPER[1] = N
3. Repeat steps 4 to 7 while TOP != NULL
4. [Pop sublist from stacks]
Set BEG = LOWER[TOP] , END = UPPER[TOP]
TOP = TOP – 1
5. Call QUICK(A, N, BEG, END, LOC)
6. [ Push left sublist onto stacks when it has 2 or more elements]
If BEG < LOC-1 then
TOP = TOP + 1, LOWER[TOP] = BEG, UPPER[TOP] = LOC-1
7. [ Push right sublist onto stacks when it has 2 or more elements]
If LOC+1 < END then
TOP = TOP+1, LOWER[TOP] = LOC + 1, UPPER[TOP] = END
8. Exit.
Tower of Hanoi
Algorithm
Tower (N, Beg, Aux, End)

1. If N = 1, then
a) Write BegEnd
b) Return

2. // [Move N-1 disks from Beg to Aux]


Call Tower (N-1, Beg, End, Aux)

3. Write BegEnd

4. // [Move N-1 disks from Aux to End]


Call Tower (N-1, Aux, Beg, End)

7. Return

You might also like