Sort using quick sort algorithm. क्विक सॉर्ट एल्गोरिथ्म का उपयोग करके छाँटें। 36,25,2,5,8,65,38,47,95


Q.) Sort using quick sort algorithm. क्विक सॉर्ट एल्गोरिथ्म का उपयोग करके छाँटें। 36,25,2,5,8,65,38,47,95

Subject: Data Structures

Quick Sort is a divide-and-conquer algorithm that sorts an array by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. This can be done in-place, requiring small additional amounts of memory to perform the sorting.

Here is a step-by-step approach to sort the given array using the Quick Sort algorithm:

Step 1: Choose a Pivot

The first step in the Quick Sort algorithm is to choose a pivot element. There are multiple strategies for choosing a pivot, such as picking the first element, the last element, the middle element, or a random element. For simplicity, we will choose the last element as the pivot in our example.

Step 2: Partitioning

Rearrange the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position.

Step 3: Recursively Apply

Recursively apply the above steps to the sub-array of elements with smaller values and the sub-array of elements with greater values.

Let's sort the given array: 36, 25, 2, 5, 8, 65, 38, 47, 95

Initial Array

Index 0 1 2 3 4 5 6 7 8
Value 36 25 2 5 8 65 38 47 95

First Iteration (Choosing pivot as 95)

Partition the array around the pivot (95). Since 95 is the largest element, it is already in its correct position, and all other elements are to its left.

Second Iteration (Choosing pivot as 47)

Now we ignore the last element (95) and choose the next pivot (47). Partition the array around the pivot (47).

Index 0 1 2 3 4 5 6
Value 36 25 2 5 8 65 38

After partitioning, we get:

Index 0 1 2 3 4 5 6 7 8
Value 36 25 2 5 8 38 47 65 95

Third Iteration (Choosing pivot as 38)

Now we ignore elements greater than 38 and choose the next pivot (38). Partition the array around the pivot (38).

Index 0 1 2 3 4
Value 36 25 2 5 8

After partitioning, we get:

Index 0 1 2 3 4 5 6 7 8
Value 25 2 5 8 36 38 47 65 95

Fourth Iteration (Choosing pivot as 36)

Now we ignore elements greater than 36 and choose the next pivot (36). Partition the array around the pivot (36).

Index 0 1 2 3
Value 25 2 5 8

After partitioning, we get:

Index 0 1 2 3 4 5 6 7 8
Value 2 5 8 25 36 38 47 65 95

Fifth Iteration (Choosing pivot as 25)

Now we ignore elements greater than 25 and choose the next pivot (25). Partition the array around the pivot (25).

Index 0 1 2
Value 2 5 8

After partitioning, we get:

Index 0 1 2 3 4 5 6 7 8
Value 2 5 8 25 36 38 47 65 95

Sixth Iteration (Choosing pivot as 8)

Now we ignore elements greater than 8 and choose the next pivot (8). Partition the array around the pivot (8).

Index 0 1
Value 2 5

After partitioning, we get:

Index 0 1 2 3 4 5 6 7 8
Value 2 5 8 25 36 38 47 65 95

Seventh Iteration (Choosing pivot as 5)

Now we ignore elements greater than 5 and choose the next pivot (5). Partition the array around the pivot (5).

Index 0
Value 2

After partitioning, we get:

Index 0 1 2 3 4 5 6 7 8
Value 2 5 8 25 36 38 47 65 95

Eighth Iteration (Choosing pivot as 2)

Now we ignore elements greater than 2 and choose the next pivot (2). Since 2 is the smallest element, it is already in its correct position.

Finally, the array is sorted:

Index 0 1 2 3 4 5 6 7 8
Value 2 5 8 25 36 38 47 65 95

This completes the Quick Sort process, and the array is now sorted in ascending order.