[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); ...
随机推荐
- javase基础笔记2——数据类型和面向对象
API:Application program interface 程序调用一个方法去实现一个功能 正则表达式:regex 用来匹配的 javaEE里边有三大框架 SSH struts spring ...
- Arduino101学习笔记(九)—— 中断函数
1.设置中断函数 //***************************************************************************************** ...
- hdfs 名称节点和数据节点
名字节点(NameNode )是HDFS主从结构中主节点上运行的主要进程,它指导主从结构中的从节点,数据节点(DataNode)执行底层的I/O任务. 名字节点是HDFS的书记员,维护着整个文件系统的 ...
- Python入门神图
国外某小哥制作的Python入门神图
- 【前台 ajax】web项目前台传递数组给后台 两种方式
项目使用maven springMVC 有需求 将前台的数组 在ajax中 送给后台 方式1: 前台代码:[注意:ajax中的属性---traditional:true, ] 如果Post ...
- Linux 环境中普通用户启动Myeclipse出错
将Myeclipse安装在/usr/local/myeclipse目录中,由root用户启动时没有问题,而用普通用户时出现如下故障: The configuration area at '/usr/l ...
- iOS Outlets Referencing Outlets
一位网友的解释: 原址:http://www.cocoachina.com/bbs/read.php?tid-21295.html
- three.js 场景入门
<!DOCTYPE html> <html> <head> <title>Example 01.02 - First Scene</title&g ...
- details和summary
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ccc 多点触控
研究了一天,多点触控的点无法保存,只能模拟多点触控了 cc.Class({ extends: cc.Component, properties: { wheelStick:{ default:null ...