1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class Code01_selectionSort { public static void selectionSort(int arr[]){ if(arr == null || arr.length<2){ return; } for(int i = 0;i<arr.length-1;i++){ int minIndex = i; for(int j = i+1; j < arr.length; j++){ minIndex = arr[j]<arr[minIndex] ? j : minIndex; } swap(arr,i,minIndex); } } public static void swap(int[] arr,int i, int j){ int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static void main(String[] args) { int[] arr={12,45,4,5456,12,12}; selectionSort(arr); for(int i= 0;i<arr.length;i++){ System.out.println(arr[i]); } } }
|