-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSortUsingRecursion.java
More file actions
43 lines (35 loc) · 1.01 KB
/
Copy pathSelectionSortUsingRecursion.java
File metadata and controls
43 lines (35 loc) · 1.01 KB
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
33
34
35
36
37
38
39
40
41
42
43
package Recursion;
/*
Selection sort = take the max ele put it at the last index then take the next max and put it in the second last index and so on
*/
import java.util.Arrays;
public class SelectionSortUsingRecursion {
public static void main(String[] args) {
int[] arr = {5,4,3,2,1};
System.out.println(Arrays.toString(sort(arr,arr.length-1)));
}
static int[] sort(int[] arr , int end){
// base condition:
if(end == 0)
return arr;
// for each index:
swap(arr,maxIndex(arr,end), end);
return sort(arr , end - 1);
}
static int maxIndex(int[] arr , int end){
int index = 0;
int max = Integer.MIN_VALUE;
for(int i = 0 ; i <= end ; i++) {
if (arr[i] > max) {
max = arr[i];
index = i;
}
}
return index;
}
static void swap(int[] arr , int st , int end){
int temp = arr[st];
arr[st] = arr[end];
arr[end]= temp;
}
}