侧边栏壁纸
博主头像
buukle博主等级

布壳儿

  • 累计撰写 106 篇文章
  • 累计创建 15 个标签
  • 累计收到 9 条评论

目 录CONTENT

文章目录

算法练习(1) - 插入排序

administrator
2021-05-03 / 0 评论 / 0 点赞 / 284 阅读 / 555 字
public class Solution{
	public static void main (String[] args){
		
		Integer[] a = {1,4,7,3,8,0,2};
		insertSort(a);
	}
	
	private static void  insertSort(int[] a){
		for(int i = 1; i< a.length; i++){
			// 缓存数组当前下标为指针
			int pi = i;
			// 将指针从i开始,向前移动,移动的条件为 该指针大于0,并且数组位于该指针的值小于其前面的值,此时交换a[pi] 与 a[pi-1] 的值,将数组位于该指针的值向前移动
			while(pi > 0 ; a[pi] < a[pi-1]){
				int tempPi = a[pi];
				int tempPiBefore = a[pi-1];
				a[pi] = tempPiBefore ;
				a[pi-1] = tempPi ;
				pi --;
			}
		}
	
	}	
}
0

评论区