插入排序小练习

package com.test.sort;


public class InsertSort {


public static void main(String[] args) {
int[] array = new int[] { 1, 9, 3, 2, 4, 6, 5, 7, 0 };
for (int k : array) {
System.out.print("  " + k);
}
InsertSort.sort(array);
System.out.println();
for (int k : array) {
System.out.print("  " + k);
}
}


public static void sort(int[] param) {
int length = param.length;
if (length == 0) {
return;
}
int temp, j;
for (int i = 1; i < length; i++) {
temp = param[i];
j = i;
while (j > 0 && param[j - 1] > temp) {
param[j] = param[j - 1];
j--;
}
param[j] = temp;
}
}
}

猜你喜欢

转载自blog.csdn.net/qq_33500630/article/details/78772949