-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sorting.java
More file actions
34 lines (31 loc) · 888 Bytes
/
Copy pathSelection_Sorting.java
File metadata and controls
34 lines (31 loc) · 888 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
package dsa;
import java.util.Arrays;
public class Selection_Sorting {
public static void main(String[] args) {
int[] arr = {5,555, 4, 1, 2};
selction(arr);
System.out.println(Arrays.toString(arr));
}
static void selction(int[]arr){
// find the max item in the remaining array and swap with correct index
for (int i=0;i< arr.length;i++){
int last= arr.length-i-1;
int max=getMaxIndex(arr,0,last);
Swap(arr,max,last);
}
}
static void Swap(int[]arr,int first,int second){
int temp=arr[first];
arr[first]=arr[second];
arr[second]=temp;
}
static int getMaxIndex(int[] arr, int start, int end){
int max=start;
for (int i=start;i<end;i++){
if (arr[max]<arr[i]){
max=i;
}
}
return max;
}
}