×

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

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 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.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.