I am trying to compare the heap and merge sort using their runtimes and have the program all created. However, the final data I receive, sending to a new file, just does not make sense to me. Maybe I have just been staring at this for too long but I cannot seem to figure out what is the issue. Thank you in advance
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include <iostream>
#include <math.h>
#include <chrono>
#include <ctime>
#include <vector>
#include <cstdlib>
using namespace std;
inline int parent(int i) {return ((i - 1) / 2);}
inline int LEFT(int i) {return (2 * i + 1);}
inline int RIGHT(int i) {return (2 * i + 2);}
void merge(int list[], int p, int q, int r)
{
int n1=q-p+1;
int n2=r-q;
int L[n1+1];
int R[n2+1];
for(int i=0;i<n1; i++)
{
L[i]=list[p+i];
}
for(int j=0;j<n2; j++)
{
R[j]=list[q+1+j];
}
int largest;
if(L[n1-1]<R[n2-1]) largest=R[n2-1];
else largest=L[n1-1];
L[n1]=largest+1;
R[n2]=largest+1;
int i=0;
int j=0;
for(int k=p; k<=r; k++)
{
if (L[i]<=R[j])
{
list[k]=L[i];
i++;
}
else
{
list[k]=R[j];
j++;
}
}
}
void merge_sort_aux(int list[], int p, int r)
{
if(p<r)
{
int q=floor((p+r)/2);
merge_sort_aux(list,p,q);
merge_sort_aux(list,q+1,r);
merge(list,p,q,r);
}
}
void merge_sort(int list[], int size)
{
merge_sort_aux(list, 0, size - 1);
}
class heap
{
public:
int *nodes;
int length;
int heap_size;
};
void max_heapify(heap list, int index)
{
int left,right,largest,exchange_temp;
left = LEFT(index);
right = RIGHT(index);
if(left <list.heap_size && list.nodes[left] > list.nodes[index])
{
largest = left;
} else
{
largest = index;
}
if(right <list.heap_size && list.nodes[right] > list.nodes[largest])
{
largest = right;
}
if(largest != index)
{
exchange_temp = list.nodes[index];
list.nodes[index] = list.nodes[largest];
list.nodes[largest] = exchange_temp;
max_heapify(list, largest);
}
}
void build_max_heap(heap list)
{
list.heap_size = list.length;
for(int i = floor(list.length/2); i>=0; i--)
{
max_heapify(list, i);
}
}
void heap_sort(int list[], int size)
{
int exchange_temp;
heap tempheap;
tempheap.length = size;
tempheap.nodes = list;
tempheap.heap_size = size;
build_max_heap(tempheap);
for(int i= tempheap.length - 1; i>=1; i--)
{
exchange_temp = tempheap.nodes[0];
tempheap.nodes[0] = tempheap.nodes[i];
tempheap.nodes[i] = exchange_temp;
tempheap.heap_size = tempheap.heap_size - 1;
max_heapify(tempheap,0);
}
}
int main()
{
std::chrono::high_resolution_clock::time_point t1,t2;
srand(time(0));
int npointsmax = 100000, nave = 100, npoints = 46;
double merge_timelist[npoints], heap_timelist[npoints];
int *rlist1= new int[npointsmax];
int *rlist2= new int[npointsmax];
int *rlist3= new int[npointsmax];
int *rlist4= new int[npointsmax];
int *rlist5= new int[npointsmax];
double nplist[npoints];
nplist[0] = 1;
for(int n=0;n<5;n++)
{
for(int j=2;j<11;j++)
{
nplist[9*n + j - 1] = j * pow(10,n);
}
}
int icounter = 0;
for (int npointsi : nplist)
{
double hptime,mgtime;
double hp_temp_timer = 0.0;
double mg_temp_timer = 0.0;
cout<<npointsi<<endl;
for(int j = 0; j< nave; j++)
{
for(int ii=0;ii<npointsi;ii++)
{
rlist1[ii]=rlist2[ii]=rlist3[ii]=rlist4[ii]=rlist5[ii]=rand() % 1000;
}
t1 = std::chrono::high_resolution_clock::now();
merge_sort(rlist1,npointsi);
t2 = std::chrono::high_resolution_clock::now();
mgtime = std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
mg_temp_timer += mgtime ;
t1 = std::chrono::high_resolution_clock::now();
heap_sort(rlist2,npointsi);
t2 = std::chrono::high_resolution_clock::now();
hptime = std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
hp_temp_timer += hptime ;
}
merge_timelist[icounter] = mg_temp_timer/nave;
heap_timelist[icounter] = hp_temp_timer/nave;
icounter++;
}
FILE * resultsfile;
resultsfile=fopen("results-comparison.txt","w");
for(int j=0;j< npoints;j++) fprintf(resultsfile, "%5e \t %10.2f \t %10.2f \n", merge_timelist[j], heap_timelist[j]);
fclose(resultsfile);
}
|
I get this as my outputfile:
0.000000e+000 0.00 1.00
0.000000e+000 0.00 1.00
0.000000e+000 0.00 1.00
0.000000e+000 156000.00 1.00
0.000000e+000 0.00 1.00
0.000000e+000 0.00 1.00
0.000000e+000 156000.00 1.00
0.000000e+000 0.00 1.00
0.000000e+000 0.00 1.00
1.560000e+005 0.00 1.00
0.000000e+000 156001.00 1.00
3.120000e+005 0.00 1.00
3.120000e+005 156001.00 1.00
1.560000e+005 312001.00 1.00
1.560010e+005 468000.00 1.00
1.560010e+005 312000.00 1.00
3.120010e+005 312000.00 1.00
0.000000e+000 624001.00 1.00
1.560000e+005 780002.00 1.00
3.120000e+005 1716003.00 1.00
1.092002e+006 1404003.00 1.00
1.092002e+006 2808005.00 1.00
1.092003e+006 3120005.00 1.00
1.404002e+006 3588006.00 1.00
1.716004e+006 4212007.00 1.00
2.028002e+006 4992010.00 1.00
2.496005e+006 5148009.00 1.00
2.496006e+006 6240010.00 1.00
9.360014e+006 24180045.00 1.00
8.736018e+006 22630038.00 1.00
1.076402e+007 29016052.00 1.00
1.341602e+007 36660065.00 1.00
1.638003e+007 45240082.00 1.00
1.934404e+007 53352093.00 1.00
2.199603e+007 61620114.00 1.00
2.527204e+007 70044125.00 1.00
2.823605e+007 78624140.00 1.00