# Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given array will be picked.

The quick sort is an efficient way of sorting algorithm and will be based on partitioning the array of the data into identical arrays.

A large number of the partitioned into two different sub-arrays which holds the values.

It divides the input of an array into two halves and then calls itself for the two halves, and it then merges the sorted two halves into one final array, which will be sorted.

Quick sort calls itself recursively twice in order to sort the resulting two sub arrays. This algorithm is quite efficient for many extensive sized data, unlike other sorting techniques as both the average and the worst case complexities are O (n2).

Quick sort was first developed by a British computer scientist named Tony Hoare in the year 1959.

The quick name sort was given to it as the sorting was made quickly, and the list of data elements was significantly faster than any of the other sorting methods, almost twice as faster.

It is based on splitting an array into smaller ones and then swapping it based on the comparison with the selected element.

The comparing with an element named the pivot is then referred to as partition exchange.

## Consider an example of sorting the elements which are present in alphabetical order. It can be done as follows:

1. Choose a splitting value, such as L. the value selected as a splitting value is called the pivot. Now divide the stack of an array consisting of all the alphabets from A to Z into two sub-arrays, A to L and M to Z. There is no rule that the two sub-arrays must always be equal in length.
2. Divide the stack of an array consisting of all the alphabets from A to Z into two sub-arrays, A to L and M to Z. There is no rule that the two sub arrays must always be equal in length.
3. Repeat the above steps 1 and 2 with the A to L pile by splitting it into two halves. In the same manner, with the pile of M to Z by again splitting it into its two halves. This process is repeated until the sub arrays are small enough so that they can quickly be sorted.
4. Finally, the smaller arrays can be placed on top of each other in order to produce a fully sorted and the ordered set of papers.
5. The reduction approach will be made use in order to split to get the single element array.
6. At each and every split, the pile will be divided, and then the same approach will be used again for the smaller piles by using the method of recursion.

## Quick sort follows the below steps:

Step 1 - Make any element a pivot

Step 2 - Partition the array on the basis of pivot

Step 3 - Apply a quick sort on the left partition recursively

Step 4 - Apply a quick sort on the correct partition recursively

## Pseudocode for quick sort:

``````quickSort(arr[], low, high)
{
if (low < high)
{
// pivot_index is partitioning index, arr[pivot_index] is now at correct place in sorted array
pivot_index = partition(arr, low, high);

quickSort(arr, low, pivot_index - 1);  // Before pivot_index
quickSort(arr, pivot_index + 1, high); // After pivot_index
}
}
``````

E.g.:

``````#include<stdio.h>

void quicksort(int number,int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;

while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main()
{
int i, count, number;

printf("How many elements are u going to enter?: ");
scanf("%d",&count);

printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
``````

Output:

``````How many elements are you going to enter?: 7
Enter seven elements: 4 1 3 2 7 6 5
Order of Sorted elements: 1 2 3 4 5 6 7
``````

## Application

There are many applications of the quick sort technique.

Before we all go into any algorithm, we should first understand its implications in the real world. Quick sort is a method for rapidly and systematically sorting any list of items. Some of the applications where quick sort is employed are mentioned below.

• Commercial computing: Used in a multitude of state and international enterprises for sorting data, including such accounts/profiles by name or any given ID, transactions by time or place, files by name or date of creation, and so on.
• Numerical computations: To achieve accuracy in all calculations, the most efficiently designed algorithms utilize priority queues and sorting.
• Information search: Sorting algorithms aid in better information search and what faster way to sort than with quick sort.

Quick sort is used for faster results and in the cases where there are space constraints.