Ranjitha
Ranjitha

Reputation: 81

Sorting of array elements

void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare_func );

How does the Compare_func work? I want to trace it for each input. Sort in increasing order:

bool Compare_func(int i,int j) 
{ 
    return (i<j); 
}

How are the parameters passed to Compare_func? E.g. for

array = {12,40,1,36};

Upvotes: 0

Views: 97

Answers (3)

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

Check for yourself:

bool Compare_func(int i,int j) 
{ 
    std::cout << "Comparing " << i << " with " << j << '\n';
    return (i<j); 
}

Just be aware that the results are only applicable to your specific implementation.

Upvotes: 1

Charles Brunet
Charles Brunet

Reputation: 23120

Sorting algorithm could be any possible algorithm. You don't need to know which one is used (bubble sort, quicksort, etc.). The algorithm will successively choose a pair of data, and compare them, then decide if it moves them or not. At each comparison, your compare function will tell to the algorithm if first element is smaller than second, or not. Sorting function will just receive two values pointed by the iterator.

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308158

The sort algorithm will call the comparison function multiple times, each time with two entries from your array. You don't know and shouldn't care how many times or what order your comparison function will be called.

Upvotes: 4

Related Questions