Quick Sort | Practice | GeeksforGeeks

Quick Sort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot.
Given an array arr[], its starting position low and its ending position high.
Implement the partition() and quickSort() functions to sort the array.

Example 1:
Input: 
N = 5 
arr[] = { 4, 1, 3, 9, 7}
Output:
1 3 4 7 9
Example 2:
Input: 
N = 9
arr[] = { 2, 1, 6, 10, 4, 1, 3, 9, 7}
Output:
1 1 2 3 4 6 7 9 10

Your Task: 
You don't need to read input or print anything. Your task is to complete the functions partition()  and quickSort() which takes the array arr[], low and high as input parameters and partitions the array. Consider the last element as the pivot such that all the elements less than(or equal to) the pivot lie before it and the elements greater than it lie after the pivot.

Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 103
1 <= arr[i] <= 104
0
arpita biswal4 days ago
class Solution{   partition(arr, low, high){       // Your code here       let pivot = arr[low];       let s=low, e= high;       while(s<e){           while(arr[s]<=pivot){               s++;           }           while(arr[e]>pivot){               e--;           }           if(s<e){               this.swap(arr,s,e);           }       }       this.swap(arr,low,e);       return e;          }   swap(arr,i,j){     let temp=arr[i];     arr[i]=arr[j];     arr[j]=temp;   }      quickSort(arr, low, high){       //code here       if(low < high){           let loc = this.partition(arr,low,high);           this.quickSort(arr,low,loc-1);           this.quickSort(arr,loc+1,high);       }   }   }
0
prab010420001 week ago
//  Java solution
class Solution{   //Function to sort an array using quick sort algorithm.   static void quickSort(int arr[], int low, int high)   {       // code here       if(low<high)       {           int p=partition(arr,low,high);           quickSort(arr,low,p-1);           quickSort(arr,p+1,high);                  }   }   static int partition(int arr[], int low, int high)   {       // your code here       int i=low-1;       int pivot=arr[high];       for(int j=low;j<=high-1;j++)       {           if(arr[j]<=pivot)           {               i++;               int temp=arr[i];               arr[i]=arr[j];               arr[j]=temp;           }       }       int temp=arr[i+1];       arr[i+1]=arr[high];       arr[high]=temp;       return i+1;   } } 
0
viveksharma731 week ago
class Solution{   public:         void swap(int arr[],int i,int j){       int temp =arr[i];       arr[i] = arr[j];       arr[j]= temp;          }         //Function to sort an array using quick sort algorithm.   void quickSort(int arr[], int low, int high)   {       // code here       if(low<high){          int pi= partition(arr,low,high);          quickSort(arr,low,pi-1);          quickSort(arr,pi+1,high);       }                 }                 public:   int partition (int arr[], int low, int high)   {      // Your code here                  int pivot=arr[high];      int i=low-1;            for(int j=low;j<high;j++){          if(arr[j]<pivot){              i++;              swap(arr,i,j);          }      }                  swap(arr,i+1,high);      return i+1;                                                                                 }
 
0
indraneelghosh7401 week ago
this is my code when I put 1 2 in custom input it runs but it shows error with input 1 2 after submitting
 
public:   //Function to sort an array using quick sort algorithm.   void quickSort(int arr[], int low, int high)   {       if(low<high){       int pi=partition(arr,low,high);       quickSort(arr,low,pi-1);       quickSort(arr,pi+1,high);   }   }   public:   int partition (int arr[], int low, int high)   {      int p=arr[low];      int i=low,j=high;     while(i<j){         while(arr[i]<=p)         i++;         while(arr[j]>=p)         j--;         if(i<j){         swap(arr[i],arr[j]);         }     }     swap(arr[low],arr[j]);      return j;   }
0
irmanivaibhav2 weeks ago
class Solution:
    def quickSort(self,arr,low,high):
        # code here
        if low<high:
            pi=self.partition(arr,low,high)
            self.quickSort(arr,low,pi-1)
            self.quickSort(arr,pi+1,high)
    
