//码牌插
public class ChaRu {
public static void main(String[] args) {
int[] nums = { 9, 56, 6, 42, 34 };
int key = 0;
int n = 0;
for (int i = 1; i < nums.length; i++) {
key = nums[i];
for (int j = i - 1; j >= 0; j--) {
if (key < nums[j]) {
nums[j + 1] = nums[j];
nums[j] = key;
}
n++;
}
}
System.out.println("********" + n);
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
}
//选好位置插
public static void main(String[] args) {
int[] nums = { 1, 5, 7, 6, 4, 2, 1 };
int key = 0;
for (int i = 1; i < nums.length; i++) {
key = nums[i];
int index = 0;
for (int j = i - 1; j >= 0; j--) {
index = j;
if (key < nums[j]) {
nums[j + 1] = nums[j];
} else {
break;
}
nums[index] = key;
}
}
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
java 插入排序
最新推荐文章于 2024-11-13 21:40:45 发布