插入排序算法
来源:互联网 发布:苏州大学网络认证登录 编辑:程序博客网 时间:2024/05/16 06:45
package com.liu.sort_test;
/**
* 插入排序 升序
*
* @author liu
*
*/
public class InsertSort {
static int arr[] = new int[] { 1, 3, 2, 5, 4, 7, 8, 0, 9 };
public static void main(String[] args) {
// 要循环的次数
for (int i = 1; i < arr.length; i++) {
int j = i - 1;// 基数的下标
int temp = arr[i];// 要插入的数据
// 即将插入的数据和之前的数据进行比较
while (j >= 0 && temp < arr[j]) {
// 直到找到不比基数小的位置
arr[j + 1] = arr[j];
j--;
}
// 在该位置插入
arr[j + 1] = temp;
}
//打印出来
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/**
* 插入排序 升序
*
* @author liu
*
*/
public class InsertSort {
static int arr[] = new int[] { 1, 3, 2, 5, 4, 7, 8, 0, 9 };
public static void main(String[] args) {
// 要循环的次数
for (int i = 1; i < arr.length; i++) {
int j = i - 1;// 基数的下标
int temp = arr[i];// 要插入的数据
// 即将插入的数据和之前的数据进行比较
while (j >= 0 && temp < arr[j]) {
// 直到找到不比基数小的位置
arr[j + 1] = arr[j];
j--;
}
// 在该位置插入
arr[j + 1] = temp;
}
//打印出来
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
0 0
- 排序算法-插入排序
- 排序算法--插入排序
- 排序算法---插入排序
- 排序算法-插入排序
- 排序算法-插入排序
- 排序算法--插入排序
- 排序算法-插入排序
- 排序算法-插入排序
- 排序算法---插入排序
- 排序算法--插入排序
- 排序算法--插入排序
- 排序算法--插入排序
- 排序算法-插入排序
- 排序算法--插入排序
- 排序算法-插入排序
- 排序算法-插入排序
- 排序算法--插入排序
- 排序算法-插入排序
- BZOJ 3714 PA2014 Kuglarz Kruskal
- android XML File配置资源文件使用详解、属性功能详解之Drawable
- 5 string 和 stringbuffer的区别?
- opencv 模板匹配matchTemplate研究之二
- 在myeclipse中使用git管理加载maven项目
- 插入排序算法
- python文件和目录操作方法大全(含实例)
- [leetcode]Find Minimum in Rotated Sorted Array
- js对Date进行处理,拿到月初的日期
- 使用mybatis对SQLite数据库分页操作
- UVA(木块问题)101
- PHP工厂模式的好处
- JAVA环状单向链表去除连续的重复元素(求代码)
- linux中关于目录权限的说明