public class Sort {
static void insertSort(int a[], int n) {
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
int temp = a[i];
int k = i - 1;
while (k >= 0 && a[k] > temp) {
a[k + 1] = a[k];
k--;
}
a[k + 1] = temp;
}
}
}
public static void main(String[] args) {
int a[] = { 3, 2, 5, 7, 8, 9, 4, 1, 0, 4, 6 };
insertSort(a, a.length );
for (int i : a) {
System.out.print(i + " ");
}
}
}
插入排序
最新推荐文章于 2024-11-10 19:17:26 发布