- /**
- * 插入排序,稳定的排序方法
- *
- * @param data
- */
- public static void sort(int[] data) {
- for (int i = 1; i < data.length; i++) {
- // 从第一个元素(索引值为0)开始,该元素可以认为已经被排序
- for (int j = i; j > 0; j--) { // 取出下一个元素,在已经排序的元素序列中从后向前扫描
- if (data[j] < data[j - 1]) { // 小于,将元素移到该位置
- int t = data[j];
- data[j] = data[j - 1];
- data[j - 1] = t;
- continue; // 避免break
- }
- break; // 大于,则不做更改,跳出,更改为下一个插入元素
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2807201513239.html
来源: http://www.codesnippet.cn/detail/2807201513239.html