-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
173 lines (136 loc) · 4.92 KB
/
Copy pathQuickSort.java
File metadata and controls
173 lines (136 loc) · 4.92 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package edu.wit.cs.comp2350;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
/* Sorts geographic points in place in an array
* by surface distance to a specific point
*
* Wentworth Institute of Technology
* COMP 2350
* Assignment 3
*
*/
public class A3 {
//TODO: document this method
public static void quickSort(Coord[] destinations) {
int left = 0;
int right = destinations.length-1;
quickSort(destinations,left,right); //passing to second method that will take left & right as parameters
}
private static void quickSort(Coord[] destinations, int left, int right) {
if(left < right) {
int pivot = partition(destinations, left, right);
quickSort(destinations, left, pivot-1); //allow arr to be broken into subarr's & sorted from both sides of pivot.
quickSort(destinations,pivot+1,right);
}
}
private static int partition(Coord[] destinations, int left, int right) {
Coord pivot = destinations[left];
int i = left;
for(int j=left+1;j<=right;j++) { //looping through piece of arr
if(destinations[j].getDist() <= pivot.getDist()) { //compare current to pivot
i = i+1; //if current < pivot - inc i
Coord temp = destinations[i];
destinations[i] = destinations[j]; //swap current with element at loc i
destinations[j] = temp;
}
}
Coord temp = destinations[i];
destinations[i] = destinations[left]; //place pivot correctly in arr
destinations[left] = temp;
return i;
}
//TODO: document this method
public static void randQuickSort(Coord[] destinations) {
int left = 0;
int right = destinations.length-1;
randQuickSort(destinations,left,right); // same idea as before w/o randoms
}
private static void randQuickSort(Coord[] destinations, int left, int right) {
if(left<right) {
int pivot = randPartition(destinations, left, right);
randQuickSort(destinations, left, pivot-1);
randQuickSort(destinations, pivot+1, right);
}
}
private static int randPartition(Coord[] destinations, int left, int right) {
Random ranIndex = new Random();
int z = ranIndex.nextInt((right-left)+1)+left; //get our pivot as a random index no matter what subarr we are in
Coord temp = destinations[left];
destinations[left] = destinations[z]; //swap the random with left for a more reliable quick-sort
destinations[z] = temp;
return partition(destinations, left, right); //didn't notice mechanics were the same until I read the textbook
}
/********************************************
*
* You shouldn't modify anything past here
*
********************************************/
// Call system sort with a lambda expression on the comparator
public static void systemSort(Coord[] destinations) {
Arrays.sort(destinations, (a, b) -> Double.compare(a.getDist(), b.getDist()));
}
// Insertion sort eventually sorts an array
public static void insertionSort(Coord[] a) {
for (int i = 1; i < a.length; i++) {
Coord tmpC = a[i];
int j;
for (j = i-1; j >= 0 && tmpC.getDist() < a[j].getDist(); j--)
a[j+1] = a[j];
a[j+1] = tmpC;
}
}
private static Coord getOrigin(Scanner s) {
double lat = s.nextDouble();
double lon = s.nextDouble();
Coord ret = new Coord(lat, lon);
return ret;
}
private static Coord[] getDests(Scanner s, Coord start) {
ArrayList<Coord> a = new ArrayList<>();
while (s.hasNextDouble()) {
a.add(new Coord(s.nextDouble(), s.nextDouble(), start));
}
Coord[] ret = new Coord[a.size()];
a.toArray(ret);
return ret;
}
private static void printCoords(Coord start, Coord[] a) {
System.out.println(start.toColorString("black"));
for (int i = 0; i < a.length; ++i) {
System.out.println(a[i].toColorString("red"));
}
System.out.println();
System.out.println("Paste these results into http://www.hamstermap.com/custommap.html if you want to visualize the coordinates.");
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.printf("Enter the sorting algorithm to use [i]nsertion sort, [q]uicksort, [r]andomized quicksort, or [s]ystem quicksort): ");
char algo = s.next().charAt(0);
System.out.printf("Enter your starting coordinate in \"latitude longitude\" format as doubles: (e.g. 42.3366322 -71.0942150): ");
Coord start = getOrigin(s);
System.out.printf("Enter your end coordinates one at a time in \"latitude longitude\" format as doubles: (e.g. 38.897386 -77.037400). End your input with a non-double character:%n");
Coord[] destinations = getDests(s, start);
s.close();
switch (algo) {
case 'i':
insertionSort(destinations);
break;
case 'q':
quickSort(destinations);
break;
case 'r':
randQuickSort(destinations);
break;
case 's':
systemSort(destinations);
break;
default:
System.out.println("Invalid search algorithm");
System.exit(0);
break;
}
printCoords(start, destinations);
}
}