Quick Sort Algorithm Problem Statement: Given an array of n integers, sort the array using the Quicksort method. Examples: Example 1: Input: N = 5 , Arr[] = {4,1,7,9,3} Output: 1 3 4 7 9 Explanation: After sorting the array becomes 1, 3, 4, 7, 9 Example 2: Input: N = 8 , Arr[] = {4,6,2,5,7,9,1,3} Output: 1 2 3 4 5 6 7 9 Explanation: After sorting the array becomes 1, 3, 4, 7, 9 Solution Disclaimer : Don’t jump directly to the solution, try it out yourself first. Problem Link . Intuition: Quick Sort is a divide-and-conquer algorithm like the Merge Sort . But unlike Merge sort, this algorithm does not use any extra array for sorting(though it uses an auxiliary stack space). So, from that perspective, Quick sort is slightly better than Merge sort. This algorithm is basically a repetition of two simple steps that are the following: Pick a pivot and place it in its correct place in the sorted array. Shift smaller elements(i.e. Smaller ...
Comments
Post a Comment