数组插入排序

//中心思想: 在已经排好序 的元素之间插入一个元素,用这个新插入的元素与前面已经排好序的元素进行比较,小的或大的往前移。

 

                  class    HomeWork4   {

                     public static void main (String[] arg)

                             {

                                        int[] arr1 = {19,7,28,1,6,9}; //  定义一个数组

                                        for(int i = 1;i < arr1,length; i++)    //   外循环,前面一个元素已经排好序了,所以就只有五个元素需要排序,因此只循环五次

                                               {

                                                         for(int j = i - 1; j >= 0;j--)  //   这个循环的是需要插入的元素与前面已经排好序的元素进行比较

                                                               {

                                                                        if(arr1[ j ] > arr1[ j + 1])    //  判断条件 

                                                                              {

                                                                                      int temp = arr1[ j ];

                                                                                      arr[ j ] = arr1[ j + 1];

                                                                                      arr1[ j + 1] = temp;

                                                                               }

                                                                 }

                                               }

                                                     for (int value : arr1)

                                                             {

                                                                       System.out.println(value);

                                                             }

                   }

     }

                                                                               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值