Explain Quick sort? Sort the following elements using Quick sort - 32, 23, 56, 78, 12, 66, 37, 93, 29, 80
Q.) Explain Quick sort? Sort the following elements using Quick sort - 32, 23, 56, 78, 12, 66, 37, 93, 29, 80
Subject: Data StructuresQuick sort is a highly efficient sorting algorithm and is based on the partitioning of an array of data into smaller arrays. A large array is partitioned into two arrays, one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.
Quick sort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst-case time complexity are O(nLogn) and image.pngO(n2), respectively.
Here is a step-by-step approach to sort the given elements using Quick sort:
Step 1: Choose the highest index value as pivot
Step 2: Partition the array using pivot value
Step 3: Quick sort the left partition recursively
Step 4: Quick sort the right partition recursively
Let's sort the given array - 32, 23, 56, 78, 12, 66, 37, 93, 29, 80
Step 1: Choose the highest index value as pivot
Here, 80 is the pivot.
Step 2: Partition the array using pivot value
After partitioning, the array becomes - 32, 23, 56, 78, 12, 66, 37, 29, 80, 93
Step 3: Quick sort the left partition recursively
After sorting the left partition, the array becomes - 12, 23, 29, 32, 37, 56, 66, 78, 80, 93
Step 4: Quick sort the right partition recursively
Since the right partition has only one element, it is already sorted.
So, the final sorted array is - 12, 23, 29, 32, 37, 56, 66, 78, 80, 93
The following table shows the process of Quick sort:
Step | Array |
---|---|
Initial Array | 32, 23, 56, 78, 12, 66, 37, 93, 29, 80 |
Choose Pivot | 80 |
After Partitioning | 32, 23, 56, 78, 12, 66, 37, 29, 80, 93 |
Sort Left Partition | 12, 23, 29, 32, 37, 56, 66, 78, 80, 93 |
Sort Right Partition | 12, 23, 29, 32, 37, 56, 66, 78, 80, 93 |
Final Sorted Array | 12, 23, 29, 32, 37, 56, 66, 78, 80, 93 |
In conclusion, Quick sort is a divide and conquer algorithm that is highly efficient for large-sized data sets. It works by partitioning an array into two smaller arrays around a pivot and then recursively sorting the two arrays.