Yurttas/PL/OOL/Cplusplus/F/05/04/03/00/selectionsort.cpp

From ZCubes Wiki
Jump to navigation Jump to search
 1/*
 2   Copyright(C) 2002
 3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4
 5   Permission to use, copy, modify, and distribute this
 6   software and its documentation for EDUCATIONAL purposes
 7   and without fee is hereby granted provided that this
 8   copyright notice appears in all copies.
 9
10   date   : January 1, 2002.
11   author : Salih Yurttas.
12
13   selectionsort.cpp
14*/
15
16
17#include <vector>
18
19using namespace std;
20
21template<class T, class C>
22void selectionsort(vector<T>& list,
23                   const C& compare) {
24  int n = list.size();
25
26  for(int i=0; i<n-1; i++) {
27    int k=i;
28    T key=list[i];
29    for(int j=i+1; j<n; j++)
30      if(compare(list[j],key)) {
31        k=j;
32        key=list[j];
33      }
34    list[k]=list[i];
35    list[i]=key;
36  }
37}