Generic BubbleSort

Bluga

Banned
Nov 28, 2000
4,315
0
0
I'm writing a function that will sort any STL container, using the bubble-sort algorithm. The program below works, but i'm not sure if it's "Generic" enough? Ant comments is appreciated
!

#include <iostream>
using namespace std;

template <class X, int size>
class MySortClass
{
public:
X arr[size];
void bubble_sort()
{
for (int i=0; i<size-1; i++)
{
for (int j=0; j<size-i-1; j++)
{
if (arr[j]>arr[j+1])
{
X temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void print()
{
for (int i=0; i<size; i++)
{
cout<<arr<<" ";
}
}
};

void main(void)
{
MySortClass<char, 3> mysortclass;
mysortclass.arr[0] = 'a';
mysortclass.arr[1] = 'c';
mysortclass.arr[2] = 'b';

cout<<"Before sort: ";
mysortclass.print();
cout<<endl;
mysortclass.bubble_sort();
cout<<"After sort: ";
mysortclass.print();
}

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Some comments:

Your class doesn't fit the STL "model". The template should provide sorting by working on Random Access Iterators.

ex)
vector<int> vec;
vec.push_back(3);
vec.push_back(2);
vec.push_back(1);


MyNamespace::bubble_sort(vec.begin(), vec.end());

 

Bluga

Banned
Nov 28, 2000
4,315
0
0
Originally posted by: singh
Some comments:

Your class doesn't fit the STL "model". The template should provide sorting by working on Random Access Iterators.

ex)
vector<int> vec;
vec.push_back(3);
vec.push_back(2);
vec.push_back(1);


MyNamespace::bubble_sort(vec.begin(), vec.end());

Thanks, how can i modify the program? Just change the parameters?

 

Bluga

Banned
Nov 28, 2000
4,315
0
0
or something like this?


template< class T >
void sort(T x[], int n)
//bubble sort
{
int pass = 0;
bool interchange = true; // a flag to check if a swap was performed
while (pass < n-1 && interchange)
{
interchange = false;
for (int j = 0; j < (n-1)-pass ; j++)
if (x[j] > x[j+1])
{
interchange = true;
swaper(x[j], x[j+1]);
}
}
}

template< class T >
void swaper(T & a, T & b)
{
T temp = a;
a = b;
b= temp;
}
 

Bluga

Banned
Nov 28, 2000
4,315
0
0

I found an example but i don't understand why it's using 3 structs, can anyone explain please? Thanks!


// generic bubble sort in C++
// STL / object version Jan 00 by Amit Patel

#include <iostream.h>
#include <vector.h>

template<class Iter, class Comparator>
void mysort(Iter a, Iter b, Comparator cmp) {
for(Iter i=a; i+1 != b; ++i)
for(Iter j=b-1; j!=i; --j)
if(!cmp(*(j-1), *j)) swap(*(j-1), *j);
}

struct Range {
int lb, ub;
Range() {}
Range(int l, int u): lb(l), ub(u) {}
};

ostream& operator << (ostream& out, const Range& r) {
return out << '[' << r.lb << ',' << r.ub << ']';
}

struct LBComparator {
bool operator() (const Range& a, const Range& b) const {
return a.lb < b.lb;
}
};

struct UBComparator {
bool operator() (const Range& a, const Range& b) const {
return a.ub < b.ub;
}
};

int main() {
const int N = 10000;
vector<Range> a(N);

for(int i=0; i<N; i++)
a = Range(i*7%1000, i*13%1000);

cout << "sorting...\n"; cout.flush();

mysort(a.begin(), a.end(), LBComparator());
mysort(a.begin(), a.end(), UBComparator());

cout << "done.\n"; cout.flush();
}