Last active
December 14, 2015 11:39
-
-
Save kuoe0/5080566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*============================================================================= | |
# FileName: selectionSort.cpp | |
# Desc: Selecetion Sort | |
# 1. find minimum element in unsorted part | |
# 2. put minimum element at end of sorted part | |
# 3. repeat above steps until unsorted part is empty | |
# Author: KuoE0 | |
# Email: kuoe0.tw@gmail.com | |
# HomePage: http://kuoe0.ch/ | |
# Version: 0.0.1 | |
# LastChange: 2013-03-04 14:31:13 | |
# History: | |
=============================================================================*/ | |
#include <iostream> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <vector> | |
using namespace std; | |
void selectionSort( vector< int > &num ) { | |
for ( int i = 0; i < num.size(); ++i ) { | |
// find the minimum element in unsorted part | |
int MIN = num[ i ], pos = i; | |
for ( int j = i + 1; j < num.size(); ++j ) { | |
if ( MIN > num[ j ] ) | |
MIN = num[ j ], pos = j; | |
} | |
// put the minimum element at end of sorted part | |
swap( num[ i ], num[ pos ] ); | |
} | |
} | |
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 ); | |
} | |
selectionSort( num ); | |
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