×

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

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

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

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

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

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

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.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.