fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include<time.h>
  5.  
  6. /* function to swap two elements in the array */
  7. void swap(int *x,int *y)
  8. {
  9. int temp;
  10. temp=*x;
  11. *x=*y;
  12. *y=temp;
  13. }
  14. int partition(int *a,int begin,int end)
  15. {
  16. int pivot=a[end],i;
  17. int indexno=begin;
  18. for(i=begin; i<end; i++)
  19. {
  20. if(a[i]<=pivot)
  21. {
  22. swap(&a[indexno],&a[i]);
  23. indexno++;
  24. }
  25. }
  26. swap(&a[indexno],&a[end]);
  27. return indexno;
  28. }
  29.  
  30. void quicksort(int *a,int begin,int end)
  31. {
  32. int j;
  33. if(begin<end)
  34. {
  35. j=partition(a,begin,end);
  36. quicksort(a,begin,j-1);
  37. quicksort(a,j+1,end);
  38. }
  39. }
  40. int main()
  41. {
  42. int a[50],i,j=20;
  43. int n=sizeof(a)/sizeof(a[0]);
  44. while(j--){
  45. srand(time(NULL));
  46.  
  47. printf("Enter number of elements\n");
  48. scanf("%d", &n);
  49.  
  50. printf("Enter %d integers\n", n);
  51.  
  52. for ( i = 0 ; i < n ; i++ )
  53. a[i]=rand();
  54. clock_t launch;
  55. launch = clock();
  56. quicksort(a,0,n-1);
  57. clock_t done;
  58. done = clock();
  59. double elapsed = (double)(done - launch) / CLOCKS_PER_SEC;
  60. for(i=0; i<n; i++)
  61. printf("%d ",a[i]);
  62. printf("the answer is %f",elapsed);
  63. }
  64. return 0;
  65. }
  66.  
  67.  
  68.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000003Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000002Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000003Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000000Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001Enter number of elements
Enter 50 integers
28955274 67192040 155673986 182691472 197326138 254646435 336579177 342477636 361469302 387304896 444514133 517143288 601238442 613491358 645004284 661345609 676849012 677703596 716784165 771482169 788972819 988743660 1040075172 1088427834 1125551996 1132068211 1214729800 1258377662 1294882743 1297925713 1385693484 1438639175 1516519457 1574058066 1608467805 1613500754 1659295634 1665592672 1701713204 1786897606 1862918810 1896381294 1899123456 1930110850 1946993496 1981674821 2044190840 2063582672 2112802323 2113073908 the answer is 0.000001