HomePOSTQuicksort program in C Language

Quicksort program in C Language

Quicksort is a divide and conquer algorithm Here In this article we are going to discuss about Quick sort algorithm with C Language programming language and Also we will learn about Types of Sorting Algorithm.


The steps are:

  1. Pick an element from the array elements, this element is called as pivot element.
  2. Divide the unsorted array of elements in two arrays with values less than the pivot come in the first sub array, while all elements with values greater than the pivot come in the second sub-array (equal values can go either way). This step is called the partition operation.
  3. Recursively repeat the step number-2(until the sub-arrays are sorted) to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values. The same logic we have implemented in the following C language program.

Time complexity of Quick Sort

  •  worst case time complexity O(n2
<img width=

Types of Sorting Algorithms:

Quicksort algorithm C Program implementation


#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 YOU Wants 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 is: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
}





OUTPUT



How many elements YOU Wants to enter?: 5

Enter 5 Elements : 10,8,9,4,2

Order of Sorted elements is: 2,4,8,9,10

Additional Reading

you can read more articles like this here.

READ MORE

If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.❤

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

- Advertisment -