forked from AdityaSubrahmanyaBhat/hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionRecursion.java
More file actions
39 lines (37 loc) · 872 Bytes
/
Copy pathselectionRecursion.java
File metadata and controls
39 lines (37 loc) · 872 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
32
33
34
35
36
37
38
39
package com.company;
import java.util.Arrays;
public class selectionRecursion {
public static void main(String[] args) {
int[] arr={3,1,2,5,4,6};
selection(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
static void selection(int[] arr,int s,int e)
{
if(e==0)
{
return;
}
int index=findMaxIndex(arr,s,e);
swap(arr,index,e);
selection(arr,s,e-1);
}
static int findMaxIndex(int[] arr,int s,int e)
{
int max=0;
for(int i=s;i<=e;i++)
{
if(arr[max]<arr[i])
{
max=i;
}
}
return max;
}
static void swap(int[] arr,int n1,int n2)
{
int t=arr[n1];
arr[n1]=arr[n2];
arr[n2]=t;
}
}