Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 63160f0

Browse files
committed
Minor improvements
1 parent c8d2e45 commit 63160f0

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/main/java/com/rampatra/common/MinHeap.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.util.Arrays;
44

55
/**
6-
* Created by IntelliJ IDEA.
7-
* <p/>
86
* A HEAP is a specialized tree-based ABSTRACT DATA TYPE that satisfies the heap property:
97
* min-heap: All non-leaf elements are either smaller than or equal to their left and right child.
108
* max-heap: All non-leaf elements are either greater than or equal to their left and right child.
@@ -16,17 +14,16 @@
1614
* Each successor can be found in O(log n). The algorithm in minHeapify() takes O(log n) time
1715
* Therefore, buildMinHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME.
1816
* <p/>
19-
* Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue.
17+
* Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue.
18+
* <a href="@see http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap07.htm">Learn more</a>
2019
*
21-
* @author: ramswaroop
22-
* @date: 8/2/15
23-
* @time: 11:57 AM
24-
* @see: http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap07.htm
20+
* @author rampatra
21+
* @since 8/2/15
2522
*/
2623
public class MinHeap {
2724

28-
int[] heap;
29-
int size;
25+
private int[] heap;
26+
private int size;
3027

3128
public MinHeap(int[] heap) {
3229
this.size = heap.length;
@@ -71,7 +68,7 @@ public void buildMinHeap() {
7168
}
7269

7370
public void insert(int elem) {
74-
heap = Arrays.copyOf(heap, size + 1);
71+
heap = Arrays.copyOf(heap, 2 * size);
7572
int i = size;
7673
int parentIndex = (int) Math.floor((i - 1) / 2);
7774
while (i > 0 && elem < heap[parentIndex]) {
@@ -143,4 +140,4 @@ public static void main(String[] args) {
143140
minHeap.insert(0);
144141
minHeap.printHeap();
145142
}
146-
}
143+
}

src/main/java/com/rampatra/sorting/HeapSort.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
/**
88
* Created by IntelliJ IDEA.
99
*
10-
* @author: ramswaroop
11-
* @date: 8/3/15
12-
* @time: 3:13 PM
10+
* @author rampatra
11+
* @since 8/3/15
1312
*/
1413
public class HeapSort {
1514

@@ -41,7 +40,7 @@ public static void heapSort(int[] a) {
4140
* @param index
4241
* @param end
4342
*/
44-
private static void maxHeapify(int[] a, int index, int end) {
43+
public static void maxHeapify(int[] a, int index, int end) {
4544
int largest = index;
4645
int leftIndex = 2 * index + 1;
4746
int rightIndex = 2 * index + 2;
@@ -76,7 +75,7 @@ private static void swap(int[] a, int firstIndex, int secondIndex) {
7675
a[firstIndex] = a[firstIndex] - a[secondIndex];
7776
}
7877

79-
public static void main(String a[]) {
78+
public static void main(String[] a) {
8079
int[] ar = new int[]{2, 5, 1, 7, 9, 4};
8180
System.out.println(Arrays.toString(ar));
8281
heapSort(ar);

0 commit comments

Comments
 (0)