import java.util.Random;
public class helloword{
public static void main(String[] args){
int[] b = new int[10];
int i;
for (i = 0; i < 10; i++){
b[i] = new Random().nextInt(10);
}
System.out.println("排序前的数据:");
for (i = 0; i < 10; i++){
System.out.println(b[i]);
}
System.out.println("排序后的数据:");
insertSort(b, 10);
for (i = 0; i < 10; i++){
System.out.println(b[i]);
}
}
//直接插入排序
public static void insertSort(int[] a, int n){
int i, j, k;
int temp;
for (i = 1; i < n; i++){
j = i - 1;
temp = a[i];
while (j >= 0 && temp <= a[j])
--j;
if (j < 0)
j += 1;
for (k = i - 1; k > j; k--)
a[k+1] = a[k];
a[j] = temp;
}
}
}
排序算法之直接插入排序----java实现
最新推荐文章于 2022-09-04 09:06:53 发布