Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.

分析

Quick Sort

index   0 1 2 3 4 5 6 7 8 9 10 11 12 13
A(key=3)4 1 2 1 5 1 3 2 3 6  2  1  4  3
1       3 1 2 1 5 1 3 2 3 6  2  1  4  4 
2       3 1 2 1 1 1 3 2 3 6  2  5  4  4
3       3 1 2 1 1 1 3 2 3 6  2  5  4  4
                      j i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution {
    /**
     * @param A an integer array
     * @return void
     */
    public void sortIntegers2(int[] A) {
        // Write your code here
        quickSortHelper(A, 0, A.length - 1);
    }
    private void quickSortHelper(int[] A, int left, int right) {
        if(left >= right)
            return;
        int i = left, j = right;
        int key = A[left + (right - left) / 2];
        while(i <= j){
            while(i <= j && A[i] < key) ++i;
            while(i <= j && A[j] > key) --j;
            if(i <= j){
                int tmp = A[i];
                A[i++] = A[j];
                A[j--] = tmp;
            }
        }
        quickSortHelper(A, left, j);
        quickSortHelper(A, i, right);
    }
}

Merge Sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Solution {
    /**
     * @param A an integer array
     * @return void
     */
    public void sortIntegers2(int[] A) {
        // Write your code here
        int[] tmp = new int[A.length];
        mergeSortHelper(A, tmp, 0, A.length - 1);
    }
    private void mergeSortHelper(int[] A, int[] tmp, int left, int right) {
        if(left >= right) return;
        int mid = left + (right - left) >> 1;
        mergeSortHelper(A, tmp, left, mid);
        mergeSortHelper(A, tmp, mid + 1, right);
        merge(A, tmp, left, mid, right);
    }
     
    private void merge(int[] A, int[] tmp, int left, int mid, int right){
        int i = left, j = mid + 1, index = left;
        while(i <= mid || j <= right){
            int a, b, min;
            a = (i <= mid) ?A[i] : Integer.MAX_VALUE;
            b = (j <= right) ? A[j] : Integer.MAX_VALUE;
            min = (a <= b) ?A[i++] : A[j++];
            tmp[index++] = min;
        }
        for(int k = left; k <= right; k++){
            A[k] = tmp[k];
        }
    }
}

Sort Integers II的更多相关文章

  1. [LintCode] Sort Integers II 整数排序之二

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

  2. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  3. [LintCode] Sort Integers 整数排序

    Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...

  4. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  5. Wiggle Sort I & II

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  6. Sort Integers

    ) algorithm. 分析 bubble sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Solution ...

  7. 143. Sort Colors II

    最后更新 一刷 class Solution { public void sortColors2(int[] colors, int k) { // write your code here if ( ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

随机推荐

  1. Pandas v0.23.4手册汉化

    Pandas手册汉化 此页面概述了所有公共pandas对象,函数和方法.pandas.*命名空间中公开的所有类和函数都是公共的. 一些子包是公共的,其中包括pandas.errors, pandas. ...

  2. Ubuntu系统python3版本设置问题

    参照:https://blog.csdn.net/wangguchao/article/details/82151372

  3. 第七章移动互联网与移动IP

    第七章移动互联网与移动IP 本章延续前几章节,对该章节内容进行归纳总结. 文章中的Why表示产生的背景,也就是说为什么会产生该技术,What表示该技术是什么,How表示该技术是如何使用的.以下将用字母 ...

  4. 第一篇:一天学会MongoDB数据库之Python操作

    本文仅仅学习使用,转自:https://www.cnblogs.com/suoning/p/6759367.html#3682005 里面新增了如果用用Python代码进行增删改查 什么是MongoD ...

  5. 解决Sublime Text 3中文显示乱码(tab中文方块)问题

    博客分类:  Sublime   一.文本出现中文乱码问题 1.打开Sublime Text 3,按Ctrl+-打开控制行,复制粘贴以下python代码,然后回车运行. 2. 复制并粘贴如下代码: P ...

  6. Python解包参数列表及 Lambda 表达式

    解包参数列表 当参数已经在python列表或元组中但需要为需要单独位置参数的函数调用解包时,会发生相反的情况.例如,内置的 range() 函数需要单独的 start 和 stop 参数.如果它们不能 ...

  7. 吴恩达 Deep learning 第一周 深度学习概论

    知识点 1. Relu(Rectified Liner Uints 整流线性单元)激活函数:max(0,z) 神经网络中常用ReLU激活函数,与机器学习课程里面提到的sigmoid激活函数相比有以下优 ...

  8. 兰亭集势股价疯涨背后:物流成外贸B2C发展掣肘

    21世纪经济报道 汤浔芳 北京报道 核心提示:“兰亭集势涨势喜人,这样的增长是这两三年中概股没有出现过的.”一位负责美股投资的基金合伙人告诉记者,此前,中概股比较低迷,持续大幅度上涨,难得一见. 在唯 ...

  9. linux 的 awk 使用

    linux中awk命令对文本内容进行操作,其功能十分强大 1.如:查看一个有几百万行内容的文件中第3列数字内容(不重复) cat test.csv | awk -F ',' '{print $3}' ...

  10. 5月5号周二课堂练习:简评cnblogs.com的用户体验

    一.用户类型 在博客园上写博客,提问题,浏览感兴趣的博客帖子的活跃用户. 二.对cnblogs的期望 在博客园上写博客更流畅,制作手机版的APP可以随时随地在线浏览大牛们写的博客,提出的问题能更好的更 ...