Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Was it all worth it ??
- In the end all it remains are just memories!!!
- */
- #include<bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define nl "\n"
- #define pp pair < ll , ll >
- #define sz(x) (ll)x.size()
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 1e6 + 5;
- //Just A Gentle Reminder about MOD
- const int mod = 1e9 + 7;
- void print(ll n , ll arr[]){
- for(int i = 0 ; i < n ; i++ ){
- cout << arr[i] << " ";
- }
- }
- void selection_sort(ll n , ll arr[]){
- cout << "selection_sort\n";
- for(int i = 0 ; i < n; i++ ){
- int min_idx = i;
- for(int j = i + 1 ; j < n ; j++ ){
- if(arr[j] < arr[min_idx]){
- min_idx = j;
- }
- }
- swap(arr[min_idx] , arr[i]);
- }
- print(n,arr);
- //Time Complexity - O(n^2)
- //Best == Worst
- //Space Complexity - O(n)
- //No extra space is needed
- }
- void bubble_sort(ll n , ll arr[]){
- cout << "\nbubble_sort\n";
- for(int i = 0 ; i < n - 1 ; i++ ){
- bool flag = 0 ;
- for(int j = 0 ; j < n - i - 1 ; j++ ){
- if(arr[j] > arr[j+1]){
- swap(arr[j] , arr[j+1]);
- flag = 1;
- }
- }
- if(!flag){
- break;
- }
- }
- print(n,arr);
- //Time - Complexity - O(n^2)
- //Best - O(n) - when array is already sorted
- //Worst - O(n^2) when array is reverse sorted
- //Space Complexity - O(n)
- //No extra space is needed
- }
- void insertion_sort(ll n , ll arr[]){
- ll cnt = 0 ;
- cout << "\ninsertion_sort\n";
- for(int i = 1 ; i < n ; i++ ){
- int j = i - 1;
- ll key = arr[i];
- while(j >= 0 && arr[j] > key){
- arr[j+1] = arr[j];
- cnt++;
- j--;
- }
- arr[j+1] = key;
- //cout << "\n";
- }
- print(n,arr);
- //Time - Complexity - O(n^2)
- //Best - O(n) - when array is already sorted
- //Worst - O(n^2) when array is reverse sorted
- //Space Complexity - O(n)
- //No extra space is needed
- }
- int main()
- {
- fast;
- ll n;
- cin >> n ;
- ll arr[n];
- for(int i = 0 ; i < n ;i++ ){
- cin >> arr[i];
- }
- //selection_sort(n,arr);
- //bubble_sort(n,arr);
- //insertion_sort(n,arr);
- return 0;
- //It's not end it yet
- }
Advertisement
Add Comment
Please, Sign In to add comment