Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Implement Quicksort Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Implement Quicksort | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array, sort it using quicksort.

Approach

Quicksort is a divide and conquer algorithm. 

  • The basic idea is to divide the array into two parts about an element(say pivot point) such that the pivoted element is at the same position as the sorted array and all elements before the pivot point are less than the pivoted element and all elements after the pivot point are greater than or equal to the pivoted element.
  • Both the parts will be called recursively and the above process will be repeated. In this way, the array will break further until each partition becomes sorted.

Note: Here, the last element of the partition is used as the pivoted element.

Analysis

  • Time Complexity:
    • Best Case: O(n * log(n))
    • Average Case: O(n * log(n))
    • Worst Case: O(n2)
  • Auxiliary Space Complexity: O(log(n))

Implementation

C++
int makePartition (vector<int> &arr, int low, int high) {
   int pivot = arr[high];
   int currentIndx = low - 1;
   for (int i = low; i < high; i++) {
       if (arr[i] < pivot) {
           currentIndx++;
           int temp = arr[i];
           arr[i] = arr[currentIndx];
           arr[currentIndx] = temp;
       }
   }
   int temp = arr[high];
   arr[high] = arr[currentIndx + 1];
   arr[currentIndx + 1] = temp;
   return currentIndx + 1;
}
void quicksort (vector<int> &arr, int low, int high) {
   if (low < high) {
       int pivot = makePartition (arr, low, high);
       quicksort (arr, low, pivot - 1);
       quicksort (arr, pivot + 1, high);
   }
}
void quickSort(vector<int> &arr) {
   int n = arr.size();
   quicksort (arr, 0, n-1);
}
Java
class Solution {
   int makePartition (int[] arr, int low, int high) {
       int pivot = arr[high];
       int currentIndx = low - 1;
       for (int i = low; i < high; i++) {
           if (arr[i] < pivot) {
               currentIndx++;
               int temp = arr[i];
               arr[i] = arr[currentIndx];
               arr[currentIndx] = temp;
           }
       }
       int temp = arr[high];
       arr[high] = arr[currentIndx + 1];
       arr[currentIndx + 1] = temp;
       return currentIndx + 1;
   }
   void quicksort (int[] arr, int low, int high) {
       if (low < high) {
           int pivot = makePartition (arr, low, high);
           quicksort (arr, low, pivot - 1);
           quicksort (arr, pivot + 1, high);
       }
   }
   void quickSort (int[] arr) {
       int n = arr.length;
       quicksort (arr, 0, n-1);
   }
}
Related Content
Arithmetic Sequence
Implement Counting Sort
Implement Insertion Sort
Implement Merge Sort
Inversion Count
Kth Largest Element
Merge Overlapping Intervals
Merge Sorted Subarrays
Square sorted array
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet