Week 11quick Sort Merge Sort
Week 11quick Sort Merge Sort
Week#11
Quick Sort & Merge Sort
Course: Data Structures and Algorithm (CE-205)
Course Teacher: Ms. Aneeta Siddiqui
Contact Info:
Room No: BS-03
Email: [email protected]
1
“Divide and Conquer”
• Very important strategy in computer
science:
Divide problem into smaller parts
Independently solve the parts
Combine these solutions to get overall
solution
• Idea 1 : Partition array into items that
are “small” and items that are “large”,
then recursively sort the two sets
Quicksort
• Idea 2: Divide array into two halves,
recursively sort left and right halves,
then merge two halves Mergesort
2
Quick Sort
• It is a highly efficient sorting algorithm and is based on
partitioning of array of data into smaller arrays.
• The pivot value divides the list into two parts. And recursively,
we find the pivot for each sub-lists until all lists contains only
one element.
3
Quick Sort
• QuickSort is a Divide and Conquer algorithm.
• The problem of sorting a set is reduced to the problem of
sorting two smaller sets.
4
Step by Step Process of Quick Sort
Step 1 - Consider the first element of the list as pivot (i.e., Element
5
Quick Sort Example (Reduction)
• Suppose A is the following list of 12 numbers
44 33 11 55 77 90 40 60 99 22 88 66
• Beginning with the last number 66, scan the list from right to
left, comparing each number with 44.
• Stop at the first number less than 44. The number is 22,
interchange 44 and 22 (swap)
44 33 11 55 77 90 40 60 99 22 88 66
22 33 11 55 77 90 40 60 99 44 88 66
22 33 11 55 77 90 40 60 99 44 88 66
22 33 11 44 77 90 40 60 99 55 88 66
Swap 55 with 44
7
Quick Sort Example
22 33 11 44 77 90 40 60 99 55 88 66
• Beginning with 55, now scan the list in the original direction,
from right to left, until meeting the first number less than 44
8
Quick Sort Example
22 33 11 40 44 90 77 60 99 55 88 66
• Again, the number to the left of 44 are each less than 44.
• Beginning with 77, scan the list from right to left, seeking a
number less than 44.
• No number is found, this means all numbers have been
scanned and compared with 44
22 33 11 40 44 90 77 60 99 55 88 66
9
Quick Sort Example
22 33 11 40 44 90 77 60 99 55 88 66
The above reduction step is repeated with each sub-lists containing 2 or more
elements.
Since we can process only one list at a time, we must be able to keep track of
some sub-lists for future processing.
The addresses of first and last elements of each sub-list, called its boundary
value, are pushed into the stacks LOWER and UPPER
The reduction step is applied to a sub-list only after its boundary values are
removed from the stacks, 10
Example 6.8
44 33 11 55 77 90 40 60 99 22 88 66
22 33 11 40 44 90 77 60 99 55 88 66
11
Example 6.8
12
Quick Sort Algorithm
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG,
FLAG
Step 2: Repeat Steps 3 to 6 while FLAG != 1
Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]
AND LOC != RIGHT
SET RIGHT = RIGHT – 1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
13
Quick Sort Algorithm
Step 5: IF FLAG = 0
[END OF LOOP]
SET FLAG = 1
[END OF IF]
Step 8: END
14
Quick Sort Algorithm
QUICK_SORT (ARR, BEG, END)
[END OF IF]
Step 2: END
15
Quick sort complexity
16
Merge Sort
17
Merge Sort Example
18
Merge Sort Algorithm
END MERGE_SORT
19
Merge Sort Algorithm
Step 1: [INITIALIZE] SET I = BEG, J = MID + 1, INDEX = 0
Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J]
SET TEMP[INDEX] = ARR[I]
SET I = I + 1
ELSE
SET TEMP[INDEX] = ARR[J]
SET J = J + 1
[END OF IF]
SET INDEX = INDEX + 1
[END OF LOOP]
Step 3: [Copy the remaining elements of right sub-array, if any]
IF I > MID
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
20
Merge Sort Algorithm
SET INDEX = INDEX + 1, SET J = J + 1
[END OF LOOP]
ELSE
[END OF LOOP]
[END OF IF]
SET K = K + 1
[END OF LOOP]
• Step 6: Exit
21
Merge sort complexity
22