vendredi 3 avril 2015

Please explain this function

The code below is used to sort the int array in ascending or descending order



//codes borrowed from learncpp.com
#include<iostream>
#include<algorithm>
using namespace std;
void SelectionSort(int *anArray, int nSize, bool(*pComparison)(int, int) {
for(int iii=0; iii<nSize; iii++) {
int nCurrent = iii;
for(int jjj=iii+1; jjj<nSize; jjj++) {
if(pComparison(anArray[nCurrent], anArray[jjj]))
nCurrent = jjj;
}
swap(anArray[nCurrent], anArray[jjj]);
}
}
bool Ascending(int nX, int nY) {
return nY>nX;
}
bool Descending(int nX, int nY) {
return nY<nX;
}
bool EvensFirst(int nX, int nY) {
if((nX%2)&&!(nY%2))
return false;
if(!(nX%2)&&(nY%2))
return true;
return Ascending(nX, nY);
}
void PrintArray(int *pArray, in nSize) {
for(int kkk=0; kkk<nSize; kkk++)
cout << pArray[kkk] << " ";
cout << endl;
}
int main() {
int anArray[9] = {3, 5, 1, 8, 9, 4, 6, 2, 7};
SelectionSort(anArray, 9, EvensFirst);
PrintArray(anArray, 9);
return 0;
}


Printing Result: 2 4 6 8 1 3 5 7 9


Can anyone here please explain how does the bool function EvensFirst work?


Aucun commentaire:

Enregistrer un commentaire