nrk009

Se_Bu_Ins_SORT

Jul 27th, 2020
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. /*
  2.     Was it all worth it ??
  3.     In the end all it remains are just memories!!!
  4.    
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  11. #define ll long long
  12. #define ld long double
  13. #define pb push_back
  14. #define fe first
  15. #define se second
  16. #define nl "\n"
  17. #define pp pair < ll , ll >
  18. #define sz(x) (ll)x.size()
  19. #define st(x) sort(x.begin(),x.end())
  20. #define rst(x) sort(x.rbegin(), x.rend())
  21. #define all(x) x.begin(),x.end()
  22. long double pi = 3.14159265358979323;
  23.  
  24. const double EPS = 1e-12;
  25. const int N = 1e6 + 5;
  26. //Just A Gentle Reminder about MOD
  27. const int mod = 1e9 + 7;
  28.  
  29.  
  30. void print(ll n , ll arr[]){
  31.  
  32.     for(int i = 0 ; i < n ; i++ ){
  33.         cout << arr[i] << " ";
  34.     }
  35. }
  36.  
  37. void selection_sort(ll n , ll arr[]){
  38.     cout << "selection_sort\n";
  39.  
  40.     for(int i = 0 ; i  < n;  i++ ){
  41.             int min_idx = i;
  42.             for(int j = i + 1 ; j < n ; j++ ){
  43.                     if(arr[j] < arr[min_idx]){
  44.                         min_idx = j;
  45.                     }
  46.             }
  47.             swap(arr[min_idx] , arr[i]);
  48.     }
  49.     print(n,arr);
  50.  
  51.     //Time Complexity - O(n^2)
  52.     //Best == Worst
  53.     //Space Complexity - O(n)
  54.     //No extra space is needed
  55. }
  56.  
  57. void bubble_sort(ll n , ll arr[]){
  58.     cout << "\nbubble_sort\n";
  59.  
  60.     for(int i = 0 ; i < n - 1 ; i++ ){
  61.             bool flag = 0 ;
  62.             for(int j = 0 ; j < n - i - 1 ; j++ ){
  63.                     if(arr[j] > arr[j+1]){
  64.                         swap(arr[j] , arr[j+1]);
  65.                         flag = 1;
  66.                     }
  67.             }
  68.             if(!flag){
  69.                 break;
  70.             }
  71.     }
  72.  
  73.     print(n,arr);
  74.  
  75.     //Time - Complexity - O(n^2)
  76.     //Best - O(n) - when array is already sorted
  77.     //Worst - O(n^2) when array is reverse sorted
  78.     //Space Complexity - O(n)
  79.     //No extra space is needed
  80. }
  81.  
  82. void insertion_sort(ll n , ll arr[]){
  83.     ll cnt = 0 ;
  84.     cout << "\ninsertion_sort\n";
  85.     for(int i = 1 ; i < n ; i++ ){
  86.             int j = i - 1;
  87.             ll key = arr[i];
  88.             while(j >= 0 && arr[j] > key){
  89.                 arr[j+1] = arr[j];
  90.                 cnt++;
  91.                 j--;
  92.             }
  93.             arr[j+1] = key;
  94.             //cout << "\n";
  95.     }
  96.     print(n,arr);
  97.     //Time - Complexity - O(n^2)
  98.     //Best - O(n) - when array is already sorted
  99.     //Worst - O(n^2) when array is reverse sorted
  100.     //Space Complexity - O(n)
  101.     //No extra space is needed
  102.  
  103. }
  104. int main()
  105. {
  106.     fast;
  107.     ll n;
  108.     cin >> n ;
  109.     ll arr[n];
  110.     for(int i = 0 ; i < n ;i++ ){
  111.         cin >> arr[i];      
  112.     }
  113.  
  114.     //selection_sort(n,arr);
  115.     //bubble_sort(n,arr);
  116.     //insertion_sort(n,arr);
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.    
  139.     return 0;
  140. //It's not end it yet
  141. }
Advertisement
Add Comment
Please, Sign In to add comment