public class InsertSortTest {
public static void InsertSort(int[] source) {
int i, j;
int insertNode;// 要插入的数据
// 从数组的第二个元素开始循环将数组中的元素插入
for (i = 1; i < source.length; i++) {
// 设置数组中的第2个元素为第一次循环要插入的数据
insertNode = source[i];
j = i - 1;
// 如果要插入的元素小于第j个元素,就将第j个元素向后移
while ((j >= 0) && insertNode < source[j]) {
source[j + 1] = source[j];
j--;
}
// 直到要插入的元素不小于第j个元素,将insertNote插入到数组中
source[j + 1] = insertNode;
System.out.print("第" + i + "趟排序:");
printArray(source);
}
}
private static void printArray(int[] source) {
for (int i = 0; i < source.length; i++) {
System.out.print("\t" + source[i]);
}
System.out.println();
}
public static void main(String[] args) {
int source[] = new int[] { 53, 27, 36, 15, 69, 42 };
System.out.print("初始关键字:");
printArray(source);
System.out.println("");
InsertSort(source);
System.out.print("\n\n排序后结果:");
printArray(source);
}
}
初始关键字: 53 27 36 15 69 42 第1趟排序: 27 53 36 15 69 42 第2趟排序: 27 36 53 15 69 42 第3趟排序: 15 27 36 53 69 42 第4趟排序: 15 27 36 53 69 42 第5趟排序: 15 27 36 42 53 69 排序后结果: 15 27 36 42 53 69
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有