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. 背景虚化 Google Camera App Nokia Refocus HTC One M8 的 Duo景深相机

    背景虚化是单反中一种比较常见的拍照形式,参看 http://www.techbang.com/posts/%2017842 https://refocus.nokia.com/

  2. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

  3. UI背景构建

    个中原因不是很明白 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=& ...

  4. Redis学习笔记(2) Redis基础类型及命令之一

    1. 基础命令 (1) 获取符合规则的键名列表 格式为:KEYS pattern 其中pattern表示支持通配符 # 建立一个名为bar的键 > SET bar OK # 获取Redis所有键 ...

  5. C# 生成随机数

    private static char[] constant = { ', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p ...

  6. poj2533 LIS

    题目链接: http://poj.org/problem?id=2533 题意:第一个数n,接下来n个数,>  ....求最长上升子序列. 这道题有两种解法,第一种是通解,也适用于别的LIS. ...

  7. [POJ1015]Jury Compromise

    题目大意:要求你从n个人中选出m个,每个人有两个值p[i],D[i],要求选出的人p总和与D总和的差值最小.若有相同解,则输出p总+D总最大的方案. 动态规划. 一直在想到底是n枚举外面还是m放外面, ...

  8. 链表 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)

    题目传送门 题意:训练指南P244 分析:链表模拟,维护链表的head和tail指针 #include <bits/stdc++.h> using namespace std; const ...

  9. iOS 为类添加Xib里面配置的view

    创建Empty文件,最好与其Controller同名, 在File's Owner的类属性里面指明其所属类(或者说它是个什么Controller), 从File's Owner右键拖向内部创建的视图( ...

  10. ASP.NET Core EF Sample

    Install EF Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityF ...