Working with comporator

I have a sorting. How can I do it with Comparator?

Example of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool myfunction (int i,int j) { return (i<j); }

template <class RandomIterator, class Compare>
void StoogeSort(RandomIterator begin, RandomIterator end, Compare comp) {
int temp{ 0 };

	if (end - start + 1 > 2) {
		temp = (end - start + 1) / 3;
		StoogeSort(a, start, end - temp);
		StoogeSort(a, start + temp, end);
		StoogeSort(a, start, end - temp);
	}
	if (a[end] < a[start]) {
		temp = a[start];
		a[start] = a[end];
		a[end] = temp;
	}
}
Last edited on
You use your comparator function instead of the < operator on line 13.
You use your comparator function instead of the < operator on line 13.


Is it so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool myfunction (int i,int j) { return (i<j); }

template <class RandomIterator, class Compare>
void StoogeSort(RandomIterator begin, RandomIterator end, Compare comp) {
int temp{ 0 };

	if (end - start + 1 > 2) {
		temp = (end - start + 1) / 3;
		StoogeSort(a, start, end - temp);
		StoogeSort(a, start + temp, end);
		StoogeSort(a, start, end - temp);
	}
	if (comp) {
		temp = a[start];
		a[start] = a[end];
		a[end] = temp;
	}
}

No, it would be more like
if ( comp(a[end], a[start]) ) {
Last edited on
Topic archived. No new replies allowed.