選擇排序法對數組進行排序
阿新 • • 發佈:2018-04-03
clas ret rgs string src image OS index png
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)); } publicstatic 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; } } } }
選擇排序法對數組進行排序