JAVA-基础-图解 插入排序

插入排序:

    原理:

    

    代码实现:

 1             /**
 2                     插入排序
 3             */
 4             public class Demo01{
 5                     public static void main(String args[]){
 6                             int nums [] = {1,34,56,8,-32,7,-9,0,235};
 7             
 8                     for(int i = 1;i<nums.length;i++){
 9                             int tmp = nums[i];
10                             int point = i;
11                             for(int j=i-1;j>=0&&nums[j]>tmp;j--){
12                                     nums[j+1]=nums[j];
13                                     point = j;
14                             }
15                             nums[point] = tmp;
16                     }
17             
18                     for(int n : nums){
19                             System.out.println(n);
20                     }
21                     }
22             }

猜你喜欢

转载自www.cnblogs.com/xiaoluohao/p/11305438.html