package selectionsort;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {5,4,2,8,6,9};
int leng = arr.length;
System.out.print("Original array:");
PrintArray(arr);
for (int i = 0 ; i < leng - 1 ; i++) {
int smallest = i;
for (int j = i + 1 ; j < leng; j++) {
if (arr[j] < arr[smallest]) {
smallest = j;
int temp = arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
}
}
}
System.out.print("Sorted array:");
PrintArray(arr);
}
static void PrintArray(int[] arr){
for (int val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
}
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