forked from shikharuki/Hacktoberfest-2k21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
27 lines (24 loc) · 828 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
27 lines (24 loc) · 828 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
import java.util.Arrays;
public class SelectionSort {
void selectionSort(int array[]) {
int size = array.length;
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array[i] < array[min_idx]) {
min_idx = i;
}
}
int temp = array[step];
array[step] = array[min_idx];
array[min_idx] = temp;
}
}
public static void main(String args[]) {
int[] arr = {45,25,89,65,24,2, 4,55,656, 3, 5, 1, 8, 6, 7};
SelectionSort temp = new SelectionSort();
temp.selectionSort(arr);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(arr));
}
}