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. kallsyms , addr to symbol

    #!/usr/bin/env python # addr2sym.py - resolve addresses to symbols, using a map file # Reads a log f ...

  2. tensorflow中tensor与数组之间的转换

    # 主要是两个方法: # 1.数组转tensor:数组a, tensor_a=tf.convert_to_tensor(a) # 2.tensor转数组:tensor b, array_b=b.eva ...

  3. 使用IntelRealScene设备结合Cocos引擎实现体感游戏开发

    英特尔开发人员专区原文地址 Cocos游戏开发引擎对于广大开发者来说都比较熟悉,Intel RealScene是什么呢,简单理解是一种特殊的摄像头,可以捕捉用户的手势,面部表情等,进而实现AR,VR的 ...

  4. EXE模块说明

    EXE模块是fastCMS系统内非常优秀的一个功能模块,它将一些操作打包成可执行单元.它具有以下优势: 1)功能明确.便于维护. 2)发起端可获取EXE模块的执行结果. 3)对于不需要执行结果的请求, ...

  5. 【PMP考试专栏】01、五大过程组和十大知识领域

  6. 【ZABBIX】Zabbix触发器的告警原理及创建方法

    概述:       触发器中的表达式使用很灵活,我们可以创建一个复杂的逻辑测试监控,触发器表达式形式如下: {<server>:<key>.<function>(& ...

  7. DeepLearning - Overview of Sequence model

    I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...

  8. Halcon算子解释

    Halcon算子解释大全 Halcon/Visionpro视频教程和资料,请访问 重码网,网址: http://www.211code.com Chapter 1 :Classification 1. ...

  9. 兰亭集势收购美国社交购物网站Ador,收购的是人才

    1 月 6 日消息,外贸电商公司兰亭集势(LightInTheBox)今日宣布,已经完成对美国社交电商网站 Ador 公司的收购.Ador 公司总部位于西雅图.这项资产收购通过现金完成,但未披露交易金 ...

  10. linux awk,sort,uniq,wc,cut命令详解

    1.awk awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 $ 表示当前行 $ 表示第一列 NF 表示一共有多少列 $NF 表示最 ...