Explain the Quick Sort Technique in C Language



Sorting is the process of arranging the elements either in ascending (or) descending order. This process is determined for accessing and organizing data quickly. Sorting helps in optimizing search techniques and operations that make the data more readable and accessible. This involves comparing elements that rearrange the readable and accessible determined by criteria.

Types of sorting

Sorting in C involves different methods that determine the data in a certain order. C language provides five sorting techniques, which are as follows ?

Quick sort

Quick Sort is a divide and conquer algorithm. This selects a pivot element and divides the array into two subarrays.

  • Step 1: Pick an element from an array, and call it a pivot element.
  • Step 2: Divide an unsorted array element into two arrays.
  • Step 3: If the value less than the pivot element comes under the first sub-array, the remaining elements with a value greater than the pivot come in the second sub-array.

Consider an example given below, wherein

  • P is the pivot element.
  • L is the left pointer.
  • R is the right pointer.

The elements are 6, 3, 7, 2, 4, 5.

Now,

  • The pivot is in a fixed position.
  • All the remaining elements are less.
  • The right elements are greater than the pivot.
  • Now, divide the array into 2 sub-arrays, the left and right parts.
  • Take the left partition and apply a quick sort.

Now,

  • The pivot is in a fixed position.
  • All the remaining elements are fewer and sorted
  • The right elements are greater and are in sorted order.
  • The final sorted list combines two sub-arrays 2, 3, 4, 5, 6, 7

Example

Here is a C program that sorts elements using the quicksort technique. It takes user input for array elements and displays the sorted order ?

#include<stdio.h>

void quicksort(int number[25], int first, int last) {
   int i, j, pivot, temp;
   if (first & lt; last) {
      pivot = first;
      i = first;
      j = last;
      while (i & lt; j) {
         while (number[i] & lt; = number[pivot] && i & lt; last)
            i++;
         while (number[j] & gt; number[pivot])
            j--;
         if (i & lt; 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 & lt; count; i++)
      scanf("%d", & number[i]);
   quicksort(number, 0, count - 1);
   printf("Order of Sorted elements: ");
   for (i = 0; i & lt; count; i++)
      printf(" %d", number[i]);
   return 0;
}

Output

When the above program is executed, it produces the following result ?

How many elements are u going to enter?: 10
Enter 10 elements: 2 3 5 7 1 9 3 8 0 4
Order of Sorted elements: 0 1 2 3 3 4 5 7 8 9
Updated on: 2025-01-20T14:04:53+05:30

93K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements