-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection Sort
44 lines (34 loc) · 1.04 KB
/
Selection Sort
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
import java.util.Random;
public class SelectionSort {
public static void main(String[] args) {
Random r = new Random();
int size = 10;
int min = 0;
int[] array = new int[size];
System.out.print("Array: ");
for (int i = 0; i < size; i++) {
array[i] = r.nextInt(100) + 1;
System.out.print(" " + array[i]);
}
System.out.println("\n");
for (int x = 0; x < size; x++) {
min = x;
for (int y = (x+1); y < size; y++) {
if (array[y] < array[min]) {
min = y;
}
}
if (min != x) {
// XOR Swap Algorithm
array[x] = array[x] ^ array[min];
array[min] = array[x] ^ array[min];
array[x] = array[x] ^ array[min];
}
}
System.out.print("Sorted: ");
for (int x = 0; x < size; x++) {
System.out.print(array[x] + " ");
}
System.out.println();
}
}