Cod sursa(job #2676466)

Utilizator tact1m4n3Dicu Tudor Andrei tact1m4n3 Data 24 noiembrie 2020 13:36:16
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>

void swap(int* a, int* b)
{
    int c = *a;
    *a = *b;
    *b = c;
}

int main()
{
    std::ifstream infile("algsort.in");
    std::ofstream outfile("algsort.out");
    
    int len;
    infile >> len;
    
    int* list = (int*)alloca(len * sizeof(int));
    
    for (int i = 0; i < len; i++)
    {
        infile >> list[i];
    }
    
    for (int i = 0; i < len - 1; i++)
    {
        while (list[i] > list[i+1])
        {
            swap(&(list[i]), &(list[i+1]));
            --i;
        }
    }
    
    for (int i = 0; i < len; i++)
        outfile << list[i] << " ";
    
    return 0;
}