Sort the following integer using quick sort. 95, 55, 42, 36, 12, 18, 48, 65, 72, 82


Q.) Sort the following integer using quick sort. 95, 55, 42, 36, 12, 18, 48, 65, 72, 82

Subject: data structures

Quick Sort Algorithm:

Quick sort is a divide-and-conquer sorting algorithm that works by recursively partitioning the list into smaller sublists, sorting those sublists, and then concatenating them back together.

Steps:

  1. Select a pivot element:

    • Choose a pivot element from the list.
    • In this example, let's choose the middle element, which is 48.
  2. Partition the list:

    • Rearrange the list so that all elements less than the pivot are on the left side of the pivot, and all elements greater than or equal to the pivot are on the right side of the pivot.
    • This can be done using two pointers, one starting from the left and one starting from the right, and moving them towards each other until they meet.
  3. Recursively apply the algorithm to the sublists:

    • Apply the quick sort algorithm to the two sublists created in step 2.

Applying Quick Sort to the Given List:

  1. Select the pivot element:

    • Pivot = 48
  2. Partition the list:

    • [95, 55, 42, 36, | 48 |, 65, 72, 82, 12, 18]
  3. Recursively apply the algorithm to the sublists:

    • Left sublist: [95, 55, 42, 36]
    • Right sublist: [65, 72, 82, 12, 18]
  4. Repeat steps 2 and 3 until the sublists are sorted:

    • Left sublist: [36, 42, 55, 95]
    • Right sublist: [12, 18, 65, 72, 82]
  5. Concatenate the sorted sublists:

    • [36, 42, 55, 95, 12, 18, 65, 72, 82]

Final Sorted List: 95, 55, 42, 36, 12, 18, 48, 65, 72, 82

Complexity:

  • Average-case time complexity: O(n log n)
  • Worst-case time complexity: O(n^2)
  • Space complexity: O(log n)