Lintcode整数排序
来源:互联网 发布:手机桌面特效软件 编辑:程序博客网 时间:2024/06/05 10:35
整数排序
给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。
对于数组 [3, 2, 1, 4, 5]
, 排序后为:[1, 2, 3, 4, 5]
。
插入排序:
public class Solution {
/*
* @param A: an integer array
* @return:
*/
public void sortIntegers(int[] A) {
// write your code here
int j=0;
for(int i=1;i<A.length;i++){
j=i;
int target=A[j];//记录A[j]的值
while(j>0&&target<A[j-1]){
A[j]=A[j-1];
j--;
}
A[j]=target;
}
}
}
冒泡排序:
public class Solution {
/*
* @param A: an integer array
* @return:
*/
public void sortIntegers(int[] A) {
// write your code here
int temp;
for(int i=0;i<A.length-1;i++){
for(int j=0;j<A.length-1-i;j++){
if(A[j]>A[j+1]){
temp=A[j+1];
A[j+1]=A[j];
A[j]=temp;
}
}
}
}
}
选择排序:
public class Solution {
/*
* @param A: an integer array
* @return:
*/
public void sortIntegers(int[] A) {
// write your code here
for(int i=0;i<A.length-1;i++){
int min=A[i];
int index=i;
for(int j=i+1;j<=A.length-1;j++){
if(A[j]<min){
min=A[j];
index=j;
}
}
int b=A[i];
A[i]=min;//把最小的值放在第i个;
A[index]=b;
}
}
}
- LintCode:整数排序
- 【LintCode】整数排序II
- LintCode【入门】整数排序
- 整数排序II-lintcode
- lintcode-入门-整数排序
- Lintcode 463 整数排序
- LintCode-整数排序
- LintCode-整数排序 II
- 整数排序-LintCode
- LintCode 整数排序
- LintCode 整数排序 II
- LintCode 整数排序
- Lintcode整数排序
- 整数排序-LintCode
- 整数排序 II -LintCode
- lintcode 463整数排序 lintcode 464整数排序II
- [LintCode] Sort Integers 整数排序
- Lintcode——整数排序
- 栅格
- 第3章 第一部分
- 制作Ubuntu安装U盘
- 鼠标事件案例—按钮切换背景颜色
- html之DOM属性查找,操作
- Lintcode整数排序
- android——暴力隐藏底部导航栏
- CentOS7 修改root密码
- Android DVB底层Linux驱动
- 图片加载框架 --Picasso 的基本使用
- 鼠标点击图片后移动事件
- 字符串:字符串的反转
- android全局替换字体最新方案
- 数据结构思维 第十七章 排序