插入排序

 1 package com.datastack.search;
 2 
 3 import java.util.Arrays;
 4 
 5 //插入排序
 6 public class InsertSort {
 7     public static void main(String[] args) {
 8         int[] arr = new int[]{1,3,5,2,5,5,512,231,123,556,669};
 9         insertSort(arr);
10         System.out.println(Arrays.toString(arr));
11     }
12     public static void insertSort(int[] arr){
13         for(int i=1;i<arr.length;i++){
14             int temp = arr[i];
15             int left = i-1;
16             while(left>=0 && arr[left] > temp){
17                 arr[left+1] = arr[left];
18                 left--;
19             }
20             arr[left+1] = temp;
21         }
22     }
23 }

 

posted @ 2019-09-22 10:20  az1l3l  阅读(163)  评论(0编辑  收藏  举报