Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ca8edc

Browse files
author
Harry Dulaney
committed
ch 23 05
1 parent d321928 commit 7ca8edc

File tree

4 files changed

+256
-1
lines changed

4 files changed

+256
-1
lines changed

‎ch_03/Exercise03_18.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ public static void main(String[] args) {
4141
System.out.println("With a package weight of " + weight + " your cost of shipping will be " + shipping);
4242
in.close();
4343
}
44-
}
44+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package ch_23.exercise23_05;
2+
3+
/**
4+
* A binary heap for inserting and deleting keys while maintaining
5+
* the sorted order
6+
* <p>
7+
* This Heap is for sorting Objects that implement the Comparable interface
8+
*
9+
* @param <E> The type of objects contained in the Heap
10+
*/
11+
public class ComparableHeap<E extends Comparable<E>> {
12+
private java.util.ArrayList<E> list = new java.util.ArrayList<>();
13+
14+
/**
15+
* Create a default heap
16+
*/
17+
public ComparableHeap() {
18+
}
19+
20+
/**
21+
* Create a heap from an array of objects
22+
*/
23+
public ComparableHeap(E[] objects) {
24+
for (int i = 0; i < objects.length; i++)
25+
add(objects[i]);
26+
}
27+
28+
/**
29+
* Add a new object into the heap
30+
*/
31+
public void add(E newObject) {
32+
list.add(newObject); // Append to the heap
33+
int currentIndex = list.size() - 1; // The index of the last node
34+
35+
while (currentIndex > 0) {
36+
int parentIndex = (currentIndex - 1) / 2;
37+
// Swap if the current object is greater than its parent
38+
if (list.get(currentIndex).compareTo(
39+
list.get(parentIndex)) > 0) {
40+
E temp = list.get(currentIndex);
41+
list.set(currentIndex, list.get(parentIndex));
42+
list.set(parentIndex, temp);
43+
} else
44+
break; // The tree is a heap now
45+
46+
currentIndex = parentIndex;
47+
}
48+
}
49+
50+
/**
51+
* Remove the root from the heap
52+
*/
53+
public E remove() {
54+
if (list.size() == 0) return null;
55+
56+
E removedObject = list.get(0);
57+
list.set(0, list.get(list.size() - 1));
58+
list.remove(list.size() - 1);
59+
60+
int currentIndex = 0;
61+
while (currentIndex < list.size()) {
62+
int leftChildIndex = 2 * currentIndex + 1;
63+
int rightChildIndex = 2 * currentIndex + 2;
64+
65+
// Find the maximum between two children
66+
if (leftChildIndex >= list.size()) break; // The tree is a heap
67+
int maxIndex = leftChildIndex;
68+
if (rightChildIndex < list.size()) {
69+
if (list.get(maxIndex).compareTo(
70+
list.get(rightChildIndex)) < 0) {
71+
maxIndex = rightChildIndex;
72+
}
73+
}
74+
75+
// Swap if the current node is less than the maximum
76+
if (list.get(currentIndex).
77+
78+
compareTo(
79+
list.get(maxIndex)) < 0) {
80+
E temp = list.get(maxIndex);
81+
list.set(maxIndex, list.get(currentIndex));
82+
list.set(currentIndex, temp);
83+
currentIndex = maxIndex;
84+
} else
85+
break; // The tree is a heap
86+
}
87+
88+
return removedObject;
89+
}
90+
91+
/**
92+
* Get the number of nodes in the tree
93+
*/
94+
public int getSize() {
95+
return list.size();
96+
}
97+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ch_23.exercise23_05;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* 23.5 (Generic heap sort) Write the following two generic methods using heap sort.
8+
* The first method sorts the elements using the Comparable interface and the
9+
* second uses the Comparator interface.
10+
* public static <E extends Comparable<E>>
11+
* void heapSort(E[] list)
12+
* public static <E> void heapSort(E[] list,
13+
* Comparator<? super E> comparator)
14+
* <p>
15+
* <p>
16+
* Binary Heap Animation:
17+
* <a href="https://yongdanielliang.github.io/animation/web/Heap.html">
18+
* https://yongdanielliang.github.io/animation/web/Heap.html </a>
19+
*/
20+
public class Exercise23_05 {
21+
22+
/**
23+
* Test Driver
24+
*/
25+
public static void main(String[] args) {
26+
Integer[] list = {-44, -5, -3, 3, 3, 1, -4, 0, 1, 2, 4, 5, 53};
27+
System.out.println("Sort using Comparable interface: ");
28+
heapSort(list);
29+
System.out.println(Arrays.toString(list));
30+
31+
Integer[] list2 = {44, 5, -3, 3, 5, 3, 12, -11, -55, 411, 125, 14, -4, 5, 66, 6, -6};
32+
System.out.println("Sort using Comparator interface: ");
33+
heapSort(list2, (o1, o2) -> {
34+
if (o1 > o2) return 1;
35+
if (o2 > o1) return -1;
36+
else return 0;
37+
});
38+
System.out.println(Arrays.toString(list2));
39+
}
40+
41+
public static <E extends Comparable<E>> void heapSort(E[] list) {
42+
ComparableHeap<E> comparableHeap = new ComparableHeap<>();
43+
for (E e : list)
44+
comparableHeap.add(e);
45+
for (int i = list.length - 1; i >= 0; i--)
46+
list[i] = comparableHeap.remove();
47+
}
48+
49+
50+
public static <E> void heapSort(E[] list,
51+
Comparator<? super E> comparator) {
52+
Heap<E> heap = new Heap<>(comparator);
53+
54+
for (E e : list)
55+
heap.add(e);
56+
for (int i = list.length - 1; i >= 0; i--)
57+
list[i] = heap.remove();
58+
59+
}
60+
}

‎ch_23/exercise23_05/Heap.java‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ch_23.exercise23_05;
2+
3+
4+
import java.util.Comparator;
5+
6+
public class Heap<E> {
7+
private java.util.ArrayList<E> list = new java.util.ArrayList<>();
8+
/**
9+
* Exercise23_05
10+
*/
11+
private final Comparator<? super E> comparator;
12+
13+
/**
14+
* Create a default heap
15+
* Exercise23_05
16+
*/
17+
public Heap(Comparator<? super E> comparator) {
18+
this.comparator = comparator;
19+
}
20+
21+
/**
22+
* Create a heap from an array of objects
23+
* Exercise23_05
24+
*/
25+
public Heap(E[] objects, Comparator<? super E> comparator) {
26+
this.comparator = comparator;
27+
for (int i = 0; i < objects.length; i++)
28+
add(objects[i]);
29+
}
30+
31+
/**
32+
* Add a new object into the heap
33+
*/
34+
public void add(E newObject) {
35+
list.add(newObject); // Append to the heap
36+
int currentIndex = list.size() - 1; // The index of the last node
37+
38+
while (currentIndex > 0) {
39+
int parentIndex = (currentIndex - 1) / 2;
40+
// Swap if the current object is greater than its parent
41+
// Exercise23_05
42+
if (comparator.compare(list.get(currentIndex), list.get(parentIndex)) > 0) {
43+
E temp = list.get(currentIndex);
44+
list.set(currentIndex, list.get(parentIndex));
45+
list.set(parentIndex, temp);
46+
} else
47+
break; // The tree is a heap now
48+
49+
currentIndex = parentIndex;
50+
}
51+
}
52+
53+
/**
54+
* Remove the root from the heap
55+
*/
56+
public E remove() {
57+
if (list.size() == 0) return null;
58+
59+
E removedObject = list.get(0);
60+
list.set(0, list.get(list.size() - 1));
61+
list.remove(list.size() - 1);
62+
63+
int currentIndex = 0;
64+
while (currentIndex < list.size()) {
65+
int leftChildIndex = 2 * currentIndex + 1;
66+
int rightChildIndex = 2 * currentIndex + 2;
67+
68+
// Find the maximum between two children
69+
if (leftChildIndex >= list.size()) break; // The tree is a heap
70+
int maxIndex = leftChildIndex;
71+
if (rightChildIndex < list.size()) {
72+
// Exercise 23 05
73+
if (comparator.compare(list.get(maxIndex), list.get(rightChildIndex)) < 0) {
74+
maxIndex = rightChildIndex;
75+
}
76+
}
77+
78+
// Swap if the current node is less than the maximum
79+
// Exercise23_05
80+
if (comparator.compare(list.get(currentIndex), list.get(maxIndex)) < 0) {
81+
E temp = list.get(maxIndex);
82+
list.set(maxIndex, list.get(currentIndex));
83+
list.set(currentIndex, temp);
84+
currentIndex = maxIndex;
85+
} else
86+
break; // The tree is a heap
87+
}
88+
89+
return removedObject;
90+
}
91+
92+
/**
93+
* Get the number of nodes in the tree
94+
*/
95+
public int getSize() {
96+
return list.size();
97+
}
98+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /