Sort Integers II

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];

        }

    }

}

null

时间: 2024-12-21 08:56:07

Sort Integers II的相关文章

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

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]. 解法一: // Quick sort class Solution { public: /** * @param A an integer array *

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

1356. Sort Integers by The Number of 1 Bits

package LeetCode_1356 import java.util.* /** * 1356. Sort Integers by The Number of 1 Bits * https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/description/ * * Given an integer array arr. You have to sort the integers in the array i

LeetCode | 1387. Sort Integers by The Power Value将整数按权重排序【Python】

LeetCode 1387. Sort Integers by The Power Value将整数按权重排序[Medium][Python][排序] Problem LeetCode The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps: if x is even then x = x / 2 if x is odd t

Sort Integers

Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) 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 {     /**      * @param A an integer a

Wiggle Sort I &amp; II

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Notice Please complete the problem in-place. Example Given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 分析: 排序,从第三个开

Sort Colors II Lintcode

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. Notice You are not suppose to use the library's sort function for this pro

[LintCode] Sort Integers 整数排序

Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm. Example Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5]. 这道题让我们实现最基本的几个O(n2)的排序算法,选择排序,冒泡排序和插入排序,都是最基本的排序算法.我们一个一个来看,首先来看冒泡排序,算法

lintcode143 - Sort Colors II - medium

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.ExampleGiven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-p