-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.c
More file actions
executable file
·76 lines (62 loc) · 1.78 KB
/
examples.c
File metadata and controls
executable file
·76 lines (62 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* examples.c
*
* Created on: Sep 22, 2014
* Author: Christopher McGrath
*/
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "examples.h"
#include "ArrayInteger.h"
#include "Heap.h"
#define INIT_SIZE 10
#define CHANGE_IN_SIZE 10
// Sample program that uses a min heap and a max heap to create
// an integer arrays sorted in increasing order and decreasing
// order respectively. The heap is built from an array holding 10
// random numbers, then 10 more numbers are added to each heap.
int exampleHeap(){
printf("Example Heap - - - - - - - - - - - - - - \n\n");
int* startingInts = malloc(sizeof(int) * 10);
srand(time(NULL));
int i = 0;
for (i = 0; i < INIT_SIZE; i += 1){
startingInts[i] = rand() % 50;
}
Heap* minHeap = H_buildHeap(MIN, startingInts, INIT_SIZE);
Heap* maxHeap = H_buildHeap(MAX, startingInts, INIT_SIZE);
for (i = 0; i < CHANGE_IN_SIZE; i += 1){
int n = rand() % 30;
H_insert(minHeap, n);
H_insert(maxHeap, n);
}
H_printHeap(minHeap);
H_printHeap(maxHeap);
int minSize = minHeap->size;
int* minSort = malloc(sizeof(int) * (minHeap->size));
int maxSize = maxHeap->size;
int* maxSort = malloc(sizeof(int) * (maxHeap->size));
for (i = 0; i < minSize; i += 1){
minSort[i] = 0;
}
for (i = 0; i < maxSize; i += 1){
maxSort[i] = 0;
}
i = 0;
while (!H_isEmpty(minHeap)){
minSort[i] = H_extractFirst(minHeap);
i += 1;
}
i = 0;
while (!H_isEmpty(maxHeap)){
maxSort[i] = H_extractFirst(maxHeap);
i += 1;
}
printf("Increasing order sort:\n");
AI_print(minSort, minSize);
printf("\nDecreasing order sort:\n");
AI_print(maxSort, maxSize);
printf("\nEnd Example Heap - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n");
return EXIT_SUCCESS;
}