Java中的插入排序。
以下是必需的程序。
示例
public class Tester {
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ){
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
public static void main(String a[]){
int arr[] = {21,60,32,01,41,34,5};
System.out.println("Before Insertion Sort");
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr); //sorting array using insertion sort
System.out.println("After Insertion Sort");
for(int i:arr){
System.out.print(i+" ");
}
}
}输出结果
Before Insertion Sort 21 60 32 1 41 34 5 After Insertion Sort 1 5 21 32 34 41 60
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短