排序算法

所有的排序算法工作在一个类似的概念:排序算法走过一堆数字,并对数字比较,并基于这些比较的结果重新排序的数。因此,通过不同的比较(它可以是一个函数),我们可以改变函数类型不影响分类编码其余的路。

这是我们的选择排序例程从以前的教训:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <algorithm> // for swap
 
// Note our user-defined comparison is the third parameter
void SelectionSort(int *anArray, int nSize, bool (*pComparison)(int, int))
{
    using namespace std;
    for (int nStartIndex= 0; nStartIndex < nSize; nStartIndex++)
    {
        int nBestIndex = nStartIndex;
 
        // Search through every element starting at nStartIndex+1
        for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++)
        {
            // Note that we are using the user-defined comparison here
            if (pComparison(anArray[nCurrentIndex], anArray[nBestIndex])) // COMPARISON DONE HERE
                nBestIndex = nCurrentIndex;
        }
 
        // Swap our start element with our best element
        swap(anArray[nStartIndex], anArray[nBestIndex]);
    }
}
 
// Here is a comparison function that sorts in ascending order
// (Note: it's exactly the same as the previous Ascending() function)
bool Ascending(int nX, int nY)
{
    return nY > nX;
}
 
// Here is a comparison function that sorts in descending order
bool Descending(int nX, int nY)
{
    return nY < nX;
}
 
// This function prints out the values in the array
void PrintArray(int *pArray, int nSize)
{
    for (int iii=0; iii < nSize; iii++)
        cout << pArray[iii] << " ";
    cout << endl;
}
 
int main()
{
    using namespace std;
 
    int anArray[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 };
 
    // Sort the array in descending order using the Descending() function
    SelectionSort(anArray, 9, Descending);
    PrintArray(anArray, 9);
 
    // Sort the array in ascending order using the Ascending() function
    SelectionSort(anArray, 9, Ascending);
    PrintArray(anArray, 9);
 
    return 0;

这个程序产生的结果:

9 8 7 6 5 4 3 2 1

1 2 3 4 5 6 7 8 9

是,很酷吧?我们给来电者能够控制我们的选择排序做它的工作。

来电者甚至可以定义自己的“奇怪”的比较函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool EvensFirst(int nX, int nY)
{
        // if nX is not even and nY is, nY goes first
    if ((nX % 2) && !(nY % 2))
        return false;
 
        // if nX is even and nY is not, nX goes first
    if (!(nX % 2) && (nY % 2))
        return true;
 
        // otherwise sort in Ascending order
    return Ascending(nX, nY);
}
 
int main()
{
    using namespace std;
 
    int anArray[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 };
 
    SelectionSort(anArray, 9, EvensFirst);
    PrintArray(anArray, 9);
 
    return 0;
}

上面的代码片段中产生以下结果:

2 4 6 8 1 3 5 7 9

正如你所看到的,使用函数指针在这方面提供了一个很好的办法让来电者“钩”,它的功能是你以前的编写和测试,这有助于促进代码重用!以前,如果你想在一个数组排序降序和升序,你需要多个版本的排序例程。现在你可以有一个版本,可以任何方式调用方的欲望!

漂亮的函数指针

让我们面对它的指针功能语法是丑陋的。然而,类型可以用于函数指针看起来更像普通变量:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值