I'm trying to create a binary search to go through my array. Never done this before, so I'm working mostly off of examples.
Here's the code for my search function:
Code:
int binsearch(int A[], int top, int bot, int look)
{
int retval;
int mid;
mid=(top + bot)/2;
if (top < bot)
retval =-1;
else if (look == A[mid])
retval=mid;
else if (look < A[mid])
retval=binsearch(A,bot,mid-1,look);
else
retval=binsearch(A,mid+1,top,look);
printf("%i", retval);
return retval;
}
I have it displaying retval for my own purposes, just to see why it's not working. I get -1 returned each time.
Here's my entire code:
Definitions
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sort.h"
/*=======================================================================================*/
void sortArray(int A[] , int num)
{
int o;
int j;
for (o = 0; o < num-1; o++)
{
j = indexSmallest(A, o, num);
if (j!= o)
swap(&A[j], &A[o]);
}
return;
}
/*=======================================================================================*/
int indexSmallest(int A[] ,int o ,int n)
{
int j;
int ret = o;
for (j = o + 1; j < n; j++)
if (A[j] < A[ret])
ret = j;
return ret;
}
/*=======================================================================================*/
void swap(int *X,int *Y)
{
int temp;
temp= *X;
*X = *Y;
*Y=temp;
return;
}
/*=======================================================================================*/
void printArray(int A[],int num)
{
int j;
for (j = 0; j < num; j++)
printf("A[%i] = %i\n", j, A[j]);
return;
}
/*=======================================================================================*/
int binsearch(int A[], int top, int bot, int look)
{
int retval;
int mid;
mid=(top + bot)/2;
if (top < bot)
retval =-1;
else if (look == A[mid])
retval=mid;
else if (look < A[mid])
retval=binsearch(A,bot,mid-1,look);
else
retval=binsearch(A,mid+1,top,look);
printf("%i", retval);
return retval;
}
Main function
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sort.h"
#define N 100
#define MAXVALUE 1000
int look, k;
int A[N];
char temp;
int n;
int index;
int main (void)
{ /*BEGIN MAIN*/
srand(time(0)); /*Seed random number generator.*/
for (k = 0; k < N; k++)
{
A[k]=(rand()%1000)+1; /*Fill the array.*/
}
sortArray(A,N); /*Call sortArray to sort the array.*/
printArray(A,N); /*Call printArray to print the array.*/
/*=================================================================================================*/
printf("\nSearch for a number [1-1000] or [-1] to terminate:"); /*Ask for number to search.*/
scanf("%i", &look); /*Assign value to "look" variable.*/
while (look != -1)
{/*BEGIN SEARCH LOOP*/
if ((index=binsearch(A, 0, MAXVALUE-1, look))==-1)
{
printf("\nNo match found for: %i \n",look);
}
else
{
printf("\nValue: %i found at index %i\n", look, index);
}
printf("\nSearch for a number [1-1000] or [-1] to terminate:");
scanf("%i", &look);
}/*END SEARCH LOOP*/
/*=================================================================================================*/
return 0;
} /*END MAIN*/
Like I said, I'm working mostly off of examples here. Hopefully there's an obvious fix.