插入排序 将数组所有元素划分成了有序区和无序区,假设当前数组有 N 个元素, 开始默认第一个元素(下标为0)所处的位置是有序区,这是局部有序,从第二个元素(i=1)至数组最后一个元素(i=N-1)属于无序区; 假设数组元素是按从左至右存放的, 如果用 i 来标记无序区中的第一个元素下标,也就是无序区中最左边或者说是无序区中下标值最小的下标, 则每趟排序是将下标 i 所指向的有效值插入有序区的适当位置,使得每趟排序完成之后,有序区的所有元素总是保持局部有序状态。 按这样来回 N -1 趟插入之后,则 N 个元素已成有序状态。 尽管插入排序的复杂度也是 O(n^2),但一般情况下,插入排序会比冒泡排序快一倍,要比选择排序还要快一点。 排序基类:BaseSort.java
package
sort;
/** * ----------------------------------------- * @文件: BaseSort.java * @作者: fancy * @邮箱: fancydeepin@yeah.net * @时间: 2012-7-18 * @描述: 基类 * ----------------------------------------- */
public
class
BaseSort
{ protected final static int ASC = 1 ; // 升序 protected final static int DESC = 0 ; // 降序 // 交换i1、i2下标指向的值 public void swap(Object[] array, int i1, int i2) { Object tempObj; tempObj = array[i1]; array[i1] = array[i2]; array[i2] = tempObj; tempObj = null ; } // 打印输出数组元素 public void print(Object[] array) { for (Object obj : array) { System.out.print(obj + " " ); } System.out.println(); } }
插入排序:InsertSort.java
package
sort;
/** * ----------------------------------------- * @文件: InsertSort.java * @作者: fancy * @邮箱: fancydeepin@yeah.net * @时间: 2012-7-18 * @描述: 插入排序 * ----------------------------------------- */
public
class
InsertSort
extends
BaseSort
{ /** * @方法名: insertSort * @参数名: array 排序对象 * @参数名: order 排序顺序 * @描述语: 插入排序:默认第一个是有序,使其余的像纸牌游戏那样依次插入,复杂度 O(n^2) */ public void insertSort(Object[] array, int order) { int length = array.length; if ( order == ASC ) { for ( int i = 1 , j ; i < length; i ++ ) { // 默认第1个(下标0)有序,i是无序区第一个元素下标,第i趟结束后,i下移,如此来回 N -1趟 for (j = 0 ; j < i; j ++ ) { // 将无序区下标为i所指向的值插入有序区适当位置 if (Double.parseDouble(array[j].toString()) > Double.parseDouble(array[i].toString())) { swap(array, i, j); } } System.out.println(" --------------------------------------------------------------------------->第 " + i + " 趟 " ); print(array); } }else if ( order == DESC ) { for ( int i = 1 , j; i < length; i ++ ) { // 默认第1个(下标0)有序,i是无序区第一个元素下标,第i趟结束后,i下移,如此来回 N -1趟 for (j = 0 ; j < i; j ++ ) { // 将无序区下标为i所指向的值插入有序区适当位置 if (Double.parseDouble(array[j].toString()) < Double.parseDouble(array[i].toString())) { swap(array, i, j); } } System.out.println(" --------------------------------------------------------------------------->第 " + i + " 趟 " ); print(array); } } } }
如需了解上面由粉红色标记出来的代码的用意,请参考我 java 分类里的 冒泡排序 这一随笔 :
http://www.blogjava.net/fancydeepin/archive/2012/07/18/java_bubblesort.html
测试类:TestApp.java
package
sort;
/** * ----------------------------------------- * @文件: TestApp.java * @作者: fancy * @邮箱: fancydeepin@yeah.net * @时间: 2012-7-18 * @描述: 测试类 * ----------------------------------------- */
public
class
TestApp
{ public static void main(String[] args) { Object[] array = { 9 , 5 , 7 , 1 , 6 , 3 , 8 , 10 , 4 , 2 } ; ( new InsertSort()).insertSort(array, InsertSort.ASC); } }
后台输出打印结果:
--------------------------------------------------------------------------->
第1趟
5
9
7
1
6
3
8
10
4
2
--------------------------------------------------------------------------->
第2趟
5
7
9
1
6
3
8
10
4
2
--------------------------------------------------------------------------->
第3趟
1
5
7
9
6
3
8
10
4
2
--------------------------------------------------------------------------->
第4趟
1
5
6
7
9
3
8
10
4
2
--------------------------------------------------------------------------->
第5趟
1
3
5
6
7
9
8
10
4
2
--------------------------------------------------------------------------->
第6趟
1
3
5
6
7
8
9
10
4
2
--------------------------------------------------------------------------->
第7趟
1
3
5
6
7
8
9
10
4
2
--------------------------------------------------------------------------->
第8趟
1
3
4
5
6
7
8
9
10
2
--------------------------------------------------------------------------->
第9趟
1
2
3
4
5
6
7
8
9
10
[ 随笔均原创,转载请注明出处:http://www.blogjava.net/fancydeepin ]