Advertisement
shawonrog

QuickSortInC

Jul 14th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include<stdio.h>
  2. void quicksort(int num[50],int left,int right){
  3.    int i, j, pivot, temp;
  4.  
  5.    if(left<right){
  6.       pivot=left;
  7.       i=left;
  8.       j=right;
  9.  
  10.       while(i<j){
  11.          while(num[i]<=num[pivot]&&i<right)
  12.             i++;
  13.          while(num[j]>num[pivot])
  14.             j--;
  15.          if(i<j){
  16.             temp=num[i];
  17.             num[i]=num[j];
  18.             num[j]=temp;
  19.          }
  20.       }
  21.  
  22.       temp=num[pivot];
  23.       num[pivot]=num[j];
  24.       num[j]=temp;
  25.       quicksort(num,left,j-1);
  26.       quicksort(num,j+1,right);
  27.  
  28.    }
  29. }
  30.  
  31. int main(){
  32.    int i, count, num[50];
  33.  
  34.    printf("The number of elements we are going to enter: ");
  35.    scanf("%d",&count);
  36.  
  37.    printf("Enter %d elements: ", count);
  38.    for(i=0;i<count;i++)
  39.       scanf("%d",&num[i]);
  40.  
  41.    quicksort(num,0,count-1);
  42.  
  43.    printf("Sorted in Ascending order: ");
  44.    for(i=0;i<count;i++)
  45.       printf(" %d",num[i]);
  46.  
  47.    return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement