Upload files to 'Exercise 2 - Heap (Week 3)'

master
Manish 3 years ago
parent 0336a17252
commit 0d4bb59657

@ -0,0 +1,97 @@
177
284
870
319
47
312
14
885
662
467
955
269
719
169
434
306
300
253
65
373
37
99
132
27
30
835
242
221
957
38
582
837
177
126
308
963
808
382
633
489
426
406
159
579
899
47
784
668
88
359
163
688
544
415
581
796
903
856
285
948
803
679
567
911
38
491
451
263
473
48
818
378
729
446
218
534
130
673
269
113
940
644
494
275
31
832
658
932
210
502
783
248
914
283
753
739
43

@ -0,0 +1,195 @@
/* License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html
*
* Exercise 2 - Implementing a heap (Week 3)
* Name: Manish
* Student Login: *****
*
* Compile as:
* $ gcc -std=c11 -Wall -o ex2 ex2.c
*/
// assert used only for testing heap property
// #include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int heap[100];
int size = 0;
void makeheap();
void shiftdown(int index);
// void shiftup(int index);
// void swap(int i, int j);
// int min(int i, int j);
// void test_heap();
// int is_bigger_or_equal(int bigger, int smaller);
int main(void)
{
printf("Enter file name: ");
char filename[257];
// Assuming filename/file path won't be longer than 256 characters
scanf("%256s", filename);
FILE* file = fopen(filename, "r");
if (!file)
{
perror(filename);
exit(EXIT_FAILURE);
}
// Trailing whitespace to consume all subsequent ' ', \t, \n, \r
while (fscanf(file, " %d ", &heap[size]) != EOF)
{
size++;
}
fclose(file);
makeheap();
// test_heap();
// int upto = min(5, size);
// Assuming at least 5 integers will be read (else segfault!)
for (int i = 0; i < 5; i++)
{
printf("%d ", heap[i]);
}
printf("\n");
return 0;
}
void makeheap()
{
// Shiftdown Method
int shiftdowns_required = (size / 2)-1;
for (int i = shiftdowns_required; i >= 0; i--)
{
shiftdown(i);
}
// Shiftup method
/*for (int i = 0; i < size; i++)
{
shiftup(i);
}*/
}
void shiftdown(int index)
{
int child;
int temp; // used for swapping
// Loop instead of recursion for efficiency
while (1)
{
child = (index * 2) + 1;
// Reached end of tree
if (child >= size)
{
return;
}
// Has only one child
if (child + 1 >= size)
{
}
// Has both children
else
{
// Pick the bigger child
if (heap[child] < heap[child + 1])
{
child = child + 1;
}
}
if (heap[index] < heap[child])
{
// swap
temp = heap[index];
heap[index] = heap[child];
heap[child] = temp;
index = child;
}
else
{
return;
}
}
}
/*
void shiftup(int index)
{
int parent;
int temp; // used for swapping
while (1)
{
if (index == 0)
{
return;
}
parent = (index - 1) / 2;
if (parent >= 0)
{
if (heap[parent] < heap[index])
{
// swap
temp = heap[index];
heap[index] = heap[parent];
heap[parent] = temp;
index = parent;
}
else
{
return;
}
}
else
{
return;
}
}
}*/
/*
inline void swap(int i, int j)
{
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}*/
/*
inline int min(int i, int j)
{
if (i < j)
{
return i;
}
return j;
}*/
/*
void test_heap()
{
int checks_required = (size/2)+1;
int child;
for (int i=0; i < checks_required; i++)
{
// Left child
child = (i*2)+1;
assert(is_bigger_or_equal(i, child));
// Right child
assert(is_bigger_or_equal(i, ++child));
}
}
inline int is_bigger_or_equal(int bigger, int smaller)
{
if (bigger < size && smaller < size)
{
return (heap[bigger] >= heap[smaller]);
}
return 1;
}
*/

@ -0,0 +1,2 @@
ex2: ex2.c
gcc -Wall -std=c11 -o ex2 ex2.c
Loading…
Cancel
Save