// Java Selection Sort
// -------------------
/*
Time Complexity
Best Time Complexity:O(n^2)
Average Time Complexity:O(n^2)
Worst Time Complexity:O(n^2)
Space Complexity
No auxiliary space is required in Linear Search implementation.
Hence space complexity is:O(1)
*/
import java.util.*;
public class SelectionSort {
public static int[] sort(int arr[]) {
for (int i = 0; i < arr.length; i++) {
int k = i; // referring to current index
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[k]) {
k = j;
}
}
int temp;
//Swapping algorithm
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
return arr; // returns the sorted array
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter Array Values: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int a[] = new int[arr.length];
a = Arrays.copyOf(sort(arr), arr.length); // copies the sorted array to another array
//sorted in ascending order
System.out.println("Sorted Array:");
for (int i = 0; i < a.length; i++) {
System.out.print(+a[i] + " ");
}
}
}