[LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.
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 + ;
}
};
类似题目:
[LintCode] Sort Integers II 整数排序之二的更多相关文章
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- [LintCode] Sort Integers 整数排序
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LeetCode] Wiggle Sort II 摆动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [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 ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Sort Integers II
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- bitmap对海量无重复的整数排序--转
原文地址:http://blog.csdn.net/u013074465/article/details/46956295 现在有n个无重复的正整数(n 小于10的7次方),如果内存限制在1.5M以内 ...
- 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); ...
随机推荐
- javascript中的true和false
今天遇到一个问题,执行下面的代码返回true还是false?请说明理由 console.log([] == ![]) 在浏览器中运行了一下,发现结果是true.为什么会这样呢?于是查找了相关的资料. ...
- html table之 全选,全不选
就是这个小功能让我和组长引发争端,就是这个小功能让我差点"被"辞职,就是这个自封装的js方法让我放下了对组长的敬畏之心,现在分享一下,其实也很简单,但是真的有这么简单吗? < ...
- jdk 1.8 Executors
Class Executors java.lang.Object java.util.concurrent.Executors public class Executors extends Objec ...
- mysql 怎样清空一个数据库中的所有表
转自:http://blog.csdn.net/zhangzhizhen1988/article/details/8432146 MySQL清空表是很重要的操作,也是最常见的操作之一,下面就为您详细介 ...
- Android自动化压力测试之Monkey Test Android常见的错误类型及黑白名单的使用方法(四)
Android常见的错误类型有两种 1.ANR类型 1)在5秒内没有响应输入的事件(例如,按键按下,屏幕触摸) 2)BroadcastReceiver在10秒内没有执行完毕 2.Crash类型 1)异 ...
- python特殊函数 __call__()
__call__ 在Python中,函数其实是一个对象: >>> f = abs >>> f.__name__ 'abs' >>> f(-123) ...
- hdu2859 dp
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2859 题意:输入一个数n,接下来是一个由n*n个字母组成的矩阵,求以左下到右上的线为轴的最 ...
- ReportNg 测试报告的定制修改【转】
前言 前段时间在Testerhome上面看到了测试报告生成系列之-------如何用 testNG 生成测试报告 简单的描述了一些测试报告的生成,接着有人在评论中回复说可以针对reportNg的测试报 ...
- JS小函数
join()\toString(): join()函数和toString()函数都是返回字符串类型. 针对一个数组: var arr = ["I","love" ...
- BestCoder Round #71 (div.2)
数学 1001 KK's Steel 类似斐波那契求和 #include <cstdio> #include <cstring> #include <algorithm& ...