C Program to Sort an Array Using Quick Sort



Quick Sort is one of the most efficient sorting algorithms and is based on the splitting of an array into smaller ones. Worst-case space complexity of Quick Sort is ‎O(n). In this program we will implement Quick Sort Algorithm.



  1. //C program to quicksort
  2. #include<stdio.h>
  3. void quicksort(int number[50],int f,int l){
  4. int i, j, pt, temp;
  5. if(f<l){
  6. pt=f;
  7. i=f;
  8. j=l;
  9. while(i<j){
  10. while(number[i]<=number[pt]&&i<l)
  11. i++;
  12. while(number[j]>number[pt])
  13. j--;
  14. if(i<j){
  15. temp=number[i];
  16. number[i]=number[j];
  17. number[j]=temp;
  18. }
  19. }
  20. temp=number[pt];
  21. number[pt]=number[j];
  22. number[j]=temp;
  23. quicksort(number,f,j-1);
  24. quicksort(number,j+1,l);
  25. }
  26. }
  27. int main(){
  28. int i, count, number[25];
  29. printf("How many elements are u going to enter?: ");
  30. scanf("%d",&count);
  31. printf("Enter %d elements: ", count);
  32. for(i=0;i<count;i++)
  33. scanf("%d",&number[i]);
  34. quicksort(number,0,count-1);
  35. printf("Order of Sorted elements: ");
  36. for(i=0;i<count;i++)
  37. printf(" %d",number[i]);
  38. return 0;
  39. }

Output
Output!
download android app

Popular C Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube