Selection Sort Using Java
package demo;
import java.util.Arrays;
import java.util.stream.Collectors;
public class SelectionSort {
public static void main(String[] args) {
int arr[] = { 2, 4, 1, 5, 3, 6, 9, 8, 7 };
for (int i = 0; i < arr.length; i++) {
int minId = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minId]) {
minId = j;
}
}
int temp = arr[minId];
arr[minId] = arr[i];
arr[i] = temp;
}
System.out.println(Arrays.stream(arr).boxed().collect(Collectors.toList()));
}
}
Comments
Post a Comment