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)

解法一:

  1. // Quick sort
  2. class Solution {
  3. public:
  4. /**
  5. * @param A an integer array
  6. * @return void
  7. */
  8. void sortIntegers2(vector<int>& A) {
  9. quick_sort(A, , A.size() - );
  10. }
  11. void quick_sort(vector<int> &A, int start, int end) {
  12. if (start >= end) return;
  13. int pos = partition(A, start, end);
  14. quick_sort(A, start, pos - );
  15. quick_sort(A, pos, end);
  16. }
  17. int partition(vector<int>& A, int start, int end) {
  18. int i = start, j = end, pivot = A[end];
  19. while (i <= j) {
  20. while (A[i] < pivot) ++i;
  21. while (A[j] > pivot) --j;
  22. if (i <= j) {
  23. swap(A[i], A[j]);
  24. ++i; --j;
  25. }
  26. }
  27. swap(A[i], A[end]);
  28. return i;
  29. }
  30. };

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

解法二:

  1. // Quick sort
  2. class Solution {
  3. public:
  4. /**
  5. * @param A an integer array
  6. * @return void
  7. */
  8. void sortIntegers2(vector<int>& A) {
  9. quick_sort(A, , A.size() - );
  10. }
  11. void quick_sort(vector<int> &A, int start, int end) {
  12. if (start >= end) return;
  13. int pos = partition(A, start, end);
  14. quick_sort(A, start, pos - );
  15. quick_sort(A, pos, end);
  16. }
  17. int partition(vector<int>& A, int start, int end) {
  18. int i = start - , pivot = A[end];
  19. for (int j = start; j < end; ++j) {
  20. if (A[j] <= pivot) {
  21. ++i;
  22. swap(A[i], A[j]);
  23. }
  24. }
  25. swap(A[i + ], A[end]);
  26. return i + ;
  27. }
  28. };

类似题目:

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. php随机生成验证码

    我们经常需要服务器向前端发送验证码,验证码需要随机产生,下面的用简单的代码实现了这一过程: <?php $pool='0123456789abcdefghijklmnopqrstuvwxyzAB ...

  2. VR 相关专业词汇

    最近在看 SIGGRAPH2015 有关 VR Display and Interaction 的几篇文章,之前从来没看过有关方面的 paper,一看才发现专业词汇太多了,根本不懂啊,幸亏 Paper ...

  3. MyBatis传入参数与parameterType

    参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...

  4. 提高Axure设计效率的10条建议 (转)

    Axure 是创建软件原型的快速有力的工具.上手很容易,但是,其中存在一个危险.这款软件是如此的直观以至于很多用户可以在没有接受过任何正式培训的情况下进行使用.他们可能不知道的是他们可能没有以恰当的方 ...

  5. ListView遍历每个Item出现NullPointerException的异常(转)

    在使用ListView过程中我们有时候需要遍历取得每个Item项中的一些数据(比如每个Item里面有TextView,需要获取它的文本等等),但是我们在遍历过程中经常会遇到NullPointerExc ...

  6. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  7. JQuery 定时器 (Jquery Timer 插件)

      jQuery Timers插件地址: http://plugins.jquery.com/project/timers JQuery Timers应用知识提供了三个函式1. everyTime(时 ...

  8. hdu1078 dp(递推)+搜索

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1078 题意:老鼠从(1.1)点出发,每次最多只能走K步,而且下一步走的位置的值必须必当前值 ...

  9. hdu2191 多重背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2191 多重背包:有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是 ...

  10. react-基础(1)

    props 创建组件 React.createClass; 直接继承React.Component;与上面不同的是初始化props和state的方法: export class Counter ext ...