剑指Offer-排序:快排

来源:互联网 发布:mac ps滤镜怎么安装 编辑:程序博客网 时间:2024/06/05 20:22

题目:

实现快排。

链接:

剑指Offer(第2版):P80

思路标签:

算法:递归

解答:

1. C++

  • 快排是基于递归操作实现的;
  • 首先从数组中随机选择一个数作为基准,与末尾的元素进行交换,放到最后一个元素的位置,方便后期比较操作;
  • 设置一个小于基准和大于基准的边界,边界一直为小于基准的最后一个数,初始设置为start-1;
  • 遍历数组,小于基准的数则交换放到边界的前面,大于基准的数则可以不变;
  • 将基准放到中间位置;
  • 上面的步骤为一次实现,称为分区(partition)操作;
  • 递归地实现左、右两个子数组的分区操作,则最终实现排序。
  • 时间复杂度:最差情况O(n^2),最佳情况O(nlogn),平均情况O(nlogn)
void swap(int* data, int i, int j){    if(i == j) return;    int temp = data[i];    data[i] = data[j];    data[j] = temp;}// 分区操作int partition(int data[], int length, int start, int end){    if(data == nullptr || length <= 0 || start < 0 || end >= length)        throw new std::exception("Invalid Parameter");    int index = rand() % (end - start + 1) + start; // 随机选择基准    swap(data, index, end);    int small = start - 1;    for(index = start; index < end; ++index){        if(data[index] < data[end]){            ++small;            if(small != index)                swap(data, small, index);        }    }    ++small;    swap(data, small, end);    return small;}void quickSort(int data[], int length, int start, int end){    if(start == end) return;    int index = partition(data, length, start, end);    if(index > start)        quickSort(data, length, start, index-1);    if(index < end)        quickSort(data, length, index+1, end);}

2. python

def quick_sort(lists, left, right):    # 快速排序    if left >= right:        return lists    key = lists[left]    low = left    high = right    while left < right:        while left < right and lists[right] >= key:            right -= 1        lists[left] = lists[right]        while left < right and lists[left] <= key:            left += 1        lists[right] = lists[left]    lists[right] = key    quick_sort(lists, low, left - 1)    quick_sort(lists, left + 1, high)    return lists

排序相关

python:

  • http://python.jobbole.com/82270/

动态图:

  • http://web.jobbole.com/87968/

其他相关问题

题目: 公司员工年龄排序

几万名员工,实现排序算法,要求时间效率O(n),可以使用常量大小辅助空间,不超过O(n)。年龄范围在0~99之间。

分析:

  • 要求时间效率为O(n),所以这也说明了需要使用额外的空间才能实现。
  • 题目中仅要求对员工的年龄进行排序即可。
void sortAges(int ages[], int length){    if(ages == nullptr || length <= 0) return;    const int oldestAge = 99;    int timesOfAge[oldestAge + 1];    for(int i=0; i<=oldestAge; ++i)        timesOfAge[i] = 0;    for(int i=0; i<length; ++i){        int age = ages[i];        if(age < 0 || age > oldestAge)            throw new std::exception("Age of out range!");        ++timesOfAge[age];    }    int index = 0;    for(int i=0; i<=oldestAge; ++i){        for(int j=0; j<timesOfAge[i]; ++j){            ages[index] = i;            ++index;        }    }}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 类克价格 益赛普类克 类克和益赛普哪个好 类克援助 英夫利西单抗 报考类别 分类别 考生类别 行业类别 职业类别 类别 人员类别 工程类别 职位类别 身份类别 会计学专业类别 考研报考类别 会计专业类别 研究生报考类别 户口类别怎么填 人员类别怎么填 报考类别定向非定向 学生类别怎么填 户籍类别怎么填 行业类别怎么填 考生类别怎么填 职业类别分类表 研究生报考类别怎么填 2018新商标类别明细45类 户籍类别怎么填写 工程类别划分标准 衣服安全类别 分类别的作用 身份类别怎么填 类别是什么意思 财务管理专业类别 2018营业执照类别明细45类图 电子商务专业类别 物流管理专业类别 工商管理专业类别 考研报名报考类别