Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- long long ar1[2000000],ar2[2000000];
- void bubble_sort(long long ar[],long long n)
- {
- long long i,j,comp1=0;
- for(i=1; i<=n-1; i++)
- {
- for(j=n-1; j>=i; j--)
- {
- if(ar[j]<ar[j-1])
- {
- swap(ar[j],ar[j-1]);
- comp1++;
- }
- }
- }
- cout<<"In bubble sort:"<<endl;
- cout<<"Total comparisons: "<<comp1<<endl;
- }
- void insertion_sort(long long ar[],long long n)
- {
- long long i,j,key,comp=0;
- for(j=1; j<n; j++)
- {
- key=ar[j];
- i=j-1;
- while(i>=0&&ar[i]>key)
- {
- ar[i+1]=ar[i];
- i=i-1;
- comp++;
- }
- comp++;
- ar[i+1]=key;
- }
- cout<<"In insertion sort:"<<endl;
- cout<<"Total comparisons: "<<comp<<endl;
- }
- int main()
- {
- long long n;
- clock_t time_req;
- cin>>n;
- long long i;
- for(i=0; i<n; i++)
- {
- ar1[i]=rand();
- ar2[i]=ar1[i];
- }
- cout<<"\n";
- time_req = clock();
- bubble_sort(ar1,n);
- time_req = clock() - time_req;
- cout << "Total clock ticks: " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
- cout<<"\n";
- time_req = clock();
- insertion_sort(ar2,n);
- time_req = clock() - time_req;
- cout << "Total clock ticks: " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement