Program to demonstrate how to sort array elements using Selection Sort algorithm in Java
Output of the program :
public class SelectionSort { public static void main(String[] args) { int[] a = {10,2,3,4,7,8,9,0}; System.out.println("Before Selection Sort: "); for(int k = 0 ; k < a.length; k++) System.out.print(a[k] + " "); int min =0 , out =0 , in = 0; for(out = 0; out < a.length; out++){ min = out; for(in = out+1; in < a.length; in++ ){ if(a[min] < a[in]){ min = in; } } int temp = 0; temp = a[min]; a[min] = a[out]; a[out] = temp; } System.out.println("\nAfter Selection Sort: "); for(int k = a.length - 1; k >= 0; k--) System.out.print(a[k] + " "); } }
Output of the program :