fork download
  1. #include<stdio.h>
  2. //quicksort
  3. void quicksort(int a[], int, int);
  4. int partition(int a[],int ,int);
  5.  
  6. int main() { int i,j,a[33],n,p,r,q;
  7. //printf("Enter the total number for sorting\n");
  8. scanf("%d", &n); for(i=0;i<n;i++) {
  9. //printf("Enter the number in array\n");
  10. scanf("%d", &a[i]); }
  11. p=0;
  12. r=n-1;
  13. quicksort(a,p,r);
  14. for (i=0;i<n;i++) {
  15. printf("\n sorted element %d",a[i]);
  16.  
  17. }
  18. return 0;
  19. }
  20.  
  21. void quicksort(int a[33],int p,int r)
  22. {
  23. int q;
  24. if(p<r){
  25. q=partition(a,p,r);
  26. quicksort(a,p,q-1);
  27. quicksort(a,q+1,r);
  28. }
  29. }
  30. int partition(int a[33], int p, int r)
  31. {
  32. int x,i, temp;
  33. x=a[r];
  34. i=p-1;
  35. for (int j=p;j<=r-1;j++)
  36. {
  37. if(a[j]<=x)
  38. {
  39. i=i+1;
  40. temp=a[i];
  41. a[i]=a[j];
  42. a[j]=temp;
  43. }
  44. }
  45. temp=a[i+1];
  46. a[i+1]=a[r];
  47. a[r]=temp;
  48. return i+1;
  49. }
Success #stdin #stdout 0s 15224KB
stdin
5
1 4 2 8 9
stdout
 sorted element 1
 sorted element 2
 sorted element 4
 sorted element 8
 sorted element 9