Cod sursa(job #3039070)

Utilizator CapotaLucasLucasCapota CapotaLucas Data 28 martie 2023 09:59:03
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
// Selection Sort - Complexitate O(n^2)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

ifstream in("algsort.in");
ofstream out("algsort.out");

const int dim = 500001;

int v[dim];

int main()
{
    int n, i, poz_min,j;

    in >> n;
    for(i = 1; i <= n; i++) {
        in >> v[i];
    }

    // selection sort
    for(i = 1; i < n; i++) {
        poz_min = i;
        for(j = i + 1; j <= n; j++ ) {
            if(v[j] < v[poz_min]) {
                poz_min = j;
            }
        }
        swap(v[i], v[poz_min]);
    }

    // afisare vector v
    for (i = 1; i <= n; i++) {
        out << v[i] << " ";
    }

    return 0;
}