-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSelectioSort.java
More file actions
30 lines (27 loc) · 942 Bytes
/
SelectioSort.java
File metadata and controls
30 lines (27 loc) · 942 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
import java.util.Arrays;
public class SelectionSort {
public static void printArray(int array[]) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void main(String[] args) {
int array[] = { 21, 16, 34, 17, 4, 1, 78 };
// Iteration till length-1
for (int i = 0; i < array.length; i++) {
int smallest = i;
// iteration for comparing element and finding the i+1 th smallest element;
for (int j = i + 1; j < array.length; j++) {
if (array[smallest] > array[j]) {
smallest = j;
}
}
int temp = array[smallest];
array[smallest] = array[i];
array[i] = temp;
}
// System.out.println(Arrays.toString(array)); // it will convert array into
// string
printArray(array);
}
}