-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergesort.java
More file actions
76 lines (65 loc) · 1.97 KB
/
Copy pathMergesort.java
File metadata and controls
76 lines (65 loc) · 1.97 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package chapter2;
import java.util.concurrent.ThreadLocalRandom;
public class Mergesort {
//typically (n log n)
private static int origin_list [] = new int [10000000];
public static void main (String args[]) {
//generate 1000 random numbers from 1-1000 and put them into an array
for(int i = 0; i < 10000000; i++) {
origin_list[i] = ThreadLocalRandom.current().nextInt(1, 9999999 + 1);
}
//call the sort function
int [] sorted = mergesort(origin_list);
//check if the array is sorted by
/*for(int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}*/
}
private static int [] mergesort(int [] list) {
//System.out.println("size: " + list.length + "\t");
if(list.length < 1) {
//error
return null;
}
if(list.length == 1) {
return list;
}
int listLength = list.length / 2;
if(list.length % 2 == 1) {
listLength = list.length / 2 + 1;
}
int [] listA = new int [list.length / 2];
int [] listB = new int [listLength];
for(int i = 0, a = 0; i < list.length/2; i++) {
listA[a++] = list[i];
}
for(int i = list.length/2, b = 0; i < list.length; i++) {
listB[b++] = list[i];
}
//repeat above step until all sub arrays have a length of 1
listA = mergesort(listA);
listB = mergesort(listB);
//then join them back together in reverse order
return merge(listA, listB);
}
private static int [] merge(int [] listA, int [] listB) {
int size = listA.length + listB.length;
int merged [] = new int [size];
for(int i = 0, a = 0, b = 0; i < merged.length; i++) {
//if one list reaches the end, put other list elements into merged list and exit
if(listA.length == a) {
merged[i] = listB[b++];
continue;
} else if (listB.length == b) {
merged[i] = listA[a++];
continue;
}
if(listA[a] < listB[b]) {
merged[i] = listA[a++];
} else {
merged[i] = listB[b++];
}
}
return merged;
}
}