Pagini recente » Cod sursa (job #3122959) | Cod sursa (job #622152) | Cod sursa (job #1981861) | Cod sursa (job #2761454) | Cod sursa (job #2676465)
#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;
}