Skip to content

Instantly share code, notes, and snippets.

@kuoe0 kuoe0/bubbleSort-flag.cpp
Last active Dec 14, 2015

Embed
What would you like to do?
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
int n;
while ( ~scanf( "%d", &n ) ) {
int x;
vector< int > num;
for ( int i = 0; i < n; ++i ) {
scanf( "%d", &x );
num.push_back( x );
}
bool flag = true;
for ( int i = 0; flag && i < n - 1; ++i ) {
flag = false;
for ( int j = 0; j < n - 1 - i; ++j )
if ( num[ j ] > num[ j + 1 ] ) {
swap( num[ j ], num[ j + 1 ] );
flag = true;
}
}
for ( int i = 0; i < num.size(); ++i )
printf( "%d ", num[ i ] );
puts( "" );
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
int n;
while ( ~scanf( "%d", &n ) ) {
int x;
vector< int > num;
for ( int i = 0; i < n; ++i ) {
scanf( "%d", &x );
num.push_back( x );
}
for ( int i = 0; i < n - 1; ++i )
for ( int j = 0; j < n - 1 - i; ++j )
if ( num[ j ] > num[ j + 1 ] )
swap( num[ j ], num[ j + 1 ] );
for ( int i = 0; i < num.size(); ++i )
printf( "%d ", num[ i ] );
puts( "" );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.