-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySelectionSort.java
More file actions
57 lines (32 loc) · 991 Bytes
/
Copy pathMySelectionSort.java
File metadata and controls
57 lines (32 loc) · 991 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.*;
public class MySelectionSort{
//functions outside of the main have to be tagges as static functions
public static void main(String[] args) {
int[] arr1 = {3,2,1,5,4};
printArray(arr1);
for(int i = 0; i < arr1.length; i++) {
//have a function that returns the smallest value
//replace the arr[i] with that returned value
int smallestIndex = findSmallestIndex(arr1, i);
//have a function that switches the positions
arr1 = switchPositions(arr1, i, smallestIndex);
printArray(arr1);
}
printArray(arr1);
}
public static int[] switchPositions(int[] arr, int start, int smallest) {
int holder = arr[start];
arr[start] = arr[smallest];
arr[smallest] = holder;
return arr;
}
public static int findSmallestIndex(int[] arr, int start) {
int smallestIndex = start;
for(int i = start; i < arr.length; i++) {
if(arr[i] < arr[smallestIndex]) {
smallestIndex = i;
}
}
return smallestIndex;
}
}