Hi...i am trying to write a program that allows me to locate a number in an array with fifteen numbers in it. I have been working on this code for a couple of days now and still can't get it to work. i have sorted the array already but now i am trying to do what is called a binary search to locate a number within the array. For example...If i input the number 28 then the output should say 28 found at position 8 or wherever. 28 is located within the array. if the number is not found then it is supposed to say 28 not found. Below is the code that i have for the shell sort and search. I have the Shell sort working so the part that i am trying to get working is the part after Binary Search. Any help would be much appreciated. I am a beginer so be simple please... Thanks so much!
void main()
{
int x[] = {28, 35, 16, 42, 92, 57, 81, 6, 10, 72, 38, 52, 32, 16, 7};
int temp, i, j, notfound,jump, first,last,mid, end;
char more;
clrscr();
printf("\n\n\tUnsorted Seq: ");
for (i = 0; i < 15; i++)
printf ("%d,", x[i]);
// Shell Sort
for(jump=15/2;jump>0;jump=jump/2)
{
for (i = jump; i < 15; ++i)
{
for(j=i-jump; j>=0 && x[j] > x[j + jump]; j=j-jump)
{
temp = x[j];
x[j] = x[j + jump];
x[j + jump] = temp;
}
}
}
printf("\n\t Sorted Seq: ");
for (i = 0; i < 15; i++)
printf ("%d,", x[i]);
// Binary Search (the part that does'nt work)
do
{
printf ("\n\n\t\tPlease input a number: ");
scanf ("%d", &temp);
first = 0;
last = 14;
mid = (last +first)/2;
while(first<=last)
{
if(temp >x[mid])
first = mid +1;
else if(temp< x[mid])
last = mid -1;
if(temp==x[mid])
{
printf ("\n\t\t%d is found at postion %d", temp,i);
break;
}
}
if (first>last)
printf ("\n\t\t%d is not found", temp);
printf ("\n\t\tDo more (Y/N)? ");
scanf ("%s", &more);
}
while (more == 'Y' || more == 'y');
}