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实现
归并排序的优点不说了. 做归并排序之前,我先试着将两个有序数组进行排序,合并成一个有序数组. 思路:定义好两个有序数组,理解的时候我先思考了数组只有一个数组的排序,然后是两个元素的数组的排序,思路就有 ...
随机推荐
- swift 之 闭包
一.闭包 格式:{ ( 参数名:类型, 参数名:类型 .. ) in 内容体 return 返回值 } 最完整的闭包 1.省略参数类型 { ( 参数名, 参数名.. ) ...
- (转载)python2+selenium自动化测试系列(一)
1.Selenium2+python自动化1-环境搭建 2.Selenium2+python自动化2-pip降级selenium3.0 3.Selenium2+python自动化3-解决pip使用异常 ...
- js-我理解的闭包
一:什么是闭包 <JS高级程序设计>指出:闭包是指有有权访问另一个函数作用域中变量的函数. 二:闭包的使用 闭包的常见的创建方式是 子函数嵌套在父函数的内部,这样,子函数就可以访问父函数中 ...
- 使用SVN同步资源后图标样式的详细解读
项目视图 The Package Explorer view - 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽 ...
- Java动手动脑(二)
1>类的对象实例化 由于main为静态类型,所以在调用函数时也必须调用静态方法,如上代码中的求平方数的静态方法,如何在静态main中调用非静态类的方法呢? 静态方法只能直接访问静态成员,无法访问 ...
- BPTT算法推导
随时间反向传播 (BackPropagation Through Time,BPTT) 符号注解: \(K\):词汇表的大小 \(T\):句子的长度 \(H\):隐藏层单元数 \(E_t\):第t个时 ...
- Servlet过滤器(详询请加qq:2085920154)
1.1 过滤器简介 过滤器是向Web应用程序的请求和响应处理添加功能的Web服务组件.在Servlet处理用户输入的请求之前,过滤器可以访问该请求.在将Web响应发送给用户之前,过滤器还可以访问该响 ...
- 为什么不能访问django自带的索引页
通过HTTP://192.168.160.128:8000访问虚拟机上的django索引页出现“ 无法访问此网站 192.168.160.128 拒绝了我们的连接请求. ” 是什么原因呢?费了好大一番 ...
- Windows Server 2003 服务器备份和恢复技巧
1.系统备份与恢复 现在很多中小型企业服务器并没有采用专业的服务器备份机制进行备份,对于个人站长而言这似乎更不可能.但是再好的服务器维护人员总有失误的时候,为了防止万一我们最好还是采用一些备 ...
- webService 跨域请求webconfig配置
在webService文件夹下单独添加一个web.config <?xml version="1.0" encoding="UTF-8"?>< ...