Data Sturcture and Algorithm Week 11
Data Sturcture and Algorithm Week 11
Semester – II Semester
EA2331201010152
1. Sort the following elements using quick sort algorithm.
44 33 11 55 77 90 40 60 99 22 88
Ans. Sort the elements using the Quick Sort algorithm and explain the process in
detail:
Elements to Sort:
44 33 11 55 77 90 40 60 99 22 88
Quick Sort Algorithm
Quick Sort is a divide-and-conquer sorting algorithm that works by recursively
partitioning the unsorted list around a chosen element (pivot). It then sorts the sub-
lists on either side of the partition.
Steps:
1. Choose a Pivot Element:
o We can choose any element from the list as the pivot. Here, let's select
the first element, 44.
2. Partitioning:
o Create two sub-lists: a left sub-list containing elements less than the
pivot and a right sub-list containing elements greater than or equal to the
pivot.
o Iterate through the remaining elements (excluding the pivot):
▪ If the current element is less than the pivot, add it to the left sub-
list.
▪ If the current element is greater than or equal to the pivot, add it to
the right sub-list.
Partitioning Illustration:
Original: 44 33 11 55 77 90 40 60 99 22 88
^ (pivot)
Left: 11 33 22 40 (elements less than 44)
Right: 55 77 90 60 99 88 (elements greater than or equal to 44)
3. Recursive Sorting:
o Recursively sort the left sub-list using Quick Sort.
o Recursively sort the right sub-list using Quick Sort.
Recursive Sorting Illustration:
44
/ \
Left Right
(sorted) (sorted)
Left: 11 22 33 40
Right: 55 60 77 88 90 99
4. Combine Sub-lists:
o Combine the sorted left sub-list, the pivot element, and the sorted right
sub-list to form the final sorted list.
Combining Sub-lists Illustration:
11 22 33 40 | 44 | 55 60 77 88 90 99 (final sorted list)
EA2331201010152
Complete Sorting Process:
1. Partitioning around the pivot (44) creates the left and right sub-lists.
2. Recursively sort the left sub-list (11, 22, 33, 40).
3. Recursively sort the right sub-list (55, 60, 77, 88, 90, 99).
4. Combine the sorted sub-lists and the pivot element to get the final sorted list:
11 22 33 40 44 55 60 77 88 90 99
Explanation in under 600 words:
Quick Sort is an efficient sorting algorithm with an average time complexity of O(n
log n). It works by dividing the unsorted list recursively around a pivot element,
creating sub-lists, sorting them independently, and then combining them into the
final sorted list.
The key steps involve:
• Choosing a pivot element (any element can be chosen).
• Partitioning the list into sub-lists using the pivot: elements less than the pivot
go to the left, and elements greater than or equal to the pivot go to the right.
• Recursively sorting the left and right sub-lists.
• Combining the sorted sub-lists and the pivot to form the final sorted list.
EA2331201010152