Sort the following keyword A,L,G,O,R,I,T,H,M by applying Quicksort method

Sort the following keyword A,L,G,O,R,I,T,H,M by applying Quicksort method

Answer:-

  1. Choose a pivot element: We’ll choose the last element as the pivot, which is “M.”
  2. 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, L, G, O, R, I, T, H | M
  1. Recursively sort subarrays:
  • Apply the same process to the subarrays on the left and right of the pivot.
  • Sort the left subarray:
   A, L, G, O, R, I, T | H | M
  • Choose the last element “H” as the pivot for the left subarray.
  • Partition the left subarray: A, G | H | L, O, R, I, T | M
  • Sort the left sub-subarray:
   A | G | H | L, O, R, I | T | M

Sort the right sub-subarray:

   A | G | H | I, L, O, R | T | M
  1. Merge the sorted subarrays:
  • The entire array is now sorted.
  • Merge the subarrays:
   A, G, H, I, L, M, O, R, T

The keywords are now sorted in alphabetical order using the Quick Sort method:

A, G, H, I, L, M, O, R, T

Leave a Reply

Your email address will not be published. Required fields are marked *