快速排序与c++sort排序比较

大家好,我是小码,最近我发现有些同学将快排与sort函数弄混了,我在这里简单介绍一下。快速排序详见上一篇博客。

快速排序_-CSDN博客

sort函数介绍

std::sort 是C++标准库中的一个通用排序算法,定义在 <algorithm> 头文件中。它用于对序列进行排序,默认按照升序排列,但可以通过自定义比较函数实现降序或其他排序规则。std::sort 接受两个迭代器参数 firstlast,分别指向序列的起始位置和结束位置(不包括 last),并对该范围内的元素进行排序。如果需要自定义排序规则,可以传递第三个参数——一个比较函数或比较器。std::sort 的时间复杂度通常为 O(nlogn),但需要注意它不是稳定排序算法,即相等元素的相对顺序可能会改变。如果需要稳定排序,应使用 std::stable_sort。

std::sort 通常采用一种混合排序算法,结合了快速排序、堆排序和插入排序的优点,以确保在各种情况下都能达到较好的性能。混合排序算法(IntroSort)是其保持 O(nlogn) 时间复杂度的关键机制:
std::sort 在许多标准库实现中使用了 IntroSort 算法。IntroSort 是一种自适应排序算法,结合了快速排序、堆排序和插入排序的优点:
快速排序:快速排序的平均时间复杂度为 O(nlogn),但在最坏情况下(如输入序列已经接近有序时)会退化为 O(n2)。
堆排序:当快速排序的递归深度超过某个阈值(通常是 2logn)时,算法会切换到堆排序。堆排序的时间复杂度为 O(nlogn),且不会退化。
插入排序:对于小规模数据(如小于某个阈值,通常是16或32个元素),std::sort 会切换到插入排序。插入排序在小规模数据上效率较高,时间复杂度为 O(n2),但在小数据量时性能较好。
通过这种混合策略,std::sort 避免了快速排序在最坏情况下的性能退化,同时利用了插入排序在小规模数据上的优势,从而在各种输入情况下都能保持接近 O(nlogn) 的时间复杂度。

调用sort函数时有以下两种形式:

void sort(Iterator first, Iterator last);
void sort(Iterator first, Iterator last, Compare comp);

comp(可选):是一个比较函数,用于定义排序的规则。如果不提供 comp,则默认使用 < 运算符进行升序排序。

对数组排序:

#include <iostream>
#include <algorithm> 

int main() {
    int arr[] = {5, 2, 9, 1, 5, 6};
    int n = sizeof(arr) / sizeof(arr[0]); // 计算数组大小

    // 使用 sort 对数组进行排序
    std::sort(arr, arr + n); // arr 是数组的起始地址,arr + n 是数组的结束地址

    // 输出排序后的数组
    for (int i = 0; i < n; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

对vector排序:

#include <iostream>
#include <vector>
#include <algorithm> 

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 使用 std::sort 对 vector 进行排序
    std::sort(vec.begin(), vec.end());

    // 输出排序后的 vector
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

对deque排序:

#include <iostream>
#include <deque>
#include <algorithm> 

int main() {
    std::deque<int> deq = {5, 2, 9, 1, 5, 6};

    // 使用 std::sort 对 deque 进行排序
    std::sort(deq.begin(), deq.end());

    // 输出排序后的 deque
    for (int num : deq) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

自定义排序

按照绝对值大小进行排序

#include <iostream>
#include <vector>
#include <algorithm>

// 自定义的比较函数,按照元素的绝对值大小进行比较
bool compare_abs(int a, int b) {
    return abs(a) < abs(b);
}

int main() {
    std::vector<int> vec = {3, -1, 4, -1, 5, 9, -2, 6};

    // 使用自定义的比较函数对数组进行排序
    std::sort(vec.begin(), vec.end(), compare_abs);

    // 输出排序后的数组
    std::cout << "排序后的数组:";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

数组按照降序排序:

#include <iostream>
#include <algorithm> 

int main() {
    int arr[] = {5, 2, 9, 1, 5, 6};
    int n = sizeof(arr) / sizeof(arr[0]); // 计算数组大小

    // 使用 std::sort 进行降序排序
    std::sort(arr, arr + n, [](int a, int b) {
        return a > b; // 比较函数,降序
    });

    // 输出排序后的数组
    for (int i = 0; i < n; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

自定义结构体降序排序:

#include <iostream>
#include <vector>
#include <algorithm> 

struct Person {
    std::string name;
    int age;
};

int main() {
    std::vector<Person> people = {
  {"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};

    // 按年龄降序排序
    std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
        return a.age > b.age; // 比较函数,降序
    });

    // 输出排序后的结果
    for (const auto& person : people) {
        std::cout << person.name << " " << person.age << std::endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值