-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
29 lines (24 loc) · 843 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
29 lines (24 loc) · 843 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
import java.util.Arrays;
public class SelectionSort {
private void sort(int[] array) {
int minIndex, temp;
for (int i = 0; i < array.length; i++) {
minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
public static void main(String[] args) {
SelectionSort obj = new SelectionSort();
int[] array = {88, 55, 23, 76, 100, 99};
System.out.println("Before Sorting: " + Arrays.toString(array));
obj.sort(array);
System.out.println("After Sorting: " + Arrays.toString(array));
}
}