QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
DSA for Beginners DSA Tutorial Data Structures Algorithms Array Strings Linked List Stack Queue Tree Graph Searching Sorting Recu
Application and uses of QuickSort is a sorting algorithm based on the Divide and
Quicksort Conquer algorithm that picks an element as a pivot and
partitions the given array around the picked pivot by
QuickSort using different
languages placing the pivot in its correct position in the sorted array.
Iterative QuickSort
Different implementations
How does QuickSort work?
of QuickSort
The key process in quickSort is a partition(). The target of
Visualization of QuickSort
partitions is to place the pivot (any element can be chosen
to be a pivot) at its correct position in the sorted array and
Partitions in QuickSort
put all smaller elements to the left of the pivot, and all
Some problems on greater elements to the right of the pivot.
QuickSort
Partition is done recursively on each side of the pivot after
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and Got It !
the pivot isourplaced
understood in its& Privacy
Cookie Policy correct position and this finally
Policy
sorts the array.
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 1/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
Choice of Pivot:
Partition Algorithm:
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 2/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 3/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 4/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
Illustration of Quicksort:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
Quicksort: Performing the partition
understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 5/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
#include <bits/stdc++.h>
using namespace std;
int pivot=arr[high];
//Index of smaller element and Indicate
//the right position of pivot found so far
int i=(low-1);
for(int j=low;j<=high;j++)
{
//If current element is smaller than the pivot
if(arr[j]<pivot)
{
//Increment index of smaller element
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[high]);
return (i+1);
}
{
// pi is the partition Skip to content
return index of pivot
https://www.geeksforgeeks.org/quick-sort/ 6/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
int pi=partition(arr,low,high);
//Recursion Call
//smaller element than pivot goes left and
//higher element goes right
quickSort(arr,low,pi-1);
quickSort(arr,pi+1,high);
}
}
int main() {
int arr[]={10,7,8,9,1,5};
int n=sizeof(arr)/sizeof(arr[0]);
// Function call
quickSort(arr,0,n-1);
//Print the sorted array
cout<<"Sorted Array\n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
// This Code is Contributed By Diwakar Jha
Java
class GFG {
We use cookies to ensure you have the best browsing experience on our website.
// A utility functionBy using
toourswap
site, you
twoacknowledge
elements that you have read and
understood our Cookie Policy & Privacy Policy
static void swap(int[] arr, int i, int j)
{
Skip to content
int temp = arr[i];
https://www.geeksforgeeks.org/quick-sort/ 7/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
arr[i] = arr[j];
arr[j] = temp;
}
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 8/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
// Driver Code
public static void main(String[] args)
{
int[] arr = { 10, 7, 8, 9, 1, 5 };
int N = arr.length;
// Function call
quickSort(arr, 0, N - 1);
System.out.println("Sorted array:");
printArr(arr);
}
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
Python3understood our Cookie Policy & Privacy Policy
# Python3 implementation
Skip toof QuickSort
content
https://www.geeksforgeeks.org/quick-sort/ 9/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
# Driver code
if __name__ == '__main__':
array = [10, 7, 8, 9, 1, 5]
N = len(array)
# Function call
quicksort(array, 0, N - 1)
print('Sorted array:')
for x in array:
print(x, end=" ")
C#
// C# implementation of QuickSort
using System;
class GFG {
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 12/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
// Driver Code
public static void Main()
{
int[] arr = { 10, 7, 8, 9, 1, 5 };
int N = arr.Length;
// Function call
quickSort(arr, 0, N - 1);
Console.WriteLine("Sorted array:");
for (int i = 0; i < N; i++)
Console.Write(arr[i] + " ");
}
}
Javascript
i++;
[arr[i], arr[j]] = [arr[j], arr[i]]; // Swap
}
}
// Driver code
let arr = [10, 7, 8, 9, 1, 5];
let N = arr.length;
// Function call
quickSort(arr, 0, N - 1);
console.log("Sorted array:");
console.log(arr.join(" "));
PHP
<?php
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood
// our
ThisCookie Policy & Privacy
Function takesPolicy
place last element as pivot
// Place the pivot as correct position
// In Sorted Array, and places all smaller to left
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 14/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
function partition(&$arr,$low,$high)
{
// Choose the Pivot Element
$pivot= $arr[$high];
for($j=$low;$j<=$high-1;$j++)
{
if($arr[$j]<$pivot)
{
// Increment index of smaller element
$i++;
list($arr[$i],$arr[$j])=array($arr[$j],$arr[$i]);
}
}
// Pivot element as correct position
list($arr[$i+1],$arr[$high])=array($arr[$high],$arr[$
return ($i+1);
}
function quickSort(&$arr,$low,$high)
{
if($low<$high)
{
// pi is partition
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
$pi=
understood ourpartition($arr,$low,$high);
Cookie Policy & Privacy Policy
// Sort the array
// Before the partition of Element
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 15/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
quickSort($arr,$low,$pi-1);
quickSort($arr,$pi+1,$high);
}
}
// Driver Code
$arr= array(10,7,8,9,1,5);
$N=count($arr);
// Function Call
quickSort($arr,0,$N-1);
echo "Sorted Array:\n";
for($i=0;$i<$N;$i++)
{
echo $arr[$i]. " ";
}
//This code is contributed by Diwakar Jha
?>
Output
Sorted array:
1 5 7 8 9 10
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 16/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
Time Complexity:
Recommended Problem
Similar Reads
Generic Static Data
Implementation of Structure vs
QuickSort… Dynamic Data…
QuickSort on QuickSort on
Doubly Linked List Singly Linked List
Related Tutorials
Mathematical and Learn Data
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
Geometric
understood our Cookie Policy & Privacy Policy
Structures with
Algorithms - Dat… Javascript | DSA…
Skip to content
https://www.geeksforgeeks.org/quick-sort/ 19/23
10/18/23, 11:54 PM QuickSort - Data Structure and Algorithm Tutorials - GeeksforGeeks
Introduction to
Map – Data
Structure and…
Next
Article Contributed By :
GeeksforGeeks
All Cheat
Sheets
Experienced CBSE Notes for Human Science and SBI Clerk Write Interview
Interviews Class 10 Resource Technology Syllabus Experience
Internship CBSE Notes for Management Notes IBPS PO Internships
Interviews Class 11 (HRM) Economics Syllabus
Competitive CBSE Notes for Management Notes IBPS Clerk
Programming Class 12 Income Tax Important Syllabus
Aptitude English Finance Topics in Ethics Aptitude
Preparation Grammar Statistics for UPSC Previous Questions
Economics Year Papers SSC CGL
Practice Papers
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/quick-sort/ 23/23