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 dc947e1

Browse files
Chapter 08 section 05 main test codes bug fixed.
1 parent d954fac commit dc947e1

File tree

1 file changed

+8
-5
lines changed
  • 08-Heap-and-Priority-Queue/05-Heapify-and-Replace-in-Heap/src

1 file changed

+8
-5
lines changed

‎08-Heap-and-Priority-Queue/05-Heapify-and-Replace-in-Heap/src/Main.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import java.util.Arrays;
12
import java.util.Random;
23

34
public class Main {
@@ -10,7 +11,7 @@ private static double testHeap(Integer[] testData, boolean isHeapify){
1011
if(isHeapify)
1112
maxHeap = new MaxHeap<>(testData);
1213
else{
13-
maxHeap = new MaxHeap<>();
14+
maxHeap = new MaxHeap<>(testData.length);
1415
for(int num: testData)
1516
maxHeap.add(num);
1617
}
@@ -34,14 +35,16 @@ public static void main(String[] args) {
3435
int n = 1000000;
3536

3637
Random random = new Random();
37-
Integer[] testData = new Integer[n];
38+
Integer[] testData1 = new Integer[n];
3839
for(int i = 0 ; i < n ; i ++)
39-
testData[i] = random.nextInt(Integer.MAX_VALUE);
40+
testData1[i] = random.nextInt(Integer.MAX_VALUE);
4041

41-
double time1 = testHeap(testData, false);
42+
Integer[] testData2 = Arrays.copyOf(testData1, n);
43+
44+
double time1 = testHeap(testData1, false);
4245
System.out.println("Without heapify: " + time1 + " s");
4346

44-
double time2 = testHeap(testData, true);
47+
double time2 = testHeap(testData2, true);
4548
System.out.println("With heapify: " + time2 + " s");
4649
}
4750
}

0 commit comments

Comments
(0)

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