Quick sort using quick sort algorithm. 36,25,2,5,8,35,86,5,47,95


Q.) Quick sort using quick sort algorithm. 36,25,2,5,8,35,86,5,47,95

Subject: Data Structures

Quick sort is a divide-and-conquer algorithm that sorts items by partitioning an array into two smaller arrays. The steps for the quick sort algorithm are as follows:

  1. Choose a Pivot: Select an element from the array to serve as the pivot. The choice of pivot can affect the performance of the algorithm, but for simplicity, we often choose the last element, the first element, or a random element.

  2. Partitioning: Rearrange the array so that all elements less than the pivot come before the pivot, while all elements greater than the pivot come after it. The pivot will then be in its final sorted position.

  3. Recursively Apply: Recursively apply the above steps to the sub-array of elements with smaller values and the sub-array of elements with larger values.

  4. Base Case: The recursion stops when the sub-array has less than or equal to one element, as a single element is always sorted.

Let's sort the given array using the Quick Sort algorithm:

Given array: 36, 25, 2, 5, 8, 35, 86, 5, 47, 95

Step-by-Step Quick Sort:

Step 1: Choose a Pivot

Let's choose the last element as the pivot for simplicity.

Pivot: 95

Step 2: Partitioning

We will rearrange the array so that elements less than 95 are on the left, and elements greater than 95 are on the right.

Left Sub-array Pivot Right Sub-array
36, 25, 2, 5, 8, 35, 86, 5, 47 95 (empty)

Since 95 is the largest element, it is already in its correct position, and there are no elements in the right sub-array.

Step 3: Recursively Apply to Left Sub-array

Now we will sort the left sub-array: 36, 25, 2, 5, 8, 35, 86, 5, 47

Let's choose the last element as the pivot again.

Pivot: 47

Partitioning the left sub-array:

Left Sub-array Pivot Right Sub-array
36, 25, 2, 5, 8, 35, 5 47 86

Now, 47 is in its correct position.

Step 4: Recursively Apply to Sub-arrays

We now have two sub-arrays to sort: 36, 25, 2, 5, 8, 35, 5 and 86. Since 86 is a single element, it is already sorted.

Let's sort the sub-array: 36, 25, 2, 5, 8, 35, 5

Choose the last element as the pivot again.

Pivot: 5

Partitioning the sub-array:

Left Sub-array Pivot Right Sub-array
2 5 36, 25, 5, 8, 35

Now, 5 is in its correct position.

We continue this process recursively for each sub-array, choosing a pivot, partitioning, and then sorting the sub-arrays. The process is repeated until all sub-arrays contain a single element or are empty.

Here's a visualization of the process:

Initial Array: [36, 25, 2, 5, 8, 35, 86, 5, 47, 95]

Choose Pivot (95) and Partition:
[36, 25, 2, 5, 8, 35, 86, 5, 47] | [95]

Choose Pivot (47) and Partition:
[36, 25, 2, 5, 8, 35, 5] | [47] | [86, 95]

Choose Pivot (5) and Partition:
[2] | [5] | [36, 25, 5, 8, 35] | [47] | [86, 95]

Choose Pivot (35) and Partition:
[2, 5] | [25, 5, 8] | [35] | [36] | [47] | [86, 95]

Choose Pivot (8) and Partition:
[2, 5] | [5] | [8] | [25] | [35] | [36] | [47] | [86, 95]

Final Sorted Array:
[2, 5, 5, 8, 25, 35, 36, 47, 86, 95]

The final sorted array is [2, 5, 5, 8, 25, 35, 36, 47, 86, 95]. Each step involves choosing a pivot, partitioning the array, and then recursively sorting the sub-arrays until the entire array is sorted.