美文网首页
C++快速排序算法,用到了函数指针的代码

C++快速排序算法,用到了函数指针的代码

作者: hahamama | 来源:发表于2022-02-23 22:34 被阅读0次

把写内容过程中经常用的内容记录起来,如下内容是关于C++快速排序算法,用到了函数指针的内容。

#include <iostream>

#include <algorithm>

#include <string>

#include <cctype>

struct example{

    int id;

    std::string name;

};

template <class T>

template <class T>

bool intCheck(int a, int b){

    return (a < b);

}

bool stringCheck(std::string a, std::string b){

    for(int i = 0; i < a.length(); i++)

        a[i] = tolower(a[i]);

    for(int i = 0; i < b.length(); i++)

        b[i] = tolower(b[i]);

    if(strcmp(a.c_str(), b.c_str()) >= 0)

        return false;

    else return true;

}

bool exampleCheck(example a, example b){

    if(strcmp(a.name.c_str(), b.name.c_str()) >= 0) return false;

    else return true;

}

    int iArray[] = { 2, 1, 56, 213, 2, 32, 32216, 14 };

    std::string sArray[] = { "Hello", "how are you?", "elephant", "aaah!", "zzzzz", "queen" };

    example structArray[] = { {1, "Joe"},

                              {4, "Billy"},

                              {2, "Zander"},

                              {3, "Tom"} };

    quickSort(iArray, 0, 7, intCheck);

    quickSort(sArray, 0, 5, stringCheck);

    quickSort(structArray, 0, 3, exampleCheck);

    std::cout << "Integers:" << std::endl;

    for(int i = 0; i < 8;i++)

        std::cout << 't' << iArray[i] << std::endl;

    std::cout << std::endl << "String:" << std::endl;

    for(int i = 0; i < 6;i++)

        std::cout << 't' << sArray[i] << std::endl;

    std::cout << std::endl << "Structure (By name):" << std::endl;

    for(int i = 0; i < 4;i++)

        std::cout << "t{ " << structArray[i].id << ", " << structArray[i].name << " }" << std::endl;

    std::cin.get();

    return 0;

}

template <class T>

    int pos = l;

    std::swap(uA[r], uA[pos]);

    if (l < r){

        std::swap(uA[r], uA[pos]);

        }

    return pos;

}

template <class T>

    if(r > l){

        int pos = partition( uA, l, r, less );

        quickSort( uA, l, pos-1, less);

        quickSort( uA, pos+1, r, less);

    }

}

相关文章

  • C++快速排序算法,用到了函数指针的代码

    把写内容过程中经常用的内容记录起来,如下内容是关于C++快速排序算法,用到了函数指针的内容。 #include #...

  • 七大排序算法之快速排序

    七大排序算法之快速排序 @(算法笔记)[排序算法, 快速排序, C++实现] [TOC] 快速排序的介绍: 快速排...

  • 各种排序算法实现

    C++实现各种排序算法。上张图。 自定义的swap函数。 冒泡排序 插入排序 希尔排序 选择排序 快速排序 归并排...

  • 快速排序的递归和非递归实现

    排序算法中很重要的快速排序 递归实现方式 递归实现方式的不同在于分区函数的不同 双向循环指针式,原理是利用左右指针...

  • 无标题文章

    划分算法与快速排序 划分算法与快速排序 划分算法由两个指针来完成,这两个指针分别指向数组的两头,左指针left向右...

  • 排序算法1: 冒泡排序算法

    C++ 冒泡排序算法的实例源代码,一些排序方法的代码集锦,该函数模板使用冒泡法对集合元素进行排序,参数说明: co...

  • IOS常见算法

    常见算法: 快速排序: 选择排序: 冒泡排序: 测试代码:

  • C++ 常用代码

    vector 迭代器遍历 C++ 函数模板 冒泡排序 快速排序

  • 排序题

    公共函数 选择排序 冒泡排序 插入排序 快速排序 归并排序——迭代算法

  • 常见排序算法

    希尔排序,快速排序,堆排序,2路归并算法的c++简单实现 在 里面写了一个随机数列生成,可以快速验证算法的正确性 ...

网友评论

      本文标题:C++快速排序算法,用到了函数指针的代码

      本文链接:https://www.haomeiwen.com/subject/ubpalrtx.html