Skip to content

Instantly share code, notes, and snippets.

@kuoe0
Created March 3, 2013 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuoe0/5076854 to your computer and use it in GitHub Desktop.
Save kuoe0/5076854 to your computer and use it in GitHub Desktop.
Insertion Sort
/*=============================================================================
# FileName: insertionSort.cpp
# Desc: Insertion Sort
# Author: KuoE0
# Email: kuoe0.tw@gmail.com
# HomePage: http://kuoe0.ch/
# Version: 0.0.1
# LastChange: 2013-03-04 00:42:04
# History:
=============================================================================*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
void insertionSort( vector< int > &num ) {
for ( int i = 1; i < num.size(); ++i ) {
int temp = num[ i ], pos = i; // record current value
// find the insertion position
for ( int j = pos - 1; j >= 0 && num[ j ] > temp; --j )
num[ j + 1 ] = num[ pos = j ];
num[ pos ] = temp;
}
}
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 );
}
insertionSort( 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