×

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[25],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[25];


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.


Related Topics

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.