Advertisement
Mostafizur_Rahman

Untitled

Jun 26th, 2021
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void counting_sort(int arr[],int cnt)
  6. {
  7.     int n=cnt;
  8.     int x = arr[1];
  9.     for(int i=2; i<=n; i++)
  10.     {
  11.         if(arr[i]>x)
  12.             x=arr[i];
  13.  
  14.     }
  15.     int freq[x+1]= {0,};
  16.     for(int i=1; i<=n; i++)
  17.     {
  18.         freq[arr[i]]++;
  19.     }
  20.     for(int i=1; i<=x; i++)
  21.     {
  22.         while(freq[i]--)
  23.             cout<<i<<" ";
  24.     }
  25.     cout<<endl;
  26.  
  27. }
  28. void bubble_sort(int arr[], int n)
  29. {
  30.  
  31.     int i, j;
  32.     for (i = 0; i < n-1; i++)
  33.         for (j = 0; j < n-i-1; j++)
  34.             if (arr[j] > arr[j+1])
  35.                 swap(arr[j], arr[j+1]);
  36.       for(int i=0;i<n;i++)
  37.       {
  38.             cout<<arr[i]<<" ";
  39.       }
  40.       cout<<endl;
  41. }
  42.  
  43. int main()
  44. {
  45.     string x;
  46.     ifstream fin;
  47.     int arr[100005],i=0,cnt=0;
  48.     fin.open("input.txt");
  49.     while(!fin.eof())
  50.     {
  51.         fin>>x;
  52.         int n=stoi(x);
  53.         arr[i++]=n;
  54.         cnt++;
  55.  
  56.     }
  57.     clock_t start = clock();
  58.  
  59.     counting_sort(arr,1000);
  60.  
  61.     printf("\nUsing Counting Sort = %0.5fms\n", (float)(clock() - start) / CLOCKS_PER_SEC);
  62.       cout<<endl<<endl;
  63.     clock_t start2 = clock();
  64.  
  65.     bubble_sort(arr,1000-1);
  66.     printf("\n Using Bubble Sort = %0.5fms\n", (float)(clock() - start2) / CLOCKS_PER_SEC);
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement