//package com.nowjava;
public class Main {
/**
NowJava.com 提供
**/
public static void main(String[] argv) throws Exception {
int[] arr = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
System.out.println(java.util.Arrays.toString(insertSort(arr)));
}
/**
* insert sort
* <p/>
* the most stable sort algorithm
* <p/>
* times of comparing equals times of swap
* <p/>
* two times as fast as bubble sort
* <p/>
* a little faster than select sort
*/
public static int[] insertSort(int[] arr) {
int swapCount = 0;
for (int i = 1; i < arr.length; i++) {
int cursor = i;
while (cursor > 0 && arr[cursor - 1] > arr[cursor]) {
swap(arr, cursor--, cursor);//来自 N o w J a v a . c o m
swapCount++;
}
}
System.out.println("swap count(insert sort): " + swapCount);
return arr;
}
/**
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。