Selection Sort

class SelectionSort{ public static void main(String[] args){ int[] a = {5,2,4,6,1,3}; selectionSort(a); printArray(a); } public static void selectionSort(int[] a){ for(int i = 0; i < a.length - 1; i++){ int min = 999999; int index = i; for(int j = i + 1; j < a.length; j++){ if(min > a[j]){ min = a[j]; index = j; } } if(a[i] > a[index]) swap(i, index, a); } } public static void printArray(int[] a){ for(int i = 0; i < a.length; i++){ System.out.print(a[i]+" "); } } public static void swap(int i, int index, int[] a){ int temp = a[i]; a[i] = a[index]; a[index] = temp; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.