-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSortR.java
32 lines (26 loc) · 870 Bytes
/
SelectionSortR.java
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
32
import java.util.Arrays;
public class selectionsortrecursion {
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1};
selectionSortRow(arr, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int left, int right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
static void selectionSortCol(int[] arr, int start, int end, int max) {
if (start > end) {
swap(arr, max, end);
return;
}
if (arr[start] >= arr[max]) max = start;
selectionSortCol(arr, start + 1, end, max);
}
static void selectionSortRow(int[] arr, int length) {
if (length == 0) return;
selectionSortCol(arr, 0, length, length-1);
selectionSortRow(arr, length - 1);
}
}