Apply quick sort algorithm to sort the list E,X,A,M,P,L,E in alphabetical order. Draw tree of recursive calls made
Answer:-
The Quick Sort algorithm works by choosing a pivot element and partitioning the array such that all elements less than the pivot are on the left, and all elements greater than the pivot are on the right. Then, the algorithm is applied recursively to the subarrays on the left and right of the pivot.
Let’s proceed step by step:
- Choose a pivot element: We’ll choose the last element as the pivot, which is “E.”
- Partition the array:
- Rearrange the elements such that all elements less than the pivot are on the left, and all elements greater than the pivot are on the right.
A, E, M, L, E | P | X
- Recursively sort subarrays:
- Apply the same process to the subarrays on the left and right of the pivot.
- Sort the left subarray:
A, E | E | M, L | P | X
- Choose the last element “L” as the pivot for the left subarray.
- Partition the left subarray:
A | E | L | M | P | X
- Sort the left sub-subarray:
A | E | L | M | P | X
Sort the right sub-subarray:
A | E | L | M | P | X
- Merge the sorted subarrays:
- The entire array is now sorted.
- Merge the subarrays:
A, E, E, L, M, P, X
Here’s the tree representation of the recursive calls made during the Quick Sort process:
E / \ A M / \ L P / X
The nodes represent the pivot elements chosen during each partitioning step, and the subtrees represent the recursive calls on the left and right subarrays.