Skip to content

Commit 5d44e67

Browse files
committed
Optimized heapdown function in binary heap and heap sort.
1 parent 760a6d4 commit 5d44e67

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

Algorithms/Sorting/Sources/HeapSort.c

+11-14
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,17 @@ void heapUp(char *arr, int index, int elemSize, int (*cmp)(const void *, const v
133133
void heapDown(char *arr, int length, int index, int elemSize, int (*cmp)(const void *, const void *)) {
134134

135135
int fChildIndex = getFirstChildIndex(index), sChildIndex = getSecondChildIndex(index);
136-
int targetChildIndex =
137-
fChildIndex >= length && sChildIndex >= length // check if leaf node.
138-
? -1
139-
: fChildIndex < length && sChildIndex < length // check if the node has two children.
140-
? cmp(arr + fChildIndex * elemSize, arr + sChildIndex * elemSize) > 0 // check for the proper node index.
141-
? fChildIndex
142-
: sChildIndex
143-
: fChildIndex >= length //the node has only one child, and we want to return that child index.
144-
? sChildIndex
145-
: fChildIndex;
146-
147-
if (targetChildIndex != -1 && cmp(arr + targetChildIndex * elemSize, arr + index * elemSize) > 0) {
148-
swap(arr + index * elemSize, arr + targetChildIndex * elemSize, elemSize);
149-
heapDown(arr, length, targetChildIndex, elemSize, cmp);
136+
int target = index;
137+
138+
if (fChildIndex < length && cmp(arr + target * elemSize, arr + fChildIndex * elemSize) < 0)
139+
target = fChildIndex;
140+
141+
if (sChildIndex < length && cmp(arr + target * elemSize, arr + sChildIndex * elemSize) < 0)
142+
target = sChildIndex;
143+
144+
if (target != index) {
145+
swap(arr + index * elemSize, arr + target * elemSize, elemSize);
146+
heapDown(arr, length, target, elemSize, cmp);
150147
}
151148

152149
}

DataStructure/Trees/Sources/BinaryHeap.c

+9-12
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,17 @@ void binaryHeapDown(void **arr, int currentIndex, int length, int (*cmp)(const v
103103

104104
int fChildIndex = binaryHeapGetFChildIndex(currentIndex),
105105
sChildIndex = binaryHeapGetSChildIndex(currentIndex),
106-
targetChildIndex = -1;
106+
target = currentIndex;
107107

108-
if (fChildIndex < length || sChildIndex < length) {
108+
if (fChildIndex < length && cmp(arr[target], arr[fChildIndex]) < 0)
109+
target = fChildIndex;
109110

110-
if (fChildIndex < length && sChildIndex < length)
111-
targetChildIndex = cmp(arr[fChildIndex], arr[sChildIndex]) > 0 ? fChildIndex : sChildIndex;
112-
else
113-
targetChildIndex = fChildIndex >= length ? sChildIndex : fChildIndex;
111+
if (sChildIndex < length && cmp(arr[target], arr[sChildIndex]) < 0)
112+
target = sChildIndex;
114113

115-
}
116-
117-
if (targetChildIndex != -1 && cmp(arr[targetChildIndex], arr[currentIndex]) > 0) {
118-
binaryHeapSwap(arr, currentIndex, targetChildIndex);
119-
binaryHeapDown(arr, targetChildIndex, length, cmp);
114+
if (target != currentIndex) {
115+
binaryHeapSwap(arr, currentIndex, target);
116+
binaryHeapDown(arr, target, length, cmp);
120117
}
121118

122119
}
@@ -260,7 +257,7 @@ void binaryHeapInsertAll(BinaryHeap *heap, void **items, int length) {
260257
if (items[i] == NULL) {
261258
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
262259
ERROR_TEST->errorCode = INVALID_ARG;
263-
return;
260+
return;
264261
#else
265262
fprintf(stderr, INVALID_ARG_MESSAGE, "item pointer", "binary heap data structure");
266263
exit(INVALID_ARG);

0 commit comments

Comments
 (0)