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; 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 }
posted on 2019-10-18 14:13 wangling1820 阅读(132) 评论(0) 编辑 收藏 举报