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. 强化学习读书笔记 - 11 - off-policy的近似方法

    强化学习读书笔记 - 11 - off-policy的近似方法 学习笔记: Reinforcement Learning: An Introduction, Richard S. Sutton and ...

  2. Phaser3跟随自定义路径移动的赛车 -- iFIERO游戏教程

      racingcar 在线预览:http://www.ifiero.com/uploads/phaser/pathrotate/代码: var config = { type: Phaser.AUT ...

  3. python程序设计——面向对象程序设计:继承

    继承是为代码复用和设计复用而设计的 在继承关系中,已有的.设计好的类称为父类或基类,新设计的类为子类或派生类 派生类可以继承父类的公有成员,但不能继承其私有成员 如果需要在派生类中调用基类的方法,可以 ...

  4. Switch Game :因子数

    A - Switch Game Problem Description There are many lamps in a line. All of them are off at first. A ...

  5. 【Pthon入门学习】多级菜单小例子

    menu_list = { '北京':{ '昌平':{ '回龙观':{ '和谐家园':{}, '矩阵小区':{}, '北店家园':{} }, '沙河':{ '北街家园1区':{}, '北街家园2区': ...

  6. 关于js中一个对象当做参数传递是按值传递还是按引用传递的个人看法

    在<JavaScript高级程序设计>这本书中有这样一段话:有很多开发人员错误的认为:在局部作用域中修改的对象会在全局作用域中反映出来,就说明参数是按引用传递的.换句话说,尼古拉认为当一个 ...

  7. Intense Heat(前缀和或尺取)

    The heat during the last few days has been really intense. Scientists from all over the Berland stud ...

  8. 20181120-10 Beta阶段第2周/共2周 Scrum立会报告+燃尽图 7

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2415] 版本控制地址    [https://git.coding.n ...

  9. Spring 依赖的Jar包简介

    Spring 依赖的Jar包简介 Spring的依赖关系 依赖关系分组 JAR文件 说 明 ant ant.jar, ant-junit.jar, ant-launcher.jar Spring采用A ...

  10. Ubuntu系统升级内核方法

    一.查看内核版本 $ uname-sr //查看内核版本 二.去Ubuntu网站http://kernel.ubuntu.com/~kernel-ppa/mainline/下载所需版本的deb文件 w ...