Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.

 
Example

Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].

快速排序是排序算法中比较重要一种,也是经常容易考的一种排序算法,务必要掌握好。快排的优点是其平均时间复杂度为O(nlgn),这样在给大数据集排序的时候,其效率明显比一些O(n2)的算法要高很多。快排主要有三步:

1. 选定中枢点pivot value,中枢点可以选择中间那个点,也可以选末尾点,一般来说我们直接选取末尾点,如果选末尾点的话注意循环结束后要调换中枢点和第一个大于中枢点的数的位置。理论上来说中枢点可以为任意值,即使这个值不在数组上存在也无妨。

2. 分割Partition,重新安排数字的位置,让所有比中枢点小的数到左半边,所有大于中枢点的书到右半边,等于中枢点的数可以在任意一边,这样数组就被分为了两段,注意两段的长度可以不相等。

3. 分别给每段排序,调用递归算法给每一段排序。

下面这个图是用来说明步骤二,分割的过程,这是快排的核心,只要这一步搞清楚了,基本了快排就能掌握了。

(from http://www.algolist.net/Algorithms/Sorting/Quicksort)

解法一:

// Quick sort
class Solution {
public:
/**
* @param A an integer array
* @return void
*/
void sortIntegers2(vector<int>& A) {
quick_sort(A, , A.size() - );
}
void quick_sort(vector<int> &A, int start, int end) {
if (start >= end) return;
int pos = partition(A, start, end);
quick_sort(A, start, pos - );
quick_sort(A, pos, end);
}
int partition(vector<int>& A, int start, int end) {
int i = start, j = end, pivot = A[end];
while (i <= j) {
while (A[i] < pivot) ++i;
while (A[j] > pivot) --j;
if (i <= j) {
swap(A[i], A[j]);
++i; --j;
}
}
swap(A[i], A[end]);
return i;
}
};

下面这种方法的快排跟上面的一点不同在于partition函数,上面使用的是while循环下面这种使用的是for循环,看个人爱好了,两种都行:

解法二:

// Quick sort
class Solution {
public:
/**
* @param A an integer array
* @return void
*/
void sortIntegers2(vector<int>& A) {
quick_sort(A, , A.size() - );
}
void quick_sort(vector<int> &A, int start, int end) {
if (start >= end) return;
int pos = partition(A, start, end);
quick_sort(A, start, pos - );
quick_sort(A, pos, end);
}
int partition(vector<int>& A, int start, int end) {
int i = start - , pivot = A[end];
for (int j = start; j < end; ++j) {
if (A[j] <= pivot) {
++i;
swap(A[i], A[j]);
}
}
swap(A[i + ], A[end]);
return i + ;
}
};

类似题目:

Sort Integers

[LintCode] Sort Integers II 整数排序之二的更多相关文章

  1. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  2. [LintCode] Sort Integers 整数排序

    Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...

  3. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  4. [LeetCode] Wiggle Sort II 摆动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  5. [LintCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  6. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  7. Sort Integers II

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

  8. bitmap对海量无重复的整数排序--转

    原文地址:http://blog.csdn.net/u013074465/article/details/46956295 现在有n个无重复的正整数(n 小于10的7次方),如果内存限制在1.5M以内 ...

  9. C语言:10个整数排序(别忘了负数)

    题目内容: 10个整数排序(别忘了负数) 例如 input 1 0 2 0 3 4 1 9 8 7 output 0 0 1 1 2 3 4 7 8 9 编码: void sort(int *a); ...

随机推荐

  1. 禁用编译器自动生成的函数(Effective C++之06)

    如果想让你的类定义出来的对象是独一无二的,即对象无法被复制,或者使用赋值操作符赋给另外一个对象,那么最好的方法就是禁用拷贝构造函数和赋值操作符.下面介绍几种禁用的方法.(方法来自Effective C ...

  2. 学习设计接口api(转)

    介绍 先说说啥是 Api 吧,以下摘自百度百科: API (Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于 ...

  3. c文件操作 (转)

    文件文件的基本概念 所谓“文件”是指一组相关数据的有序集合. 这个数据集有一个名称,叫做文件名. 实际上在前面的各章中我们已经多次使用了文件,例如源程序文件.目标文件.可执行文件.库文件 (头文件)等 ...

  4. MySQL5.5出面ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)问题的解决办法

    问题描述 安装完MySQL5.5数据库,使用Navicat Premium以及命令窗口连接数据库都报以下错误: ERROR 1045 (28000): Access denied for user ' ...

  5. 在windows系统的文件右键菜单中增加“命令提示符”

    本实用小工具能够在windows系统的文件右键菜单中增加“命令提示符”,方便快速进入制定文件的命令提示窗口,避免逐层输入或复制文件夹路径,极其实用. 工具下载地址如下:360云盘(访问密码:5b71) ...

  6. loj 1167(二分+最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26881 思路:我们可以二分最大危险度,然后建图,由于每个休息点只能 ...

  7. C语言基本点初探

    1,对于int a=10++;此语句错误,为什么呢,对于i++来说,i是一个变量,是把i加1然后赋值给i,然而10不是一个变量所以无法执行加加的语法; 2,运算符的优先级: 赋值运算符<逻辑运算 ...

  8. Linux内核system_call中断处理过程

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...

  9. 【T_SQL】 基础

    一.T-SQL 的组成 1.DML(数据操作语言 Data Manipulation Language)               查询.插入.删除和修改数据库中的数据.SELECT.INSERT. ...

  10. 笑谈Android图表-MPAndroidChart

    MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活.MPA ...