微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

函数指针对数组进行排序

如何解决函数指针对数组进行排序

我有一个练习来实现两个函数ascendingSort,descendingSort和函数 mySort

#include<iostream>
#include <math.h>
using namespace std;

void ascendingSort(int a[],int n) {
     //Todo
}

void descendingSort(int a[],int n) {
    //Todo
}

void mySort(int a[],int n,void (*sort)(int[],int)) {
    /*
     * STUDENT ANSWER
     * Todo: sort array based on sort algorithm of function sort. 
     */
}

int main() {
    int n = 5;
    int a[5] = { 1,2,3,4,5 };
    void (*sortAlgorithm)(int[],int) = descendingSort;
    
    mySort(a,n,sortAlgorithm);
    for (int i = 0; i < n; ++i) {
    printf("%d ",a[i]);
    }
    return 0;
}

我可以实现两种排序功能。但我不知道如何编写函数 mySort 你能给我任何想法吗?谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。