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

Commit 93c459f

Browse files
author
Ram swaroop
committed
code refactoring
1 parent 919b8cf commit 93c459f

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

src/me/ramswaroop/arrays/KthLargestElement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ public static int getKthLargestElement(int[] a, int k) {
4444
MaxHeap.buildMaxHeap(a);
4545
if (k == 1) break;
4646

47-
MaxHeap.swap(a, 0, a.length - 1);
47+
swap(a, 0, a.length - 1);
4848
a = Arrays.copyOfRange(a, 0, a.length - 1);
4949
k--;
5050
}
5151

5252
return a[0];
5353
}
5454

55+
private static void swap(int[] a, int firstIndex, int secondIndex) {
56+
a[firstIndex] = a[firstIndex] + a[secondIndex];
57+
a[secondIndex] = a[firstIndex] - a[secondIndex];
58+
a[firstIndex] = a[firstIndex] - a[secondIndex];
59+
}
60+
5561
public static void main(String a[]) {
5662
int[] ar = new int[]{2, 4, 5, 7, 1, 8, 9};
5763
System.out.println(Arrays.toString(ar));

src/me/ramswaroop/arrays/sorting/HeapSort.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,47 @@ public static void heapSort(int[] a) {
2424
MaxHeap.buildMaxHeap(a);
2525

2626
for (int i = a.length - 1; i > 0; i--) {
27-
MaxHeap.swap(a, 0, i);
28-
MaxHeap.maxHeapify(a, 0, i);
27+
swap(a, 0, i);
28+
maxHeapify(a, 0, i);
2929
}
3030
}
3131

32+
/**
33+
* Makes the array {@param a} satisfy the max heap property starting from
34+
* {@param index} till {@param end} position in array.
35+
* <p/>
36+
* See this {@link MaxHeap#maxHeapify} for a basic version of maxHeapify.
37+
* <p/>
38+
* Time complexity: O(log n).
39+
*
40+
* @param a
41+
* @param index
42+
* @param end
43+
*/
44+
private static void maxHeapify(int[] a, int index, int end) {
45+
int largest = index;
46+
int leftIndex = 2 * index + 1;
47+
int rightIndex = 2 * index + 2;
48+
49+
if (leftIndex < end && a[index] < a[leftIndex]) {
50+
largest = leftIndex;
51+
}
52+
if (rightIndex < end && a[largest] < a[rightIndex]) {
53+
largest = rightIndex;
54+
}
55+
56+
if (largest != index) {
57+
swap(a, index, largest);
58+
maxHeapify(a, largest, end);
59+
}
60+
}
61+
62+
private static void swap(int[] a, int firstIndex, int secondIndex) {
63+
a[firstIndex] = a[firstIndex] + a[secondIndex];
64+
a[secondIndex] = a[firstIndex] - a[secondIndex];
65+
a[firstIndex] = a[firstIndex] - a[secondIndex];
66+
}
67+
3268
public static void main(String a[]) {
3369
int[] ar = new int[]{2, 5, 1, 7, 9, 4};
3470
System.out.println(Arrays.toString(ar));

src/me/ramswaroop/common/MaxHeap.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,29 @@ public class MaxHeap {
2929
* Makes the array {@param a} satisfy the max heap property starting from
3030
* {@param index} till the end of array.
3131
* <p/>
32-
* Time complexity: O(log n).
33-
*
34-
* @param a
35-
* @param index
36-
*/
37-
public static void maxHeapify(int[] a, int index) {
38-
maxHeapify(a, index, a.length);
39-
}
40-
41-
/**
42-
* Makes the array {@param a} satisfy the max heap property starting from
43-
* {@param index} till {@param l} position in array.
32+
* See {@link me.ramswaroop.arrays.sorting.HeapSort#maxHeapify} for a modified
33+
* version of maxHeapify.
4434
* <p/>
4535
* Time complexity: O(log n).
4636
*
4737
* @param a
4838
* @param index
49-
* @param l
5039
*/
51-
public static void maxHeapify(int[] a, int index, int l) {
40+
public static void maxHeapify(int[] a, int index) {
5241
int largest = index;
5342
int leftIndex = 2 * index + 1;
5443
int rightIndex = 2 * index + 2;
5544

56-
if (leftIndex < l && a[index] < a[leftIndex]) {
45+
if (leftIndex < a.length && a[index] < a[leftIndex]) {
5746
largest = leftIndex;
5847
}
59-
if (rightIndex < l && a[largest] < a[rightIndex]) {
48+
if (rightIndex < a.length && a[largest] < a[rightIndex]) {
6049
largest = rightIndex;
6150
}
6251

6352
if (largest != index) {
6453
swap(a, index, largest);
65-
maxHeapify(a, largest, l);
54+
maxHeapify(a, largest);
6655
}
6756
}
6857

@@ -79,7 +68,7 @@ public static void buildMaxHeap(int[] a) {
7968
}
8069
}
8170

82-
public static void swap(int[] a, int firstIndex, int secondIndex) {
71+
private static void swap(int[] a, int firstIndex, int secondIndex) {
8372
a[firstIndex] = a[firstIndex] + a[secondIndex];
8473
a[secondIndex] = a[firstIndex] - a[secondIndex];
8574
a[firstIndex] = a[firstIndex] - a[secondIndex];

src/me/ramswaroop/common/MinHeap.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,20 @@ public class MinHeap {
3535
* @param index
3636
*/
3737
public static void minHeapify(int[] a, int index) {
38-
minHeapify(a, index, a.length);
39-
}
40-
41-
/**
42-
* Makes the array {@param a} satisfy the min heap property starting from
43-
* {@param index} till {@param l} position in array.
44-
* <p/>
45-
* Time complexity: O(log n).
46-
*
47-
* @param a
48-
* @param index
49-
*/
50-
public static void minHeapify(int[] a, int index, int l) {
5138
int smallest = index;
5239
int leftIndex = 2 * index + 1;
5340
int rightIndex = 2 * index + 2;
5441

55-
if (leftIndex < l && a[index] > a[leftIndex]) {
42+
if (leftIndex < a.length && a[index] > a[leftIndex]) {
5643
smallest = leftIndex;
5744
}
58-
if (rightIndex < l && a[smallest] > a[rightIndex]) {
45+
if (rightIndex < a.length && a[smallest] > a[rightIndex]) {
5946
smallest = rightIndex;
6047
}
6148

6249
if (smallest != index) {
6350
swap(a, index, smallest);
64-
minHeapify(a, smallest, l);
51+
minHeapify(a, smallest);
6552
}
6653
}
6754

0 commit comments

Comments
 (0)