Sort Integers II
分析
Quick 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
|
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的更多相关文章
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- [LintCode] Sort Integers 整数排序
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Wiggle Sort I & II
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 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 ...
- 143. Sort Colors II
最后更新 一刷 class Solution { public void sortColors2(int[] colors, int k) { // write your code here if ( ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
随机推荐
- Mybatis传递参数的三种方式
第一种: Dao层使用@Param注解的方法 VersionBox getVersionByVersionNumAndVersionType(@Param("versionNum" ...
- NPOI读取Excel到集合对象
之前做过的项目中有个需要读取Excel文件内容的需求,因此使用NPOI实现,写下以下代码,这个只是一个代码段,还有很多地方需要优化,希望能对大家有所帮助 public static IList< ...
- kallsyms , addr to symbol
#!/usr/bin/env python # addr2sym.py - resolve addresses to symbols, using a map file # Reads a log f ...
- JAVA学习笔记--策略设计模式与适配器模式
一.策略设计模式 创建一个能够根据所传递对象的不同而具有不同行为的方法被称为策略设计模式:这类方法包含所要执行的算法中固定不变的部分,而“策略”包含变化的部分.策略就是传递进去的参数对象,它包含要执行 ...
- Codeforces Round #553 (Div. 2) C
C. Problem for Nazar time limit per test 1 second memory limit per test 256 megabytes input standard ...
- PytorchZerotoAll学习笔记(五)--逻辑回归
逻辑回归: 本章内容主要讲述简单的逻辑回归:这个可以归纳为二分类的问题. 逻辑,非假即真.两种可能,我们可以联想一下在继电器控制的电信号(0 or 1) 举个栗子:比如说你花了好几个星期复习的考试(通 ...
- 笔试题:C++打印队列
题目:打印队列 题目介绍:现在用打印机打印队列,已知打印任务有9个优先级(1-9),现在给出一系列任务,求输出打印顺序(任务下标,从0开始). 例: 输入:9,3,5,4,7,1 输出:0,4,2,3 ...
- Amazon.com 购物 信用卡预售期
I understand and thanks for confirming. In this case, the $1.00 is not a charge. It is an authoriza ...
- windows8和windows server2012不联网安装.net 3.5(包括2.0和3.0)
安装完win8后 发现系统默认没有安装.net3.5 如果使用在线更新的话需要很久才能完成,特别是当前的网速以及微软的服务器.速度很忙,其实我们利用win8的安装盘就可以不需要联网更新,而且几分钟就搞 ...
- MySQL 中的数据类型介绍
1.MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 2.数值类型(12) 2.1. ...