linking heapsort file and main file
I have a main.c file and a heap.c file. The heap.c file sorts data but it gets it's data from a pre-set size in the main function. I am trying to have it so that when I compile with:
Code:
gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR | MERG) *.c
so for example:
Code:
gcc -std=c99 -DRAND -DPRNT -DTYPE=float -DHEAP *.c
and run with:
It will use that input of 34000 with main.c and I believe pass the base address of the array, number of elements, size of each element, and the compare function into my heapsort function of heap.c
The main.c file has an if statement to pass this:
Code:
#elif defined HEAP
srtheap(a, nelem, sizeof(TYPE), compare);
So basically I want to get rid of the main function in my heap.c file and have it run off of the main.c file instead, hope this makes sense. The main.c file cannot and should not be changed, only the heap.c file.
main.c file:
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "srt.h"
int compare(const void *, const void *);
int main(int argc, char *argv[]) {
int nelem = argc == 2 ? atoi(argv[1]) : SHRT_MAX;
TYPE *a = calloc(nelem, sizeof(TYPE));
#ifdef RAND
for (int i = 0; i < nelem; ++i) {
a[i] = (TYPE)rand() / RAND_MAX;
}
#else
for (int i = 0; i < nelem; ++i) {
a[i] = i;
}
#endif
#if defined BUBB
srtbubb(a, nelem, sizeof(TYPE), compare);
#elif defined HEAP
srtheap(a, nelem, sizeof(TYPE), compare);
#elif defined INSR
srtinsr(a, nelem, sizeof(TYPE), compare);
#elif defined MERG
srtmerg(a, nelem, sizeof(TYPE), compare);
#else
qsort(a, nelem, sizeof(TYPE), compare);
#endif
#ifdef PRNT
for (int i = 0; i < nelem; ++i) {
printf("%f\n", a[i]);
}
#else
for (int i = 0; i < nelem - 1; ++i) {
if (a[i] > a[i + 1]) {
printf("fail\n");
goto end;
}
}
printf("pass\n");
#endif
end:
free(a);
return 0;
}
int compare(const void *p1, const void *p2) {
if (*(TYPE *)p1 < *(TYPE *)p2) {
return -5;
}
else if (*(TYPE *)p1 > *(TYPE *)p2) {
return +5;
}
return 0;
}
heap.c file with main function:
Code:
#include <stdio.h>
#include <stdlib.h>
void swap(void* ap, void* bp, size_t s){
int temp;
temp = *(int*)ap;
*(int*)ap = *(int*)bp;
*(int*)bp = temp;
}
int compar(void* ap, void* bp){
if(*(int*)ap < *(int*)bp){
return -5;
}
else if(*(int*)ap > *(int*)bp){
return 5;
}
return 0;
}
void printarray(void* root, size_t nelem){
int i;
for(i = 0; i < nelem; ++i){
printf(" %i ", *(int*)(root + 4*i));
}
printf("\n\n");
}
void shiftdown(void* root, void* base, size_t nelem, size_t size){
int leftIndex = ((base - root)/size * 2 + 1);
int rightIndex = ((base - root)/size * 2 + 2);
void* left;
void* right;
if(leftIndex < nelem){
left = leftIndex*size + root;
}
if(rightIndex < nelem){
right = rightIndex*size + root;
}
if(rightIndex < nelem){
if(compar(right, base) > 0){
if(compar(left, right) > 0){
swap(base, left, size);
}
else{
swap(base, right, size);
shiftdown(root, right, nelem, size);
}
}
}
if(leftIndex < nelem){
if(compar(left, base) > 0){
swap(base, left, size);
shiftdown(root, left, nelem, size);
}
}
return;
}
void heapify(void* root, void* base, size_t nelem, size_t size){
int heap;
heap = 1;
int baseIndex = (base - root)/size;
int leftIndex = ((base - root)/size * 2 + 1);
int rightIndex = ((base - root)/size * 2 + 2);
void* left;
void* right;
if(leftIndex < nelem)
left = leftIndex*size + root;
if(rightIndex < nelem)
right = rightIndex*size + root;
if(rightIndex < nelem){
if(compar(right, base) > 0){
swap(base, right, size);
heap = 0;
}
}
if(leftIndex < nelem){
if(compar(left, base) > 0){
swap(base, left, size);
heap = 0;
}
}
if(baseIndex + 1 < nelem)
heapify(root, base + size,nelem, size);
if(heap == 0)
heapify(root, root, nelem, size);
return;
}
void srtheap(void* base, size_t nelem, size_t size){
while(nelem > 1){
swap(base, base + (nelem-1) * size, size);
nelem--;
shiftdown(base,base,nelem,size);
}
}
int main(int argc, char *argv[])
{
int SIZE = 34000;
int a[SIZE];
int i;
for( i = 0; i < SIZE;i++){
a[i] = SIZE - i;
}
size_t size = 4;
size_t nelem = sizeof(a)/sizeof(a[0]);
void* base = malloc(sizeof(void));
base = (void*)a;
printarray(base, nelem);
srtheap(base, nelem, size);
printarray(base, nelem);
printf("Sorted\n");
system("PAUSE");
return 0;
}
heap.c file without main function - doesn't work, not sure how to do it but the problem is with the srtheap() function.
Code:
#include <stdio.h>
#include <stdlib.h>
void swap(void* ap, void* bp, size_t s){
int temp;
temp = *(int*)ap;
*(int*)ap = *(int*)bp;
*(int*)bp = temp;
}
int compar(void* ap, void* bp){
if(*(int*)ap < *(int*)bp){
return -5;
}
else if(*(int*)ap > *(int*)bp){
return 5;
}
return 0;
}
void printarray(void* root, size_t nelem){
int i;
for(i = 0; i < nelem; ++i){
printf(" %i ", *(int*)(root + 4*i));
}
printf("\n\n");
}
void shiftdown(void* root, void* base, size_t nelem, size_t size){
int leftIndex = ((base - root)/size * 2 + 1);
int rightIndex = ((base - root)/size * 2 + 2);
void* left;
void* right;
if(leftIndex < nelem){
left = leftIndex*size + root;
}
if(rightIndex < nelem){
right = rightIndex*size + root;
}
if(rightIndex < nelem){
if(compar(right, base) > 0){
if(compar(left, right) > 0){
swap(base, left, size);
}
else{
swap(base, right, size);
shiftdown(root, right, nelem, size);
}
}
}
if(leftIndex < nelem){
if(compar(left, base) > 0){
swap(base, left, size);
shiftdown(root, left, nelem, size);
}
}
return;
}
void heapify(void* root, void* base, size_t nelem, size_t size){
int heap;
heap = 1;
int baseIndex = (base - root)/size;
int leftIndex = ((base - root)/size * 2 + 1);
int rightIndex = ((base - root)/size * 2 + 2);
void* left;
void* right;
if(leftIndex < nelem)
left = leftIndex*size + root;
if(rightIndex < nelem)
right = rightIndex*size + root;
if(rightIndex < nelem){
if(compar(right, base) > 0){
swap(base, right, size);
heap = 0;
}
}
if(leftIndex < nelem){
if(compar(left, base) > 0){
swap(base, left, size);
heap = 0;
}
}
if(baseIndex + 1 < nelem)
heapify(root, base + size,nelem, size);
if(heap == 0)
heapify(root, root, nelem, size);
return;
}
void srtheap1(void* base, size_t nelem, size_t size){
while(nelem > 1){
swap(base, base + (nelem-1) * size, size);
nelem--;
shiftdown(base,base,nelem,size);
}
}
void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) {
int a[size];
size_t size1 = 4;
nelem = sizeof(a)/sizeof(a[0]);
base = malloc(sizeof(void));
base = (void*)a;
printarray(base, nelem);
srtheap1(base, nelem, size1);
printarray(base, nelem);
printf("Sorted\n");
system("PAUSE");
return;
}