-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
129 lines (104 loc) · 3.46 KB
/
Sorting.java
File metadata and controls
129 lines (104 loc) · 3.46 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package finalproject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry; // You may need it to implement fastSort
public class Sorting {
/*
* input HashMap Comparable values
* returns ArrayList with keys from map ordered
* in descending order based on the values they mapped to
*
* O(n*log(n)), n is # pairs on map
*/
public static <K, V extends Comparable> ArrayList<K> fastSort(HashMap<K, V> results) {
//get the arrayList of URLs
ArrayList<K> unsorted = new ArrayList<K>();
unsorted.addAll(results.keySet());
//make it an array to sort
Object[] temp = unsorted.toArray();
//mergeSort
temp = sort(temp, 0, temp.length - 1, results);
//turn it back to a list
ArrayList<Object> sorted = new ArrayList<>(Arrays.asList(temp));
return (ArrayList<K>) sorted;
}
//slightly overcomplicated but rather typical merge()
private static <K, V extends Comparable> void merge(Object[] keys, int first, int mid, int last, HashMap<K, V> results) {
//find lengths of split arrays, n1 is first half n2 second half
int l1 = (mid - first) + 1;
int l2 = last - mid;
//create arrays with correct size
Object half1[] = new Object[l1];
Object half2[] = new Object[l2];
//fill respective arrays with correct values
for(int i = 0; i < l1; ++i) {
half1[i] = keys[first + i];
}
for(int c = 0; c < l2; ++c) {
half2[c] = keys[mid + 1 + c];
}
//counters for first & second array halves
int i = 0, c = 0;
int k = first;
//while they both have stuff
while(i < l1 && c < l2) {
//compare and set appropriately
if(results.get(half1[i]).compareTo(results.get(half2[c])) > 0) {
keys[k] = half1[i];
i++;
} else {
keys[k] = half2[c];
c++;
}
k++;
}
//if either half has anything left at the end add it on
while(i < l1 || c < l2) {
if(i < l1) {
keys[k] = half1[i];
i++;
k++;
} else {
keys[k] = half2[c];
c++;
k++;
}
}
}
/*
* input HashMap Comparable values
* returns ArrayList with keys from map ordered
* in descending order based on the values they mapped to
*
* O(n^2) bubble sort, n is # pairs on map
*/
public static <K, V extends Comparable> ArrayList<K> slowSort (HashMap<K, V> results) {
ArrayList<K> sortedUrls = new ArrayList<K>();
sortedUrls.addAll(results.keySet());
//bubble sort
int N = sortedUrls.size();
for(int i=0; i<N-1; i++){
for(int j=0; j<N-i-1; j++){
if(results.get(sortedUrls.get(j)).compareTo(results.get(sortedUrls.get(j+1))) <0){
K temp = sortedUrls.get(j);
sortedUrls.set(j, sortedUrls.get(j+1));
sortedUrls.set(j+1, temp);
}
}
}
return sortedUrls;
}
private static <K, V extends Comparable> Object[] sort(Object[] keys, int first, int last, HashMap<K, V> results) {
if (first < last) {
//find midpoint
int mid = (first + last) / 2;
//recursively sort first then second half
sort(keys, first, mid, results);
sort(keys, mid + 1, last, results);
//merge all the pieces back together
merge(keys, first, mid, last, results);
}
return keys;
}
}