-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA4_SelectionSort.java
More file actions
38 lines (29 loc) · 951 Bytes
/
DSA4_SelectionSort.java
File metadata and controls
38 lines (29 loc) · 951 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
import java.util.Arrays;
public class DSA4_SelectionSort {
public static void main(String[] args) {
int [] arr = {10 ,45,64 ,67 ,9, 3, 5, 2 ,0 ,-2};
Selection(arr);
System.out.println(Arrays.toString(arr));
}
static void Selection(int arr[]){
for (int i = 0; i < arr.length; i++) {
int end = arr.length - i -1;
int maxIndex = MAXindex(arr , 0 , end);
swap(arr, maxIndex, end);
}
}
static int MAXindex(int [] arr ,int start ,int end){
int max = start ;
for (int index = start; index <= end; index++) {
if (arr[max]< arr[index]){
max = index ;
}
}
return max ;
}
static void swap (int [] arr , int first ,int second){
int temp = arr[first];
arr[first]= arr[second];
arr[second]= temp;
}
}