duplicate detection algorithm
Hi.
I've been having troubles with a permutation program. I'm using a module named 'duplicate()' wich tells me when an array of int named 'A' has a duplicate. That is>
A[1] = 5, A[2] = 3, A[3] = 1 A[4] = 5.
there is a duplicate here, A[1] = 5 and A[4] = 5
the function works when the dimension of A is small, say 5 or 6. When the dimension is about 15, the function fails and the program blacks out-
The module is recursive and it works as follows
it takes the first element of A (A[1]) and compares it with the following elements to the right (A[2], A[3] etc. if there is a duplicate, the function returns 1. Otherwise it calls itself, but now taking as first element the element at the right, A[2]. and so on.
The first call from the main program would be duplicate(1)
I'm posting the following code of 'duplicate()'. OBJECTS is the dimension of A[]
int duplicate (int i) {
int j=i+1;
if (j<=OBJECTS) {
while ( (A[i]!=A[j]) && (j<=OBJECTS) ) j++;
if (j>OBJECTS) { // no duplicate
if (i<OBJECTS)
return duplicate(i+1);
else
return 0; // no duplicate
}
else // duplicate
return 1;
} // if
else
return 0; // no duplicate
}
Can anyone tell me why it works when OBJECTS is small and not when it is 14,15....?
Thanks in advance.