forked from InsideEmpire/CS61B-PathwayToSuccess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
executable file
·100 lines (89 loc) · 3.57 KB
/
Copy pathQuickSort.java
File metadata and controls
executable file
·100 lines (89 loc) · 3.57 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
import edu.princeton.cs.algs4.Queue;
public class QuickSort {
/**
* Returns a new queue that contains the given queues catenated together.
*
* The items in q2 will be catenated after all of the items in q1.
*/
private static <Item extends Comparable> Queue<Item> catenate(Queue<Item> q1, Queue<Item> q2) {
Queue<Item> catenated = new Queue<Item>();
for (Item item : q1) {
catenated.enqueue(item);
}
for (Item item: q2) {
catenated.enqueue(item);
}
return catenated;
}
/** Returns a random item from the given queue. */
private static <Item extends Comparable> Item getRandomItem(Queue<Item> items) {
int pivotIndex = (int) (Math.random() * items.size());
Item pivot = null;
// Walk through the queue to find the item at the given index.
for (Item item : items) {
if (pivotIndex == 0) {
pivot = item;
break;
}
pivotIndex--;
}
return pivot;
}
/**
* Partitions the given unsorted queue by pivoting on the given item.
*
* @param unsorted A Queue of unsorted items
* @param pivot The item to pivot on
* @param less An empty Queue. When the function completes, this queue will contain
* all of the items in unsorted that are less than the given pivot.
* @param equal An empty Queue. When the function completes, this queue will contain
* all of the items in unsorted that are equal to the given pivot.
* @param greater An empty Queue. When the function completes, this queue will contain
* all of the items in unsorted that are greater than the given pivot.
*/
private static <Item extends Comparable> void partition(
Queue<Item> unsorted, Item pivot,
Queue<Item> less, Queue<Item> equal, Queue<Item> greater) {
while (!unsorted.isEmpty()) {
Item temp = unsorted.dequeue();
if (temp.compareTo(pivot) < 0) {
less.enqueue(temp);
} else if (temp.compareTo(pivot) > 0) {
greater.enqueue(temp);
} else {
equal.enqueue(temp);
}
}
}
/** Returns a Queue that contains the given items sorted from least to greatest. */
public static <Item extends Comparable> Queue<Item> quickSort(
Queue<Item> items) {
Queue<Item> copy = new Queue<>();
for (Item item : items) {
copy.enqueue(item);
}
if (copy.isEmpty() || copy.size() == 1) {
return copy;
}
Queue<Item> less = new Queue<>();
Queue<Item> equal = new Queue<>();
Queue<Item> greater = new Queue<>();
partition(copy, getRandomItem(copy), less, equal, greater);
less = quickSort(less);
greater = quickSort(greater);
return catenate(less, catenate(equal, greater));
}
public static void main(String[] args) {
Queue<String> students = new Queue<>();
students.enqueue("Vanessa");
students.enqueue("Alice");
students.enqueue("Josh");
students.enqueue("Garrison");
students.enqueue("Ethan");
Queue<String> sortedStudents = quickSort(students);
System.out.println("Original: " + students);
System.out.println("Original size: " + students.size());
System.out.println("Sorted: " + sortedStudents);
System.out.println("Sorted size: " + sortedStudents.size());
}
}