-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
31 lines (27 loc) · 882 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
31 lines (27 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Recursion;
import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {4, 3, 2, 1};
selection(arr, arr.length, 0, 0);
System.out.println(Arrays.toString(arr));
}
static void selection(int[] arr, int r, int c, int max){
if (r == 0){
return;
}
if (c < r){
if (arr[c] >= arr[max]){
selection(arr, r, c+1, c); // here c becomes the max
}else {
selection(arr, r, c+1, max);
}
} else { // means we have got our max already
// swapping the max to the last position of the array.
int temp = arr[max];
arr[max] = arr[r-1];
arr[r-1] = temp;
selection(arr, r-1, 0, 0); // then it will do the same thing
}
}
}