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. AxureRP7.0各类交互效果汇总帖(转)

    了便于大家参考,我把这段时间发布分享的所有关于AxureRP7.0的原型做了整理. 以下资源均有对应的RP源文件可以下载. 当然 ,其中有部分是需要通过完成解密游戏[攻略]才能得到下载地址或者下载密码 ...

  2. pythonyCool-moviepy

    你想要登上山顶去看美丽的风光,却在山腰发现了草莓. 今天给大家推荐一个很酷的python包moviepy.我在伯乐在线发现的看这个链接: http://python.jobbole.com/81185 ...

  3. 为什么模板函数的声明和实现都放在.h文件中

    当你不使用这个模板函数或模板类,编译器并不实例化它,当你使用时,编译器需要实例化它,因为编译器是一次只能处理一个编译单元,也就是一次处理一个cpp文件,所以实例化时需要看到该模板的完整定义.所以都放在 ...

  4. kafka 集群安装与安装测试

    一.集群安装 1. Kafka下载:wget https://archive.apache.org/dist/kafka/0.8.1/kafka_2.9.2-0.8.1.tgz 解压 tar zxvf ...

  5. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  6. sqlmap使用手册

    转自:http://hi.baidu.com/xkill001/item/e6c8cd2f6e5b0a91b7326386 SQLMAP 注射工具用法 1 . 介绍1.1 要求 1.2 网应用情节 1 ...

  7. .htaccess 的写法

    RewriteCond RewriteRule 记录下现在我会的: RewriteEngine On#RewriteRule ^(.*)/(.*)/$ index.php?m=index&c= ...

  8. HDU 2836 (离散化DP+区间优化)

    Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...

  9. Android权限(转)

    http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html 访问登记属性 android.permission.ACCESS_CHEC ...

  10. Leetcode Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...