Quick Sort

You are given an array of integers. You need to sort the array in ascending order using quick sort.

Quick sort is a divide and conquer algorithm in which we choose a pivot point and partition the array into two parts i.e, left and right. The left part contains the numbers smaller than the pivot element and the right part contains the numbers larger than the pivot element. Then we recursively sort the left and right parts of the array.

Example:

Let the array = [ 4, 2, 1, 5, 3 ]
Let pivot to be the rightmost number.

example

After the 1st level partitioning the array will be { 2, 1, 3, 4, 5 } as 3 was the pivot. After 2nd level partitioning the array will be { 1, 2, 3, 4, 5 } as 1 was the pivot for the left part and 5 was the pivot for the right part. Now our array is sorted and there is no need to divide it again.

Input format:
The first line of input contains an integer 'T' denoting the number of queries or test cases. 

The first line of each input consists of an integer 'N' denoting the size of the array.

The second line of each input consists of 'N' space-separated integers denoting the elements of the array.
Output format:
For each test case, print a single line containing space-separated integers denoting the elements of the array after sorting.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Follow Up
Can you solve this in the worst case NlogN complexity?
Constraints:
1 <= T <= 10
1 <= N <= 10 ^ 3
-10 ^ 9 <= ARR[i] <= 10 ^ 9

Where 'T' is the number of test cases, 'N' is the length of the array 'ARR', and 'ARR[i]' is the array element at index i.

Time limit: 1 sec.
CodingNinjas
author
2y

Practise algorithms and coding

CodingNinjas
author
2y
Right most pivot

The first approach will be picking edge elements as pivot.

What needs to be done:

  • Pick the rightmost element as pivot.
  • Partition the array with numbers smaller than pivot on the left of p...read more
CodingNinjas
author
2y
Random pivot

The second approach will be picking a random element as pivot.

What needs to be done:

  • Pick the random element in the given range of array as pivot.
  • Partition the array with numbers smaller th...read more
CodingNinjas
author
2y
Median pivot

The third approach will be picking median as pivot and applying 3 - way quick sort.

What needs to be done:

  • Pick the median of the given range of array as pivot using an optimized quick selec...read more
Add answer anonymously...
Bank of America Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter