Cod sursa(job #2676465)

Utilizator tact1m4n3Dicu Tudor Andrei tact1m4n3 Data 24 noiembrie 2020 13:35:20
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 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));
    
    infile >> list[0];
    int i = 1;
    do
    {
        infile >> list[i];
        while (list[i-1] > list[i])
        {
            swap(&(list[i]), &(list[i+1]));
            --i;
        }
        
        i++;
    } while (i < len);
    /*
    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;
}