fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4.  
  5. void quicksort(int*,int,int);
  6. int partition(int*,int,int);
  7. void swap(int* a,int* b)
  8. {
  9. int temp=*a;
  10. *a=*b;
  11. *b=temp;
  12. }
  13.  
  14. int main()
  15. {
  16. int n=10000;
  17. int it=0;
  18. srand(time(NULL));
  19.  
  20. printf("Array_size,Time(m/s):\n");
  21.  
  22. while(it++<10)
  23. {
  24. int* arr=(int*)malloc(n*sizeof(int));
  25. if(!arr)
  26. {
  27. printf("Memory allocation failed\n");
  28. return 1;
  29. }
  30. for(int i=0;i<n;i++)
  31. {
  32. arr[i]=rand()%10000;
  33. }
  34.  
  35. clock_t start,end;
  36. start=clock();
  37. quicksort(arr,0,n-1);
  38. end=clock();
  39.  
  40. double timetaken=((double)(end-start))*1000/CLOCKS_PER_SEC;
  41. printf("%d,%0.5f\n",n,timetaken);
  42.  
  43. free(arr);
  44. n+=10000;
  45. }
  46. return 0;
  47. }
  48.  
  49. void quicksort(int arr[],int low,int high)
  50. {
  51. if(low<high)
  52. {
  53. int j=partition(arr,low,high);
  54. quicksort(arr,low,j-1);
  55. quicksort(arr,j+1,high);
  56. }
  57. }
  58.  
  59. int partition(int arr[],int low,int high)
  60. {
  61. int pivot=arr[low];
  62. int i=low;
  63. int j=high+1;
  64. while(1)
  65. {
  66. do{
  67. i++;
  68. }while(i<=high && arr[i]<pivot);
  69. do{
  70. j--;
  71. }while(j>=low && arr[j]>pivot);
  72. if(i>=j)break;
  73. swap(&arr[i],&arr[j]);
  74. }
  75. swap(&arr[low],&arr[j]);
  76. return j;
  77. }
  78.  
Success #stdin #stdout 0.06s 5276KB
stdin
Standard input is empty
stdout
Array_size,Time(m/s):
10000,1.05100
20000,2.64200
30000,2.10200
40000,3.15200
50000,4.14500
60000,5.25400
70000,5.89500
80000,7.35500
90000,7.99700
100000,8.40800