All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Quick Sort In C

Last Updated : Mar 11, 2024

Quick Sort In C

In this article we will show you the solution of quick sort in C, firstly we understand the sorting and Quick. Sorting is the process of managing the elements in the structured format. Quick means fast or capable of acting with speed.

Quick Sort is nothing but it is an algorithm used in C language for sorting an array.

Now, let us understand the concept of Quick sort using C Language.

Step By Step Guide On Quick Sort In C :-

Quick Sort is developed by Tony Hoare(C.A.R Hoare) in 1959..

Quick Sort means it is the quick and it is the algorithm generally preferred for sorting.

It uses divide and conquer algorithm. It Means The type of algorithm will divide the main problem into sub-problems and also would break them collectively.

It works by taking a pivot element from the array and partitioning the other basics into two sub arrays, according to whether they're smaller than or greater than the pivot. It is the fast sorting algorithm.

It's also called as partition exchange sort or comparison sort.

It’s average complexity is O(logn(n)), in Quick sort elements are arranged in ascending order. Following are the steps used in quick sorting:

  • Find the pivot element which is used for diving array into two subarrays.
  • Then firstly sort the array from left side
  • And then sort the array from right side

Let’s understand about quick sort with the help of following code:

#include <stdio.h>
void swap(int *a, int *b) {
  int t = *a;
  *a = *b;
  *b = t;
}
int partition(int array[], int low, int high) {
  int pivot = array[high];
  int i = (low - 1);
  for (int j = low; j < high; j++) {
    if (array[j] <= pivot) {
      i++;
      swap(&array[i], &array[j]);
    }
  }
  swap(&array[i + 1], &array[high]);
  return (i + 1);
}
void quickSort(int array[], int low, int high) {
  if (low < high) {
    int pi = partition(array, low, high);
    quickSort(array, low, pi - 1);
    quickSort(array, pi + 1, high);
  }
}
void printArray(int array[], int size) {
  for (int i = 0; i < size; ++i) {
    printf("%d ", array[i]);
  }
  printf("\n");
}
int main() {
  int data[] = {23, 11, 25, 2, 3};
  int n = sizeof(data) / sizeof(data[0]);
  printf("Unsorted Array\n");
  printArray(data, n);
  quickSort(data, 0, n - 1);
  printf("Sorted array in ascending order: \n");
  printArray(data, n);
}
  1. In first line we use header file which is used for standard input output.
  2. After that we create the function which is used to swap the elements.
  3. Then we create the function for finding the position of the partition of an array in which we select the element as a pivot, greater element.
  4. After that we traverse each element of an array and compare them.
  5. Then we use the swap for swapping the elements.
  6. After that elements can be partitioned by their positions left or right.
  7. Then we create the function for printing the elements in an array.
  8. Then our main function begins in which we store the values in unsorted form which we get into the sorted form with the help of quick sort.

Conclusion :-

Hence, we successfully learnt about quick sort. We can see that it is the quickest sorting method. It uses the simple algorithm known as divide and conquer.

I hope this article on quick sort in C helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