Skip to content
Snippets Groups Projects
Commit c9af2a19 authored by Rudi Buss's avatar Rudi Buss
Browse files

Update Solution Exercise 4 - Bubble sort.cpp

parent c741acd9
No related branches found
No related tags found
No related merge requests found
......@@ -2,45 +2,38 @@
using namespace std;
int main ()
{
int a [10] , i , zw , change ;
cout << "Unsorted: ";
for ( i =0; i <10; i ++)
{
a [i] = rand()%100;
cout << a [i] << " ";
}
// sorting (output of the intermediate steps while sorting…)
do
{
cout << endl << "Intermediate step: ";
change=0; // No swapping necessary
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; // Swapping
}
for (i=0; i<10; i++)
cout << a [i] << " ";
}
while (change);
cout << endl;
cout << "Sorted: ";
for ( i =0; i <10; i ++)
{
cout << a [i] << " ";
}
cout << endl;
return 0;
int main() {
int a[10], i, zw, change;
cout << "Unsorted: ";
for (i = 0; i < 10; i++) {
a[i] = rand() % 100;
cout << a[i] << " ";
}
// sorting (output of the intermediate steps while sorting…)
do {
cout << endl << "Intermediate step: ";
change = 0; // No swapping necessary
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; // Swapping
}
for (i = 0; i < 10; i++) cout << a[i] << " ";
} while (change);
cout << endl;
cout << "Sorted: ";
for (i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment