| 1 | public class Main { |
| 2 | static void selectionSort(int[] arr){ |
| 3 | int n = arr.length; |
| 4 | for(int i = 0; i < n-1; i++){ // i represent the current index |
| 5 | //Find the minimum element in unsorted part of array |
| 6 | int min_index = i; |
| 7 | for(int j = i+1; j < n; j++){ |
| 8 | if(arr[j] < arr[min_index]){ |
| 9 | min_index = j; |
| 10 | } |
| 11 | } |
| 12 | //swap current element and minimum element -> current index i will have correct element |
| 13 | // a[min_index], a[i] |
| 14 | int temp = arr[i]; |
| 15 | arr[i] = arr[min_index]; |
| 16 | arr[min_index] = temp; |
| 17 | } |
| 18 | } |
| 19 | public static void main(String[] args) { |
| 20 | int[] arr = {7, 4, 1, 2, 100, 90}; |
| 21 | selectionSort(arr); |
| 22 | for(int i = 0; i < arr.length; i++){ |
| 23 | System.out.print(arr[i] + " "); |
| 24 | } |
| 25 | } |
| 26 | } |
nothing calls this directly
no outgoing calls
no test coverage detected