Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Quick-Sort ) Popular Algorithms | Quick Sort | THE INQUISITIVE





Quick Sort

LEVEL:Beginner

Description

Given a list of integers and a integer (size of array). Sort the given array using Quick Sort Algorithm.

Input Format

First line contains the size of array (n) followed by n integers separated by spaces.

Output Format

Print the sorted array separated by spaces.


Example 1:

Input
5
5 4 3 2 1
Output
1 2 3 4 5
Example 2:

Input
6
96 54 100 32 45 64
Output
32 45 54 64 96 100
Example 3:

Input
7
69 95 34 67 45 62 44
Output
34 44 45 62 67 69 95

oops

Login to see Discussion




Approach

Step-i) Choose the highest index value has pivot
Step-ii) Take two variables to point left and right of the list excluding pivot, left points to the low index, right points to the high.
Step-iii) While value at left is less than pivot move right, while value at right is greater than pivot move left.
Step-iv) If both step 5 and step 6 does not match swap left and right.
Step-v) If left ≥ right, the point where they met is new pivot.

Time Complexity: O(n log n)
Space Complexity: O(log n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution