import java.io.*; class GFG { static void selectionSort(int arr[], int n){ for(int i = 0; i < n; i++){ int min_ind = i; for(int j = i + 1; j < n; j++){ if(arr[j] < arr[min_ind]){ min_ind = j; } } int temp = arr[i]; arr[i] = arr[min_ind]; arr[min_ind] = temp; } } public static void main (String[] args) { int a[] = {2, 1, 4, 3}; selectionSort(a, 4); for(int i = 0; i < 4; i++){ System.out.print(a[i] + " "); // OUTPUT : 1 2 3 4 } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter