0% found this document useful (0 votes)
3 views22 pages

Week 11quick Sort Merge Sort

The document provides an overview of Quick Sort and Merge Sort algorithms, highlighting their divide and conquer strategies. Quick Sort partitions an array around a pivot and recursively sorts the resulting subarrays, while Merge Sort divides the array into halves, sorts them, and merges the sorted halves. Both algorithms are efficient for sorting large datasets, with Quick Sort having an average and worst-case complexity of O(n^2).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Week 11quick Sort Merge Sort

The document provides an overview of Quick Sort and Merge Sort algorithms, highlighting their divide and conquer strategies. Quick Sort partitions an array around a pivot and recursively sorts the resulting subarrays, while Merge Sort divides the array into halves, sorts them, and merges the sorted halves. Both algorithms are efficient for sorting large datasets, with Quick Sort having an average and worst-case complexity of O(n^2).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

CLO-1

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.

• A large array is partitioned into two arrays one of which holds


values smaller than the specified value, say pivot, based on
which the partition is made, and another array holds values
greater than the pivot value.

• 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.

• After partitioning an array, the algorithm calls itself recursively


twice to sort the two resulting subarrays.

• This algorithm is quite efficient for large-sized data sets as its


average and worst-case complexity are O(n2), respectively.

• The Quick Sort is divided into two steps


• Reduction step
• Quick Sort

4
Step by Step Process of Quick Sort

In Quick sort algorithm, partitioning of the list is performed using


following steps

Step 1 - Consider the first element of the list as pivot (i.e., Element

at first position in the list).


Step 2 - Define two variables i and j. Set i and j to first and last
elements of the list respectively.
Step 3 - Increment i until list[i] > pivot then stop.
Step 4 - Decrement j until list[j] < pivot then stop.
Step 5 - If i < j then exchange list[i] and list[j].
Step 6 - Repeat steps 3,4 & 5 until i > j.
Step 7 - Exchange the pivot element with list[j] 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

• In reduction step of the quick sort algorithm finds the final


position of one of the number. The first number is 44.

• 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

44>22 then Swap


6
Quick Sort Example

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

• Observe that the numbers 88 and 66 to the right of 44 are


greater than 44.
• Beginning with 22, next scan the list in the opposite direction,
from left to right.
• When compared, found 55 is greater than 44 then swap

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

55>44 then Swap

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

• Observe that the numbers 22, 33 and 11 to the left of 44 are


each less than 44.

• Beginning with 55, now scan the list in the original direction,
from right to left, until meeting the first number less than 44

• Swap 44 with 40 to obtain the list


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

• Again, the numbers to the right of 44 are greater


• Begin with 40, scan the list from left to right. The first number
greater than 44 is 77. Swap 44 with 77.

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

• Now, the numbers less than 44 form a sub-list of numbers to


the left of 44. And numbers greater than 44 form the sub-list
of numbers to the right of 44

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

First sub- Second sub-list


list

9
Quick Sort Example
22 33 11 40 44 90 77 60 99 55 88 66

First sub- Second sub-list


list
 The number 44 is correctly placed in its final position, and the task of sorting the
original list has now been reduced to the task of sorting each of the above sub-
lists.

 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.

 It is accomplished by using two STACKs, called LOWER and UPPER, to


temporarily hold such sub-lists.

 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

Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT

SET LEFT = LEFT + 1

[END OF LOOP]

Step 6:IF LOC = LEFT

SET FLAG = 1

ELSE IF ARR[LOC] < ARR[LEFT]

SWAP ARR[LOC] with ARR[LEFT]

SET LOC = LEFT

[END OF IF]

Step 7: [END OF LOOP]

Step 8: END

14
Quick Sort Algorithm
QUICK_SORT (ARR, BEG, END)

Step 1: IF (BEG < END)

CALL PARTITION (ARR, BEG, END, LOC)

CALL QUICKSORT(ARR, BEG, LOC - 1)

CALL QUICKSORT(ARR, LOC + 1, END)

[END OF IF]

Step 2: END

15
Quick sort complexity

16
Merge Sort

• Divide it in two at the midpoint


• Conquer each side in turn (by
recursively sorting)
• Merge two halves together

17
Merge Sort Example

18
Merge Sort Algorithm

MERGE_SORT(arr, beg, end)

if beg < end


set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if

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]

[Copy the remaining elements of left sub-array, if any]

ELSE

Repeat while I <= MID

SET TEMP[INDEX] = ARR[I]

SET INDEX = INDEX + 1, SET I = I + 1

[END OF LOOP]

[END OF IF]

Step 4: [Copy the contents of TEMP back to ARR] SET K = 0

Step 5: Repeat while K < INDEX

SET ARR[K] = TEMP[K]

SET K = K + 1

[END OF LOOP]

• Step 6: Exit

21
Merge sort complexity

22

You might also like