Iterative Implementation of quick sort

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Given an array, this function will sort the array using quick sort. Here, quick sort is not implemented recursively, it is implemented in iterative manner.


arr[] = {4, 1, 10, 23, 5}

sorted array is {1, 4, 5, 10, 23}


Partition Algorithm

1. Take rightmost element as the pivot

2. From the start index to end index, Take a variable pIndex and point it to start index. If the element is less than pivot, swap with the element at pIndex and  increment pIndex. Otherwise, do nothing.  ie, pushing all the elements less than pivot to left and greater elements to the right

3. Swap the rightmost element with the element at pIndex

4. return pIndex 

iterativeQuicksort Algorithm

Create a stack which has the size of the array

1. Push Initial values of start and end in the stack ie, parent array(full array) start and end indexes

2. Till the stack is empty

3.  Pop start and end indexes in the stack

4. call the partition function and store the return value it in pivot_index

5. Now, push the left subarray indexes that is less to the pivot into the stack ie, start, pivot_index -1

6. push the right subarray indexes that is greater than pivot into the stack ie, pivot+1, end

The above algorthm can be clearly explained in below diagram

C++ Program

#include <bits/stdc++.h>
using namespace std;

int partition(int arr[], int start, int end)
	int pivot = arr[end]; //rightmost element is the pivot
	int pIndex = start;  //Is to push elements less than pivot to left and greater than to right of pivot
	for (int i = start; i < end; ++i)
		if (arr[i] < pivot)
	swap(arr[pIndex], arr[end]);
	return pIndex;
void iterativeQuicksort(int arr[], int start, int end)

	int stack[end - start + 1];//Initializing stack size
	int top = -1; //To keep track of top element in the stack
	//pushing intial start and end
	stack[++top] = start;
	stack[++top] = end; 	

	//pop till the stack is empty
	while(top >= 0) 
		//poping the top two elements 
		//ie,poping parent subarray indexes to replace child subbary indexes 
		end = stack[top--];
		start = stack[top--]; 	
		int pivot_index = partition(arr, start, end);

		//Pushing the left subarray indexes that are less than pivot
	    if (pivot_index - 1 > start )
	    	stack[++top] = start;
	    	stack[++top] = pivot_index -1;
	    //pushing the right subarray indexes that are greater than pivot
	    if (pivot_index + 1 < end)
	    	stack[++top] = pivot_index + 1;
	    	stack[++top] = end;

void printArray(int arr[], int n)
	//Printing the sorted array
	cout<<"Sorted array is [ ";
	for (int i = 0; i < n; ++i)
		cout<<arr[i]<<" ";

int main()
	int arr[]= {4, 1, 10, 23, 5};  //creating an array
    int n = sizeof(arr)/sizeof(arr[0]); //size of the array
    printArray(arr, n);
    return 0;
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top