Ciao, devo scrivere una funzione mergesort in C. L'ho fatto ma non mi funziona. Il codice è:
codice:
#include <stdio.h>
#include <stdlib.h>
#include "sorting.h"
void msort(int a[], int n) {
int aux[] = copy(a, aux, n);
msort2(a, 0, n - 1, aux);
}
void msort2(int a[], int fst, int lst, int b[]) {
if(fst < lst) {
int m = (fst + lst) / 2;
msort2(b, fst, m, a);
msort2(b, m + 1, lst, a);
merge(b, fst, m, lst, a);
}
}
int[] copy(int a[], int aux[], int n) {
int i;
for(i = 0; i < n; i++)
aux[i] = a[i];
return aux;
}
void merge(int a[], int fst, int mid, int lst, int c[]) {
int i = fst;
int j = mid + 1;
int k = fst;
while(i <= mid && j <= lst) {
if(a[i] <= a[j])
c[k++] = a[i++];
else
c[k++] = a[j++];
}
while(i <= mid)
c[k++] = a[i++];
while(j <= lst)
c[k++] = a[j++];
}
File header:
codice:
void msort(int a[], int n);
void msort2(int a[], int fst, int lst, int b[]);
int[] copy(int a[], int aux[], int n);
void merge(int a[], int fst, int mid, int lst, int c[]);
Messaggio d'errore:
codice:
In file included from sorting.c:6:0:
sorting.h:8:4: error: expected identifier or ‘(’ before ‘[’ token
sorting.c: In function ‘msort’:
sorting.c:81:2: error: invalid initializer
sorting.c: At top level:
sorting.c:94:4: error: expected identifier or ‘(’ before ‘[’ token
Dove sbaglio? La funzione copy è giusta?
Grazie