package selectionSort; import java.util.Arrays; public class Exam { //使用选择排序法对数组进行排序 public static void main(String[] args) { int[] nums= {23,3453,123,54,12,45,145,135,656,146,65}; SelectionSort(nums); System.out.println(Arrays.toString(nums)); } public static void SelectionSort(int[] nums) { if(nums==null||nums.length<0) { return; } for(int i=0;i<nums.length-1;i++) { int arrayMin=nums[i]; int arrayMinIndex=i; for(int j=i+1;j<nums.length;j++) { if(arrayMin>nums[j]) { arrayMin=nums[j]; arrayMinIndex=j; } } if(arrayMin!=i) { nums[arrayMinIndex]=nums[i]; nums[i]=arrayMin; } } } }
原文地址:https://www.cnblogs.com/BatmanY/p/8710781.html
时间: 2024-10-05 05:25:39