2015-04-04 66 views
1

下面的代碼被用來以升序或降序的順序int數組排序請解釋該功能

//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[iii]); 
    } 
} 
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, int 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; 
} 

打印結果是9 7 5 3 1 8 6 4 2而不是2 4 6 8 1 3 5 7 9

任何人都可以在這裏請解釋bool函數EvensFirst是如何工作的?

+1

你是否熟悉'nX%2'的作用?或者更具體地說,如果它的值爲零或不爲零,它在每種情況下意味着什麼? – lurker 2015-04-04 01:42:40

+0

不是,我感到困惑於((nX%2)&&!(nY%2)) – 2015-04-04 01:59:59

+0

'%'是模。 「%2」是模塊2(如果將數字除以2,則可以得到其餘的結果)。如果'nX%2'爲零,那麼'nX'就是偶數。如果'nX%2'非零(1),那麼'nX'是奇數。所以'(nX%2)&&! (nY%2)'如果'nX'爲奇數且'nY'爲偶數,則爲真,因爲零有一個假值,並且1具有真值。 – lurker 2015-04-04 02:01:43

回答

1

從標準報價:

裏邊反二元%運營收益第一個表達除以第二個表達式的餘數。如果/或%的第二個操作數爲零,則行爲未定義;否則(a/b)* b + a%b等於a。 如果兩個操作數都是非負的,那麼餘數是非負的;如果沒有,剩餘的符號是實現定義的(74)

(74)根據正在進行的修改ISO C的工作,優選的整數除法算法遵循ISO Fortran標準ISO/IEC 1539:1991,其中商總是趨於零。

實施例:

11 % 3 = 2,因爲11 = 3*3 + 2

現在,操作模2可以給我們只有兩個可能的結果:0或1。另外:

1)對於任何給定偶數N,N % 2將始終爲0。這是因爲偶數的定義乘法爲2.

2)對於任何給定奇數MM % 2總會給1.這是因爲任何偶數可以被定義爲2K + 1,其中KN0(自然數或零)。由於上述原因,我們可以使用x % 2表達式作爲邏輯條件,因爲在C/C++中「0爲false,其他都爲真」。

所以:

if(x%2) 
{ 
    //x%2 != 0, which means x%2 = 1, which means x is an odd number 
} 
else 
{ 
    //x%2 == 0, which means x is an even number 
} 

現在,讓我們回到你EvensFirst功能:

bool EvensFirst(int nX, int nY) 
{ 
    if((nX%2)&&!(nY%2)) 
     return false; 
    if(!(nX%2)&&(nY%2)) 
     return true; 
    return Ascending(nX, nY); 
} 

這個函數接受兩個參數:nXnY並返回true,如果nXnY前放置(否則爲false)。首先,它檢查條件(nX%2)&&!(nY%2)。這種情況本質上是指:

「檢查,如果nX是一個奇數,nY是一個偶數。

如果它的計算結果爲true,函數返回false - 平均首先被採用,所以nY應放在nX之前)。

然後,它檢查第二個條件:!(nX%2)&&(nY%2),這意味着:

「檢查,如果nX是偶數和nY是奇數」。

如果它的計算結果爲真,則函數返回true - 平均首先被採用,因此nX應放置在nY之前。

如果這兩個條件評估爲false,則意味着它們都是奇數,或者都是均等的。在這種情況下,函數使用「升序」比較來比較它們 - 將首先採用較小的數字。

1

運算符模%返回其操作數的除法的其餘部分。它可以用來檢查數字是偶數還是奇數,因爲如果x/2的剩餘部分是0這意味着x是偶數。 這意味着如果x%2=0比x是偶數,否則它是奇數。


這裏的EvensFirst如何工作

bool EvensFirst(int nX, int nY) { 
    if((nX%2)&&!(nY%2)) //if nX is even and nY is odd 
     return false; //returns false 
    if(!(nX%2)&&(nY%2)) //if nX is odd and nY is even 
     return true; //returns true 
    //if they are both odd or even returns the result of ascending 
    return Ascending(nX, nY); //true if nY > nX 
} 

這裏的你如何使用EvensFirst

void SelectionSort(int *anArray, int nSize, bool(*pComparison)(int, int) { 
    for(int iii=0; iii<nSize; iii++) { //for each number in the array 
     int nCurrent = iii; 
     for(int jjj=iii+1; jjj<nSize; jjj++) { //for each following elemnt 
      if(pComparison(anArray[nCurrent], anArray[jjj])) //compare the two elements 
       nCurrent = jjj; 
      } 
     swap(anArray[nCurrent], anArray[jjj]); //and switch their positions if they are in the wrong order 
    } 
} 
+0

雅,我只是現在修好了,thx提醒 – 2015-04-04 01:50:43

+0

我再次編輯。告訴我,如果你需要別的東西:) – 2015-04-04 02:07:35

3

該計劃最主要的是利用funtion的指針。 bool(*pComparison)(int, int)是一個指向函數的指針,該函數返回bool類型值並將其作爲參數2 int值。您可以檢查不同的輸出SelectionSort(anArray, 9, Ascending);SelectionSort(anArray, 9, Descending);(如果您已正確編碼選擇排序功能)。

注意:基於不同的函數指針,這個一般的排序例程會給出不同的輸出。例程的其餘部分是基本排序例程,將minmax的值替換爲當前元素。

bool EvensFirst(int nX, int nY) { 
    if((nX%2)&&!(nY%2)) //if nX is odd and nY is even the 
//nY should be in first position. So to change the order return false 
     return false; 
    if(!(nX%2)&&(nY%2))//if nX is even and nY is odd the 
//nX should be in first position. So to retain the order return true 
     return true; 
    return Ascending(nX, nY);// both even or both odd return the ascending function's output. 
} 

如果nx爲甚至如果我們把它由2它會給0 例如剩餘部分,12 12%2 = 0

34%2 = 0

9%2 = 1 ---->這就是爲什麼這很奇怪。

,每一個奇數號可在形式2m+1寫現在,如果我們除以2這個數字,我們將獲得1個爲第1部分的其餘部分給餘0

偶數的解釋是相同的偶數表示通過2m。因此,當除以2時,將給出餘數0。

if((nX%2)&&!(nY%2)) 
     return false; 
if nX is even it nX%2=0 and if nY is odd then ny%2=1 
So expressin becomes 
if(0 && 1)-->which evaluates to false and go to next condition. 

很少wxamples澄清你idea--

檢查x是偶數

if(x%2==0) 
    //x is even. 

Also can be written as 
if(!x%2) 
    //x is even. 

檢查,如果x是奇數

if(x%2==1) 
     // x is odd 
    Also can be written as 

    if(x%2) 
     // x is odd. 

檢查x和y均爲甚至

if(!x%2 && !y%2) 
    //both even 

檢查,如果任一個是連

if(!x%2 || !y%2) 
    //either of them is even 
+0

@JuenKhaw .:看看任何偶數除以2得到餘數爲0,任何奇數給1。不是嗎? nX%2相當於'nx is odd'。好像奇數會給1並進入循環。 – coderredoc 2015-04-04 02:10:50

+0

thx爲你解釋,這是有幫助的:D – 2015-04-04 06:32:11