插入排序方式

package vincent.sort;

public class InsertSort {

	/**
	 * @param args
	 */
	static  int[] a = new int[10];
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		a[0] = 4;
		a[1] = 1;
		a[2] = 8;
		a[3] = 9;
		a[4] = 10;
		a[5] = 6;
		a[6] = 2;
		a[7] = 0;
		a[8] = 5;
		a[9] = 14;
		insertionSort();
	}
	
	public static void insertionSort(){
		int in , out;
		for(out = 1; out < 10; out ++ ){
			
			int temp = a[out];
			in = out;
			while(in > 0 && a[in-1] >= temp){
				a[in] = a[in -1];
				-- in;
			}			
			a[in] = temp;
			print();
		}
	}
	
	public static void print(){
		for(int i : a){
			System.out.print(i + "  ");
		}
		System.out.println();
	}
}


附加上控制台输出

 

1  4  8  9  10  6  2  0  5  14 
1  4  8  9  10  6  2  0  5  14 
1  4  8  9  10  6  2  0  5  14 
1  4  8  9  10  6  2  0  5  14 
1  4  6  8  9  10  2  0  5  14 
1  2  4  6  8  9  10  0  5  14 
0  1  2  4  6  8  9  10  5  14 
0  1  2  4  5  6  8  9  10  14 
0  1  2  4  5  6  8  9  10  14 

 

插入排序逻辑:

外层for循环中, out 变量从1 开始,向右移动, 他标记了为排序的部分的最左端的数据。 而在内层的while循环中,in变量从out变量开始,向左移动,直到temp变量小于in所指向的数据项,或者他已经不能够再往左移动为止,while循环的每躺都是向右移动一个已经排序的数据项,并且注意  完毕之后需要将 已经找到位置的a[in] = temp。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值