Skip to content

Commit 79a5d20

Browse files
committed
Slight modifications to code
1 parent 951653f commit 79a5d20

File tree

2 files changed

+34
-57
lines changed

2 files changed

+34
-57
lines changed

src/main/java/com/thealgorithms/datastructures/heaps/LeonardoHeap.java

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
package com.thealgorithms.datastructures.heaps;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.Collections;
6-
import java.util.List;
7-
83
import com.thealgorithms.bitmanipulation.SingleBitOperations;
94
import com.thealgorithms.maths.LeonardoNumber;
5+
import java.util.ArrayList;
6+
import java.util.List;
107

8+
/**
9+
* Wikipedia: https://en.wikipedia.org/wiki/Smoothsort
10+
*/
1111
public class LeonardoHeap<T extends Comparable<T>> {
1212

1313
private int leonardoLevelTracker;
1414
private int leonardoHeapSize;
15-
private final List<T> leonardoHeap;
15+
private final List<T> leonardoHeap;
1616

1717
public LeonardoHeap() {
1818
this.leonardoHeap = new ArrayList<T>();
1919
this.leonardoLevelTracker = 0;
2020
this.leonardoHeapSize = 0;
2121
}
2222

23-
public static Integer[] getLeonardoNumbers() {
24-
Integer[] leonardoNumbers = {1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049, 242785, 392835, 635621, 1028457, 1664079, 2692537, 4356617, 7049155, 1405773, 18454929, 29860703, 48315633, 78176337, 126491971,
25-
204668309, 331160281, 535828591};
26-
27-
return leonardoNumbers;
28-
}
29-
3023
public int getHeapsize() {
3124
return this.leonardoHeapSize;
3225
}
@@ -41,12 +34,12 @@ private void decreaseLeonardoLevelTracker() {
4134
}
4235

4336
private void increaseLeonardoLevelTracker() {
44-
Integer[] consecutiveTreeIndices = findConsecutiveLeonardoTreeIndices(leonardoLevelTracker);
45-
if (consecutiveTreeIndices[0] != -1) {
37+
ArrayList<Integer> consecutiveTreeIndices = findConsecutiveLeonardoTreeIndices(leonardoLevelTracker);
38+
if (consecutiveTreeIndices.get(0) != -1) {
4639
// if 0th or 1st index is -1 that implies there are no concequtive trees
47-
leonardoLevelTracker = SingleBitOperations.clearBit(leonardoLevelTracker, consecutiveTreeIndices[0]);
48-
leonardoLevelTracker = SingleBitOperations.clearBit(leonardoLevelTracker, consecutiveTreeIndices[1]);
49-
leonardoLevelTracker = SingleBitOperations.setBit(leonardoLevelTracker, consecutiveTreeIndices[1] + 1);
40+
leonardoLevelTracker = SingleBitOperations.clearBit(leonardoLevelTracker, consecutiveTreeIndices.get(0));
41+
leonardoLevelTracker = SingleBitOperations.clearBit(leonardoLevelTracker, consecutiveTreeIndices.get(1));
42+
leonardoLevelTracker = SingleBitOperations.setBit(leonardoLevelTracker, consecutiveTreeIndices.get(1) + 1);
5043
} else if ((leonardoLevelTracker & 2) == 0) {
5144
leonardoLevelTracker = SingleBitOperations.setBit(leonardoLevelTracker, 1);
5245
} else {
@@ -102,7 +95,6 @@ private void shiftRootAndRestoreHeap() {
10295
Integer[] currentLeonardoTreeLevels = findAllLeonardoTreeIndices();
10396
int previousTreeSizeCumulative = 0;
10497
ArrayList<Integer> rootNodeIndices = new ArrayList<Integer>();
105-
Collections.reverse(Arrays.asList(currentLeonardoTreeLevels)); // To get the Levels in decreasing order of levels
10698

10799
// The number of roots are going to be same the the number of levels
108100
// iterate over the currentLeonardoTreeLevels and get roots
@@ -114,7 +106,7 @@ private void shiftRootAndRestoreHeap() {
114106

115107
int rootNodeIndexForHeapify = rootNodeIndices.getLast();
116108
int leonardoTreeLevelforHeapify = currentLeonardoTreeLevels[currentLeonardoTreeLevels.length - 1];
117-
boolean swaped =false;
109+
boolean swaped = false;
118110

119111
for (int i = 1; i < rootNodeIndices.size(); i++) {
120112

@@ -144,27 +136,24 @@ private void shiftRootAndRestoreHeap() {
144136
swaped = true;
145137
}
146138
j = j - 1;
147-
if(j > 0) {
139+
if (j > 0) {
148140
currentRootNodeIndex = rootNodeIndices.get(j);
149141
prevRootNodeIndex = rootNodeIndices.get(j - 1);
150-
}
151-
else{
142+
} else {
152143
// j = 0 reached the left most tree
153144
break;
154145
}
155146
}
156-
157-
if(swaped) {
147+
148+
if (swaped) {
158149
maxHeapifyLeonardoTree(rootNodeIndexForHeapify, leonardoTreeLevelforHeapify);
159150
swaped = false;
160151
}
161-
162152
}
163153

164154
maxHeapifyLeonardoTree(rootNodeIndexForHeapify, leonardoTreeLevelforHeapify); // In case of insert and no swap.
165-
166155
}
167-
156+
168157
private int getRightMostTree() {
169158
// Isolate the rightmost set bit
170159
int isolatedBit = leonardoLevelTracker & -leonardoLevelTracker;
@@ -178,17 +167,20 @@ private int getRightMostTree() {
178167
return position;
179168
}
180169

181-
private static Integer[] findConsecutiveLeonardoTreeIndices(int num) {
170+
private static ArrayList<Integer> findConsecutiveLeonardoTreeIndices(int num) {
182171
int prevOneIndex = -1;
183172
int currentLevel;
184173

185-
Integer[] answer = new Integer[] {-1, -1};
174+
ArrayList<Integer> answer = new ArrayList<Integer>();
175+
answer.add(-1);
176+
answer.add(-1);
177+
186178
for (int i = 0; num > 0; i++) {
187179
currentLevel = num & 1;
188180
if (currentLevel == 1) {
189181
if (prevOneIndex != -1) {
190-
answer[0] = prevOneIndex;
191-
answer[1] = i;
182+
answer.set(0, prevOneIndex);
183+
answer.set(1, i);
192184
}
193185
prevOneIndex = i;
194186
} else {
@@ -205,34 +197,24 @@ private void swap(int i, int j) {
205197
leonardoHeap.set(j, temp);
206198
}
207199

208-
// TODO use lists
209200
private Integer[] findAllLeonardoTreeIndices() {
210-
int setBitCount = 0;
211-
for (int i = 0; i < Integer.SIZE; i++) {
212-
if ((leonardoLevelTracker & (1 << i)) != 0) {
213-
setBitCount++;
214-
}
215-
}
216-
217-
Integer[] setBitIndexes = new Integer[setBitCount];
218-
int index = 0;
219-
for (int i = 0; i < Integer.SIZE; i++) {
201+
List<Integer> setBitIndexes = new ArrayList<>();
202+
for (int i = Integer.SIZE - 1; i >= 0; i--) {
220203
if ((leonardoLevelTracker & (1 << i)) != 0) {
221-
setBitIndexes[index++] = i;
204+
setBitIndexes.add(i);
222205
}
223206
}
224-
return setBitIndexes;
207+
return setBitIndexes.toArray(new Integer[0]);
225208
}
226209

227-
228-
public void insertElement(T element) {
210+
public void addElement(T element) {
229211
increaseLeonardoLevelTracker();
230212
leonardoHeap.add(element);
231213
increaseHeapSize();
232214
shiftRootAndRestoreHeap();
233215
}
234216

235-
public T deleteElement() {
217+
public T removeElement() {
236218
decreaseLeonardoLevelTracker();
237219
decreaseHeapSize();
238220
T element = leonardoHeap.removeLast();

src/main/java/com/thealgorithms/sorts/SmoothSort.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.thealgorithms.sorts;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.Collections;
6-
7-
import com.thealgorithms.bitmanipulation.SingleBitOperations;
83
import com.thealgorithms.datastructures.heaps.LeonardoHeap;
94

105
/**
@@ -19,12 +14,12 @@ private static <T extends Comparable<T>> void smoothSort(T[] array) {
1914
int length = array.length;
2015
LeonardoHeap<T> leonardoHeap = new LeonardoHeap<T>();
2116

22-
for(int i = 0; i < length ; i++) {
23-
leonardoHeap.insertElement(array[i]);
17+
for (int i = 0; i < length; i++) {
18+
leonardoHeap.addElement(array[i]);
2419
}
2520

26-
for(int i = 0; i < length; i++) {
27-
T maxElement = leonardoHeap.deleteElement();
21+
for (int i = 0; i < length; i++) {
22+
T maxElement = leonardoHeap.removeElement();
2823
array[length - i - 1] = maxElement;
2924
}
3025
}

0 commit comments

Comments
 (0)