加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java实现插入排序

发布时间:2020-12-15 07:22:15 所属栏目:Java 来源:网络整理
导读:1 package xiaoling; 2 public class InsertSort{ 3 public static void main(String[] args){ 4 int [] nums = new int [10 ]; 5 for ( int loc=0; locnums.length; ++ loc) 6 nums[loc] = ( int )(Math.random() * 100) + 5 ; 7 for ( int num: nums) Syst
 1 package xiaoling;
 2 public class InsertSort{
 3         public static void main(String[] args){
 4                 int[] nums = new int[10];
 5                 for (int loc=0; loc<nums.length; ++loc)
 6                         nums[loc] = (int)(Math.random() * 100) + 5;
 7                 for (int num: nums) System.out.print(num + " ");
 8                 System.out.println();
 9                 new InsertSort().insertSort(nums,0,nums.length);
10                 for (int num: nums) System.out.print(num + " ");
11                 System.out.println();
12         }
13         public void insertSort(int[] nums,int start,int end){
14                 for (int i=start; i<end; ++i){
15                         int temp = nums[i];
16                         int j = i;
17                         for (; j>start; --j){
18                                 if (nums[j-1] > temp) 
19                                         nums[j] = nums[j-1];
20                                 else 
21                                         break;  
22                         }
23                         nums[j] = temp;
24                 }
25         }
26 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读