LintCode 463 Sort Integer
这个是O(n2)的排序的总结
/* bubble sort */
public static void sortIntegers(int[] A) {
// Write your code here
int len = A.length;
if (len == 0) return;
for (int j = 0; j < len; ++j) {
for (int i = 0; i < len-1-j; ++i) { // len-1-j
if (A[i] > A[i+1]) {
int temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
}
}
}
/* selection sort */
public static void sortIntegers(int[] A) {
for (int i = 0; i < A.length-1; i++){
int index = i;
for (int j = i+1; j < A.length; j++){
if (A[index] > A[j]) index=j; // find the smallest
}
// then swap
int smaller = A[index];
A[index] = A[i];
A[i] = smaller;
}
}
/* insertion sort */
public static void sortIntegers(int[] A) {
int n = A.length;
for (int i = 1; i < n; i++) {
int key = A[i]; // boundary
int j = i - 1; // another pointer
while ((j > -1) && (A[j] > key)) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = key;
}
}
LintCode 463 Sort Integer的更多相关文章
- you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(255), sort integer not null
you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...
- zenefits oa - sort integer array in lexographical order
[ 12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000 ] Then the output should be: [ 1 | 100000 | 12 | 222 ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LintCode] Wiggle Sort 扭动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Java基础:整型数组(int[]、Integer[])排序
Windows 10家庭中文版,java version "1.8.0_152",Eclipse Oxygen.1a Release (4.7.1a), 参考链接:http://w ...
- 归并排序的java实现
归并排序的优点不说了. 做归并排序之前,我先试着将两个有序数组进行排序,合并成一个有序数组. 思路:定义好两个有序数组,理解的时候我先思考了数组只有一个数组的排序,然后是两个元素的数组的排序,思路就有 ...
随机推荐
- Git学习(四)——分支管理
一.创建与合并分支 1.创建分支 一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点.每次提交 ,mast ...
- Python3利用BeautifulSoup4批量抓取站点图片的代码
边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...
- Java广度优先爬虫示例(抓取复旦新闻信息)
一.使用的技术 这个爬虫是近半个月前学习爬虫技术的一个小例子,比较简单,怕时间久了会忘,这里简单总结一下.主要用到的外部Jar包有HttpClient4.3.4,HtmlParser2.1,使用的开发 ...
- orm获取关联表里的属性值
ORM——关系对象模型 laravel中的Eloquent ORM用于和数据表互动,其中每个数据库表会和一个对应的「模型」互动,想要了解请查看官方文档或自行百度.获取关联表里的属性值代码如下: /** ...
- zabbix nagios 类nagios 之 不以性能为前提的开发和监控都是瞎扯淡
从最初的nagios到现在强大的zabbix 3.0,我想说,不以性能为前提的开发和监控都是瞎扯淡? 首先我对这两款监控软件的认识: zabbix,很多企业都在用,给人的感觉是很炫,不过我个人觉得虽然 ...
- redirect和forward
1.重定向 <mvc:view-controller path="/" view-name="redirect:/admin/index"/>即如果 ...
- git使用简单教程
废话不多说,直接开始 1. 进入https://github.com/ 创建你的账号,然后开始创建一个你的仓库(基本概念请自行百度),比如jun. 2. 创建好仓库之后,下载git 3,设置git,就 ...
- ExtJs 获取Dom对象
对象指页面上的某一部分,如:Input等.我觉得在EXT JS中会有三类基本对象,htmlelement , EXT.Element和CompositeElement .分别解释一下: htmlele ...
- shell常用命令
wget wget -P /root url -O rename 实现文件下载指定目录和重命名
- 总结30个CSS选择器
或许大家平时总是在用的选择器都是:#id .class 以及标签选择器.可是这些还远远不够,为了在开发中更加得心应手,本文总结了30个CSS3选择器,希望对大家有所帮助. 1 *:通用选择器 * ...