Skip to content

Commit 38925de

Browse files
committed
Merge pull request xtaci#43 from yanzhe-chen/heap-bug-fix
Fix two bugs in heap implementation.
2 parents 0359b8d + cc7793c commit 38925de

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LIBS = -lm
1313
PROGRAMS = m_based_demo \
1414
integer_demo \
1515
insertion_sort_demo \
16+
shell_sort_demo \
1617
radix_sort_demo \
1718
shuffle_demo \
1819
quick_sort_demo \
@@ -81,6 +82,9 @@ integer_demo: $(SRCDIR)/integer_demo.cpp
8182
insertion_sort_demo: $(SRCDIR)/insertion_sort_demo.cpp
8283
$(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS)
8384

85+
shell_sort_demo: $(SRCDIR)/shell_sort_demo.cpp
86+
$(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS)
87+
8488
radix_sort_demo: $(SRCDIR)/radix_sort_demo.cpp
8589
$(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS)
8690

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
Bubble sort
4242
Selection sort
4343
Insertion sort
44+
Shell sort
4445
Radix sort
4546
Quick sort
4647
Merge sort

include/heap.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace alg {
6161
public:
6262
Heap(int max) {
6363
m_size = 0;
64-
m_max = max+1;
64+
m_max = max;
6565
m_heap = new elem[m_max];
6666
};
6767

@@ -101,7 +101,7 @@ namespace alg {
101101
inline void clear() { m_size = 0; }
102102

103103
bool contains(const T & data) {
104-
for(int i=1;i<=m_size;i++) {
104+
for(int i=0;i<m_size;i++) {
105105
if(m_heap[i].data== data) return true;
106106
}
107107
return false;
@@ -166,7 +166,7 @@ namespace alg {
166166
}
167167

168168
int j = j1;
169-
int j2 = j1+1; // left child
169+
int j2 = j1+1; // right child
170170
if (j2 < n && !less(j1,j2)) {
171171
j = j2; // choose the minium one.
172172
}

include/shell_sort.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************************
2+
* DANIEL'S ALGORITHM IMPLEMENTAIONS
3+
*
4+
* /\ | _ _ ._ o _|_ |_ ._ _ _
5+
* /--\ | (_| (_) | | |_ | | | | | _>
6+
* _|
7+
*
8+
* SHELL SORT
9+
*
10+
* 1. sort array in O(n^(3/2)) time.
11+
*
12+
* https://en.wikipedia.org/wiki/Shellsort
13+
*
14+
******************************************************************************/
15+
16+
#ifndef __SHELL_SORT_H__
17+
#define __SHELL_SORT_H__
18+
19+
namespace alg {
20+
/**
21+
* shell sort an array
22+
*/
23+
template<typename T>
24+
static void shell_sort(T *array, int len) {
25+
int h = 1;
26+
while (h < len / 3) {
27+
h = 3 * h + 1; // 1, 4, 13, 40, 121, ...
28+
}
29+
while (h >= 1) {
30+
for (int i = h; i < len; i++) {
31+
int cur = array[i];
32+
int j = i - h;
33+
while (j >= 0 && array[j] > cur) {
34+
array[j + h] = array[j];
35+
j = j - h;
36+
}
37+
array[j + h] = cur;
38+
}
39+
h = h / 3;
40+
}
41+
}
42+
}
43+
44+
#endif //

src/shell_sort_demo.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
5+
#include "generic.h"
6+
#include "shell_sort.h"
7+
8+
using namespace alg;
9+
10+
int main()
11+
{
12+
const int MAX_ELEMENTS = 10;
13+
int list[MAX_ELEMENTS];
14+
15+
int i = 0;
16+
17+
srand(time(NULL));
18+
// generate random numbers and fill them to the list
19+
for(i = 0; i < MAX_ELEMENTS; i++ ){
20+
list[i] = rand()%100;
21+
}
22+
printf("The list before sorting is:\n");
23+
printlist(list,MAX_ELEMENTS);
24+
25+
// sort the list using shell sort
26+
shell_sort<int>(&list[0],MAX_ELEMENTS);
27+
28+
// print the result
29+
printf("The list after sorting using shell sort algorithm:\n");
30+
printlist(list,MAX_ELEMENTS);
31+
return 0;
32+
}

0 commit comments

Comments
 (0)