Cod sursa(job #2151100)

Utilizator httpsLup Vasile https Data 4 martie 2018 02:15:40
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>

using namespace std;

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

int a[100];
int main()
{
    int n;
    fin>>n;

    //int a[n+1];
    for(int i = 1;i<=n;++i) fin>>a[i];

    int leftInd = 0, rightInd = n+1;
	bool change = true;

    while (change && leftInd + 1 < rightInd) {

		change = false;
        for (int i = leftInd + 1; i < rightInd - 1; ++i) {
            if (a[i]>a[i + 1]) {
                int aux = a[i];
                a[i] = a[i + 1];
                a[i + 1] = aux;
				change = true;
            }
        }

		--rightInd;

        for (int i = rightInd - 1; i > leftInd + 1; --i) {
            if (a[i]<a[i-1]) {
                int aux = a[i];
                a[i] = a[i - 1];
                a[i - 1] = aux;
				change = true;
            }
        }

		leftInd++;
    }

    for(int i = 1;i<=n;++i)
        fout<<a[i]<<' ';
    fout.close();
    return 0;
}