-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.java
42 lines (36 loc) · 1002 Bytes
/
SelectionSort.java
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
// AUTHOR: Soel Micheletti
import java.util.Random;
class SelectionSort{
public static int[] selectionSort(int[] a) {
for(int i = a.length - 1; i>= 0; i--) {
int max = a[0];
int index = 0;
for(int j = 0; j<= i; j++) {
if(a[j]>max) {
max = a[j];
index = j;
}
}
int tmp = a[i];
a[i] = max;
a[index] = tmp;
}
return a;
}
public static boolean isSorted(int[] a){
for(int i = 0; i < a.length - 1; i++){
if(a[i] > a[i + 1])
return false;
}
return true;
}
public static void main(String[] args) {
Random ran = new Random();
int[] a = new int[10000];
for(int i = 0; i < a.length; i++){
a[i] = ran.nextInt(10000);
}
selectionSort(a);
System.out.println(isSorted(a));
}
}