-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
39 lines (32 loc) · 897 Bytes
/
InsertionSort.java
File metadata and controls
39 lines (32 loc) · 897 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
31
32
33
34
35
36
37
38
39
// AUTHOR: Soel Micheletti
import java.util.Random;
class InsertionSort{
public static int[] insertionSort(int[] a) {
for(int i = 1; i<a.length; i++) {
int j = i;
while(j > 0 && a[j] < a[j-1]) {
int tmp = a[j];
a[j] = a[j-1];
a[j-1] = tmp;
j--;
}
}
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);
}
insertionSort(a);
System.out.println(isSorted(a));
}
}