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 88b6e31

Browse files
Merge pull request HarryDulaney#31 from HarryDulaney/Add_Chapter_22_23_24_25_answers
ch 23 05
2 parents 4f220ba + d93b5cd commit 88b6e31

File tree

6 files changed

+322
-3
lines changed

6 files changed

+322
-3
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+
}

‎ch_22/Exercise22_03.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
* So, Time complexity is O(n) because
2929
* as the length of the input grows, the time will increase at a linear rate
3030
* based on the size of s1.
31-
* The size of s2 is not relevant since we are only check that the string have the same starting char
32-
* and then if they do checking for equality of s1 substring and s2;
31+
* The size of s2 is not relevant to time complexity because we iterate based on the length of s1.
3332
*
3433
* --------------------------------- Additional efficiency ---------------------------------
3534
* This could be made more efficient by utilizing the constraint of neighboring characters must be

‎ch_22/Exercise22_04.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package ch_22;
2+
3+
4+
import java.util.Scanner;
5+
6+
/**
7+
* (Pattern matching) Write a program that prompts the user to enter two strings
8+
* and tests whether the second string is a substring of the first string. (Don’t use
9+
* the indexOf method in the String class.) Analyze the time complexity of
10+
* your algorithm. Here is a sample run of the program:
11+
* <p>
12+
* Enter a string s1: Mississippi
13+
* Enter a string s2: sip
14+
* matched at index 6
15+
* <p>
16+
* __________________________________________________________________________________________
17+
* ----------------------------- Time Complexity Analysis -----------------------------------
18+
* __________________________________________________________________________________________
19+
* Time complexity is: O(n) (Linear Time)
20+
* <p>
21+
* If s1.length is 100 and s2.length is 4:
22+
* Worst case is that the loop will execute over all values of s1
23+
* <p>
24+
* T(n) = 100 * (s1 String loop) = O(n)
25+
* <p>
26+
* So, Time complexity is O(n) because
27+
* as the length of the input grows, the time will increase at a linear rate
28+
* based on the size of s1.
29+
* The size of s2 is not relevant to time complexity because we iterate based on the length of s1.
30+
*/
31+
public class Exercise22_04 {
32+
public static void main(String[] args) {
33+
Scanner scanner = new Scanner(System.in);
34+
System.out.print("Enter a string s1: ");
35+
String s1 = scanner.nextLine();
36+
37+
System.out.print("Enter a string s2: ");
38+
String s2 = scanner.nextLine();
39+
int result = new Exercise22_04().indexOfSubstring(s1, s2);
40+
if (result == -1) {
41+
System.out.println("s2 is not a substring of s1...");
42+
return;
43+
}
44+
45+
System.out.println("matched at index " + result);
46+
scanner.close();
47+
}
48+
49+
private int indexOfSubstring(String s1, String s2) {
50+
int index = 0;
51+
int matchLength = s2.length();
52+
char s2Start = s2.charAt(0);
53+
while (index < (s1.length() - matchLength)) {
54+
/* Try to match first char in s2 with char at next index */
55+
if (s1.charAt(index) == s2Start) {
56+
String s1Sub = s1.substring(index, index + matchLength);
57+
if (s1Sub.equals(s2)) {
58+
return index;
59+
}
60+
}
61+
index++;
62+
}
63+
return -1;
64+
}
65+
}
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 によって変換されたページ (->オリジナル) /