cplusplus-training/Teil_1/5. Felder/Lösungen/Loesung Uebung 4 - Bubble Sort.cpp
2021-10-15 08:36:02 +02:00

47 lines
679 B
C++

#include <iostream>
#include <cstdlib>
using namespace std ;
int main ()
{
int a [10] , i , zw , change ;
cout << "unsortiert: ";
for ( i =0; i <10; i ++)
{
a [i] = rand()%100;
cout << a [i] << " ";
}
// sortieren ( und dabei ausgeben ...)
do
{
cout << endl << "Zwischenschritt: ";
change=0; // keine Vertauschung noetig
for (i=0; i<9; i++)
if ( a [i] > a [i+1])
{
zw = a[i];
a[i] = a[i+1];
a[i+1] = zw;
change = 1; // Vertauschung
}
for (i=0; i<10; i++) cout << a [i] << " ";
}
while (change);
cout << endl;
cout << "sortiert: ";
for ( i =0; i <10; i ++)
{
cout << a [i] << " ";
}
cout << endl;
system("pause");
return 0;
}