### Tutorial Playlist

Tony Hoare, a British computer scientist, invented the QuickSort algorithm in 1959. The name "Quick-sort" stems from the fact that it can sort a list of data elements substantially faster (twice or three times faster) than any other sorting method.

Quicksort is one of the most efficient sorting algorithms. It works by breaking an array (partition) into smaller ones and swapping (exchanging) the smaller ones, depending on a comparison with the 'pivot' element picked.

By the end of this tutorial, you will have a better understanding of the fundamental technicalities of the Quick Sort with all the necessary details along with practical implementations.

## What Is the Quick Sort Algorithm?

Quicksort is a highly efficient sorting technique that divides a large data array into smaller ones. A vast array is divided into two arrays, one containing values smaller than the provided value, say pivot, on which the partition is based. The other contains values greater than the pivot value.

Now, look at the working of the Quick-Sort algorithm to understand the Quick  Sort better.

#### Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTME

## How Does Quick Sort Work?

To sort an array, you will follow the steps below:

1. You will make any index value in the array as a pivot.
2. Then you will partition the array according to the pivot.
3. Then you will recursively quicksort the left partition
4. After that, you will recursively quicksort the correct partition.

Let's have a closer look at the partition bit of this algorithm:

1. You will pick any pivot, let's say the highest index value.
2. You will take two variables to point left and right of the list, excluding pivot.
3. The left will point to the lower index, and the right will point to the higher index.
4. Now you will move all elements which are greater than pivot to the right.
5. Then you will move all elements smaller than the pivot to the left partition.

And this is how the QuickSort algorithm works. Now implement this algorithm through a simple C++ code.

#### New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & More

## How to Implement the Quick Sort Algorithm?

You will be provided with an array of elements {10, 7, 8, 9, 1, 5}. You have to write a code to sort this array using the QuickSort algorithm. The final array should come out to be as {1, 5, 7, 8, 9, 10}.

Code:

// C++ implementation of QuickSort

#include <bits/stdc++.h>

using namespace std;

// A utility function to swap two elements

void swap(int* a, int* b)

{

int t = *a;

*a = *b;

*b = t;

}

/* This function takes the final pivot element, puts the pivot element in an ordered array, and places all smaller elements on the left side of the pivot, as well as all larger elements on the right of the pivot. */

int partition (int arr[], int l, int h)

{

int pivot = arr[h]; // pivot

int i = (l - 1); // Index of smaller element and indicates the right position of pivot found so far

for (int k = l; k <= h - 1; k++)

{

// When the actual element is less than the pivot

if (arr[k] < pivot)

{

i++; // increment index of smaller element

swap(&arr[i], &arr[k]);

}

}

swap(&arr[i + 1], &arr[h]);

return (i + 1);

}

//A function to implement quicksort

void quickSort(int arr[], int l, int h)

{

if (l < h)

{

//pi is a partitioning index, and

//arr[p] is now in the correct location.

int pi = partition(arr, l, h);

// Separately sort elements before

// partition and after partition

quickSort(arr, l, pi - 1);

quickSort(arr, pi + 1, h);

}

}

/* Function to print an array */

void print_array(int arr[], int size)

{

int i;

for (i = 0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

}

int main()

{

int arr[] = {11, 13, 16, 1, 3, 5, 9};

int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

cout << "Sorted array: \n";

printArray(arr, n);

return 0;

}

You have now explored the working of Quick Sort with a code. Now you will see some of the advantages of the Quick Sort.

## What Are the Advantages of Quick Sort?

Let us discuss a few significant benefits of using Quick Sort and a few scenarios where Quick Sort is proven to be delivering the best performance.

• It is an in-place algorithm since it just requires a modest auxiliary stack.
• Sorting n objects takes only n (log n) time.
• Its inner loop is relatively short.
• After a thorough mathematical investigation of this algorithm, you can make a reasonably specific statement about performance issues.

#### Full Stack Web Developer Course

To become an expert in MEAN Stack

## What Are the Disadvantages of Quick Sort?

Despite it being the quickest algorithm, Quick Sort does a few downfalls. Let us address a few significant limitations that you should be considering before you implement Quick Sort in real-time.

• It is a recursive process. The implementation is quite tricky, mainly if recursion is not provided.
• In the worst-case scenario, it takes quadratic (i.e., n2) time.
• It is fragile in the sense that a slight error in implementation can go unreported and cause it to function poorly.

With this, you have come to an end of this tutorial. You will now look at what could be your next steps to master other sorting algorithms.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

## Next Steps

Your next stop in mastering data structures should be the selection Sort Algorithm. Using in-place comparisons, the selection sort algorithm divides the list into two parts, with the sorted half on the left and the unsorted half on the right.

If you're searching for a more in-depth understanding of software development that can help you carve out a successful career in the field, look no further. If that's the case, consider Simplilearn's Postgraduate Program in Full Stack Web Development, which is offered in collaboration with Caltech CTME, the world's largest technology education institution. This Post-Graduate Program will teach you how to master both front-end and back-end Java technologies, beginning with the fundamentals and progressing to the more advanced aspects of Full Stack Web Development. In this web development course, you'll study Angular, Spring Boot, Hibernate, JSPs, and MVC, which will help you to get started as a full-stack developer.

If you have any questions or require clarification on this "Merge Sort Algorithm" tutorial, please leave them in the comments section below. Our expert team will review them and respond as soon as possible.