Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. /*
  2. * CSE 2032: Advanced Algorithms Lab
  3. * Quick Sort [First Element as pivot]
  4. *
  5. * Task 1: Understand the code thoroughly and add comments on each line (3.5 Marks)
  6. * Task 2: Take random numbers as input [Range(0-99)] (3.5 Marks)
  7. * Task 3: Convert the code from descending to ascending order (3 Marks)
  8. */
  9. #include <stdio.h>
  10.  
  11. int array[1000],low,high,temp;
  12.  
  13. void quicksort(int pivot, int last)
  14. {
  15. if(pivot<last)
  16. {
  17. low=pivot+1;
  18. high=last;
  19. while(array[low]<array[pivot])
  20. low++;
  21.  
  22. while(array[high]>array[pivot])
  23. high--;
  24.  
  25. while(low<high)
  26. {
  27. temp=array[high];
  28. array[high]=array[low];
  29. array[low]=temp;
  30.  
  31. while(array[low]<array[pivot])
  32. low++;
  33.  
  34. while(array[high]>array[pivot])
  35. high--;
  36. }
  37.  
  38. temp=array[high];
  39. array[high]=array[pivot];
  40. array[pivot]=temp;
  41.  
  42. quicksort(pivot,high-1);
  43. quicksort(high+1,last);
  44. }
  45.  
  46. }
  47. int main()
  48. {
  49. int n,i;
  50.  
  51. printf("Input the size of the array :");
  52. scanf("%d",&n);
  53.  
  54. printf("\n\nEnter each element: \n\n");
  55. for(i=0; i<n; i++)
  56. {
  57. scanf("%d",&array[i]);
  58. }
  59.  
  60. quicksort(0,n-1);
  61.  
  62. printf("\n\nAfter Sorting: \n\n");
  63. for(i=0; i<n; i++)
  64. printf("%d ",array[i]);
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement