jsdecastro28 wrote:
> please i need it badly
> 
> 
> #include<stdio.h>
> #include<conio.h>
> void main()
> {
> int a[10],n,i,j,temp;
> int beg,end,mid,target;
> clrscr();
> printf("enter the total numbers:&#8255;);
> scanf("%d&#8255;,&n);
> printf("enter the array elements:&#8255; );
> for(i=0;i<n;i++)
> scanf("%d&#8255;,&a[i]);
> for(i=0;i<n-1;i++)
>     {  
>      for(j=0;j<n-i-1;j++)
>        {
>           If(a[j+1]<a[j])
>             {
>                temp=a[j];
>                 a[j]=a[j+1];
>                  a[j+1]=temp;
>             }
>       }
>   }
> printf("the sorted numbers are:&#8255;);
> for(i=0;i<n;i++)
> printf("%4d&#8255;,a[i]);
> beg=a[0];
> end=a[9];
> mid=(beg+end)/2;
> printf("\nenter the number to be searched:&#8255;);
> scanf("%d&#8255;,&target);
> while(beg<=end && a[mid]!=target)
> {
> if(target<a[mid])
>  end=mid-1;
> else
> beg=mid+1;
> mid=(beg+end)/2;
> }
> If(a[mid]==target)
>   {
> printf("\nthe number is found at position %2d&#8255;,mid);
>   }
> else
>     {
> printf("\nthe number is not found:&#8255;);
>      }
> getch();
> }

Wouldn't it have just been easier (and probably faster) to use qsort() 
and bsearch() OR just scanned the entire array directly looking for just 
one value instead of doing a bubblesort (which has O(n^2) complexity) 
and then something that resembles bsearch() (which has O(log n) complexity)?

(Or just used C++?)

As to dynamic allocation:

int *a;

...
a = malloc(sizeof(int) * n);

...
free(a);

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to