    def partition(self,arr,low,high):
        # code here
        i=low
        j=high-1
        pivot=arr[high]
        while i<j:
            while i<high and arr[i]<pivot:
                i+=1
            while j>low and arr[j]>=pivot:
                j-=1
            if i<j:
                arr[i],arr[j]=arr[j],arr[i]
        if arr[i]>pivot:
            arr[i],arr[high]=arr[high],arr[i]
        return i
    
0
vivekfaujdar482 weeks ago
JAVA CODE :
static void quickSort(int arr[], int low, int high)   {       if(low<high){       int pivot=partition(arr,low,high);              quickSort(arr,low,pivot-1);       quickSort(arr,pivot+1,high);       }   }   static int partition(int arr[], int low, int high)   {       // your code here       Solution obj=new Solution();       int pivot =arr[low];       int i=low;       int j=high;              while(i<j){           while(arr[i]<=pivot && i<j) i++;           while(arr[j]>pivot) j--;                      if(i<j) obj.swap(arr,i,j);                  }       obj.swap(arr,low,j);       return j;   }    public void swap(int a[],int i,int j){       int temp=a[i];       a[i]=a[j];       a[j]=temp;   }
0
himanshu567843 weeks ago
//Function to sort an array using quick sort algorithm.   static void quickSort(int arr[], int low, int high)   {   if(high<=low)return;   int p=partition(arr,low,high);   quickSort(arr,low,p-1);   quickSort(arr,p+1,high);   }   static int partition(int x[], int low, int high)   {   int i;   int pi=x[high],p=i=low;   for(;i<=high;i++)   {   if(x[i]<pi)   {    int t=x[p];    x[p]=x[i];    x[i]=t;    p++;   }   }   x[high]=x[p];   x[p]=pi;   return p;   // code here   }
0
surajdas19073 weeks ago
class Solution
{
    public:
    int swap(int arr[],int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    
    //Function to sort an array using quick sort algorithm.
    void quickSort(int arr[], int low, int high)
    {
        if(low<high){
        int pi=partition(arr,low,high);
        quickSort(arr,low,pi-1);
        quickSort(arr,pi+1,high);
        }
    }
    
    public:
    int partition (int arr[], int low, int high)
    {
       // Your code here
       int pivot=arr[high];
       int i=low-1;
       for(int j=low;j<high;j++){
           if(arr[j]<pivot){
               i++;
               swap(arr,i,j);
           }
       }
       swap(arr,i+1,high);
       return i+1;
    }
};
0
tarun119043123 weeks ago
Hoare’s Partition Scheme:
class Solution
{
    public:
    //Function to sort an array using quick sort algorithm.
    void quickSort(int arr[], int low, int high)
    {
        if(low < high){
            int p= partition(arr,low,high);
            quickSort(arr,low,p);
            quickSort(arr,p+1,high);
        }
    }
    
    public:
    int partition (int arr[], int low, int high)
    {
       int p=arr[low];
        low--;high++;
       while(true){
       
           do{
               low++;
           }while(arr[low] < p);
           
           do{
               high--;
           }while(arr[high] > p);
           
           if(low>=high)
           return high;
           
           int temp=arr[low];
           arr[low]=arr[high];
           arr[high]=temp;
       }
    }
};
0
aachalsaxena0014 weeks ago
void quickSort(int arr[], int low, int high){   // code here   while(low<high){       int pivot=partition(arr,low,high);       quickSort(arr,low,pivot-1);       quickSort(arr,pivot+1,high);   } }   int partition (int arr[], int low, int high){  int pivot=arr[high];  int i=low-1;  for(int j=0;j<high-1;j++){      if(arr[j]<pivot){          i++;          swap(arr[i],arr[j]);      }  }  swap(arr[i+1],arr[high]);  return (i+1);}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.