Understanding Quick Sort: A Divide and Conquer Sorting Algorithm


Introduction
As a part of my 100-day Data Structures and Algorithms (DSA) challenge, I recently delved into Quick Sort, a popular sorting algorithm known for its efficiency and elegance in sorting arrays. Quick Sort follows the principle of 'divide and conquer,' efficiently breaking down the problem into smaller, more manageable parts to solve it.
The Quick Sort Algorithm
#include <bits/stdc++.h>
using namespace std;
//------------------Partition--------------------------//
int partition(int arr[],int s,int e){
//consider the pivot element to be at start index
int pivot_element = arr[s];
//count the smaller element
int count = 0;
for(int i =s+1;i<=e;i++){
if(arr[i]<=pivot_element){
count++;
}
}
//pivot index would be s+count
int pivot_index = s+count;
swap(arr[pivot_index],arr[s]);
//fullfill the 3rd condition <e | e| >e
int i =s,j=e;
while(i < pivot_index && j > pivot_index){
while(arr[i]<arr[pivot_index]){
i++;
}
while (arr[j]>arr[pivot_index]){
j--;
}
if(i < pivot_index && j > pivot_index){
swap(arr[i++],arr[j--]);
}
}
return pivot_index;
}
//------------------Quick Sort-------------------------//
void quick_sort(int arr[],int s,int e){
//base Case
if(s>=e){
return;
}
//partitioning
int pivot = partition(arr,s,e);
//sort the left part
quick_sort(arr,s,pivot);
//sort the right part
quick_sort(arr,pivot+1,e);
}
int main(){
int arr[5] = {3,1,4,5,2};
int n = 5;
quick_sort(arr,0,n-1);
for(int e:arr){
cout<<e<<" ";
}
return 0;
}
Let's dive into the code that I explored and dissected, understanding its working and significance in sorting.
The Partition Function
The partition
function is the core of Quick Sort, responsible for rearranging elements around a pivot. Here's a simple breakdown of its steps:
Choosing a Pivot: The algorithm selects a pivot element (in this case, the first element of the array) to compare against.
Counting Smaller Elements: It counts the number of elements smaller than the pivot.
Rearranging Elements: It rearranges the elements such that all elements smaller than the pivot come before it, and larger elements come after it.
Quick Sort Function
The quick_sort
function implements the recursive nature of the Quick Sort algorithm:
Base Case: It checks if the start index is greater than or equal to the end index. If so, it returns, as the array is already sorted.
Partitioning the Array: It utilizes the
partition
function to find the correct position of the pivot.Sorting the Left and Right Parts: Recursively, it sorts the left and right portions of the array concerning the pivot, effectively dividing the problem into smaller sub-arrays until the entire array is sorted.
Sample Code Execution
In the main
function, the Quick Sort algorithm is applied to an example array {3, 1, 4, 5, 2}
of size 5. The sorted array is printed, demonstrating the effectiveness of the Quick Sort algorithm.
Understanding the Efficiency
Quick Sort is highly efficient, with an average time complexity of O(n log n). However, in its worst-case scenario, it can degrade to O(n^2) if the pivot selection is consistently poor. Despite this, its average-case efficiency and space complexity of O(log n) make it a favourable choice for sorting large datasets.
Conclusion
In conclusion, Quick Sort is a powerful and widely used sorting algorithm due to its simplicity, efficiency, and effectiveness in most scenarios. By dividing the problem and conquering it through recursive partitioning, Quick Sort stands as a testament to the effectiveness of divide-and-conquer strategies in algorithmic problem-solving.
Through my exploration and understanding of Quick Sort, I've gained insights into its inner workings, its efficiency, and its importance in the realm of sorting algorithms.
Subscribe to my newsletter
Read articles from Pranav Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Pranav Sharma
Pranav Sharma
Passionate Tech Enthusiast | Embracing the Power of DSA, Python Libraries, and Innovative Projects ๐๐ฌ Welcome to my profile! ๐ As a tech enthusiast, I'm constantly fueled by curiosity and a thirst for knowledge. I thrive on exploring the ever-evolving landscape of new technologies and pushing the boundaries of what's possible. ๐จโ๐ป My Expertise: ๐ก DSA in C++: Currently diving deep into the world of Data Structures and Algorithms in C++. Strengthening my problem-solving skills and optimizing code efficiency are my top priorities. ๐ Python Libraries: I'm well-versed in utilizing various Python libraries like OpenCV, NumPy, and Pandas to develop innovative solutions. Leveraging the power of these libraries, I'm shaping ideas into reality. ๐ฎ Projects: I've had the pleasure of working on exciting projects, including building a Brick Breaker game using Java frameworks, crafting unique Android apps, and even developing a hand recognition model using Python and OpenCV. ๐ My Approach: I believe in continuous learning and hands-on experience. By embracing real-world challenges and experimenting with cutting-edge technologies, I'm constantly expanding my skill set and nurturing my passion for technology. ๐ Lifelong Learner: I'm always on the lookout for opportunities to enhance my knowledge and keep up with the rapid pace of the tech industry. Staying updated with the latest trends and breakthroughs enables me to stay ahead of the curve. ๐ค Let's Connect: I'm eager to connect with fellow tech enthusiasts, industry professionals, and like-minded individuals who share my love for technology. Let's collaborate, exchange ideas, and inspire each other to reach new heights! โ๏ธ Feel free to reach out to me for exciting discussions, project collaborations, or any tech-related queries. Together, we can create something remarkable and make a positive impact in the world of technology. #TechEnthusiast #DataStructures #Algorithms #Python #OpenCV #NumPy #Pandas #ProjectDevelopment #LifelongLearner