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. 基于MapReduce的(用户、物品、内容)的协同过滤推荐算法

    1.基于用户的协同过滤推荐算法 利用相似度矩阵*评分矩阵得到推荐列表 已经推荐过的置零 2.基于物品的协同过滤推荐算法 3.基于内容的推荐 算法思想:给用户推荐和他们之前喜欢的物品在内容上相似的物品 ...

  2. IO多路复用(二) -- select、poll、epoll实现TCP反射程序

    接着上文IO多路复用(一)-- Select.Poll.Epoll,接下来将演示一个TCP回射程序,源代码来自于该博文https://www.cnblogs.com/Anker/p/3258674.h ...

  3. python2/3 发送https请求时,告警关闭方法

    问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)情况下,控制台会输出以下错误: InsecureRequestWarning: Unverifi ...

  4. Phonegap 环境配置

    目前要开发 Web App 还是有比较多的选择的 如 Phonegap.MUI.AppCan,接下来以 Web前端开发工程师 的角度来一个 Phonegap 的 First Blood 一.开发环境: ...

  5. BFC的表象认识

    首先字面翻译,这三个字母分别代表什么,box,formatting, context,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用. 形象点就是说一种规范,规范什么呢?规范盒子内部 ...

  6. Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  7. 【Alpha】阶段第一次Scrum Meeting

    [Alpha]阶段第一次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 后端接口开发 测试接口,修正bug 赵智源 撰写测试方案书 部署实际任务和编写测试样例 ...

  8. win10自带中文输入法的用户体验

    用户界面: 貌似没有什么界面,不过我感觉这就是最大的优点,没有过度渲染的界面,没有烦人的推送.弹窗,没有定期不定期的更新提示,简洁也是我使用这款输入法的最主要的原因 记住用户的选择: 这点我认为win ...

  9. HDU 5229 ZCC loves strings 博弈

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5229 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  10. lintcode-414-两个整数相除

    414-两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11. 标签 二分法 思路 ...