您的位置:首页 > 其它

算法导论第一章

2017-09-15 10:58 92 查看

插入排序

package com.gds.algorithms;

/**
* Created by yangzhanku on 2017/9/7.
*/
public class InsertionSortTest {
public static void main(String[] args) {
int[] arrayTest = {100, 9, 8, 78, 90, 10};
InsertionSort(arrayTest);
}

private static void InsertionSort(int[] arrayTest) {
for (int j = 1; j < arrayTest.length; j++) {
int key = arrayTest[j];
int i = j - 1;
while (i >= 0 && arrayTest[i] > key) {
arrayTest[i + 1] = arrayTest[i];
i--;
}
arrayTest[i + 1] = key;
}

for (int k :
arrayTest) {
System.out.print(k + " ");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息