Yurttas/PL/OOL/Cplusplus/F/05/04/03/01/bubblesort.cpp

From ZCubes Wiki
Jump to navigation Jump to search
 1/*
 2   Copyright(C) 1998
 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, 1998.
11   author : Salih Yurttas.
12
13   bubblesort.cpp
14*/
15
16
17#include <vector>
18
19using namespace std;
20
21template<class T, class C>
22void bubblesort(vector<T>& list,
23                const C& compare) {
24  int n = list.size();
25
26  for(int i=0; i<n-1; i++)
27    for(int j=n-1; j>=i+1; j--)
28      if(compare(list[j],list[j-1])) {
29        T t = list[j];
30        list[j] = list[j-1];
31        list[j-1] = t;
32      }
33}