From cf3a29feb5887344b6633ead1b4b6d5657a15a4b Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sun, 15 Jun 2014 03:24:33 +0200 Subject: old stuff: algorithms --- algorithms/selection_sort.cc | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 algorithms/selection_sort.cc (limited to 'algorithms/selection_sort.cc') diff --git a/algorithms/selection_sort.cc b/algorithms/selection_sort.cc new file mode 100644 index 0000000..1c42a78 --- /dev/null +++ b/algorithms/selection_sort.cc @@ -0,0 +1,45 @@ +#include + +#define N 4 + +using namespace std; + + +void +selectionSort(int *A, int n) +{ + int i, j, max, maxpos; + for (j=n-1; j>0; j--) { + /* find max between A[0],....A[j] */ + max = A[j]; + maxpos = j; + for (i=j-1; i>=0; i--) { + if (A[i]>max) { + max = A[i]; + maxpos = i; + } + } + /* swap */ + A[maxpos] = A[j]; + A[j] = max; + } +} + + +int +main (int argc, char * const argv[]) { + int A[N] = {3,1,1,2}; + + for(unsigned int i=0; i