Cod sursa(job #2998448)

Utilizator DariusGhercaDarius Gherca DariusGherca Data 9 martie 2023 15:19:22
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>
//#define f cin
//#define g cout
using namespace std;
vector <int> v;
void insertion_sort(vector <int> &v, int &n){
    for(int i = 1; i < v.size(); i++){
        int j = i, key = v[i];
        while(j > 0 && v[j-1] > key){
            swap(v[j], v[j-1]);
            j--;
        }
    }
}
void count_sort(vector <int> &v, int &n){
    vector <int> a(INT_MAX);
    int maxi = -1;
    for(int i = 0; i < v.size(); i++){
        a[v[i]]++;
        maxi = max(maxi, v[i]);
    }
    v.clear();
    for(int i = 0; i <= maxi;i++){
        while(a[i]){
            v.push_back(i);
            a[i]--;
        }
    }
}
/*
    cod python dat de prof:
    def insertion_sort(v):
        for i in range(1, len(v)):
            j = i - 1
            key = v[i]
            while j >= 0 and  v[j] > key:
                v[j+1] = v[j]
                j -= 1
            v[j+1] = key
*/
int main()
{
    ifstream f("algsort.in");
    int n;
    f>>n;
    for(int i=1; i<=n; i++){
        int x;
        f>>x;
        v.push_back(x);
    }
    f.close();
    count_sort(v, n);
    ofstream g("algsort.out");
    for(auto i:v){
        g<<i<<" ";
    }
    g.close();
    return 0;
}