Advertisement
Adrita

task 5( ds lab 2) 3rd sem

Jan 22nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long ar1[2000000],ar2[2000000];
  4. void bubble_sort(long long ar[],long long n)
  5. {
  6. long long i,j,comp1=0;
  7. for(i=1; i<=n-1; i++)
  8. {
  9. for(j=n-1; j>=i; j--)
  10. {
  11. if(ar[j]<ar[j-1])
  12. {
  13. swap(ar[j],ar[j-1]);
  14. comp1++;
  15. }
  16. }
  17. }
  18. cout<<"In bubble sort:"<<endl;
  19. cout<<"Total comparisons: "<<comp1<<endl;
  20. }
  21. void insertion_sort(long long ar[],long long n)
  22. {
  23. long long i,j,key,comp=0;
  24. for(j=1; j<n; j++)
  25. {
  26. key=ar[j];
  27. i=j-1;
  28. while(i>=0&&ar[i]>key)
  29. {
  30. ar[i+1]=ar[i];
  31. i=i-1;
  32. comp++;
  33. }
  34. comp++;
  35. ar[i+1]=key;
  36. }
  37. cout<<"In insertion sort:"<<endl;
  38. cout<<"Total comparisons: "<<comp<<endl;
  39. }
  40. int main()
  41. {
  42. long long n;
  43. clock_t time_req;
  44. cin>>n;
  45. long long i;
  46. for(i=0; i<n; i++)
  47. {
  48. ar1[i]=rand();
  49. ar2[i]=ar1[i];
  50. }
  51. cout<<"\n";
  52. time_req = clock();
  53. bubble_sort(ar1,n);
  54. time_req = clock() - time_req;
  55. cout << "Total clock ticks: " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
  56. cout<<"\n";
  57. time_req = clock();
  58. insertion_sort(ar2,n);
  59. time_req = clock() - time_req;
  60. cout << "Total clock ticks: " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement