Categories
- 4th semester (27)
- 5th semester (3)
- ADA (13)
- Assembly Level Language (12)
- BE (45)
- C Language Programming (5)
- C language (20)
- C++ Language (5)
- CCP Lab programing (3)
- Computer Programming Lab (3)
- DAA Lab Programming (13)
- Data Structure and C++ laboratory Program (6)
- Data Structure and C++ labotary Program (5)
- Design and Analysis of algorithm (14)
- First Year (5)
- MASM (12)
- Microprocessor (12)
- Microprocessor lab program (12)
- System Software & OS Laboratory (5)
- Unix program (4)
- bachelor of engineering (30)
- basic (1)
- basic mathematics (2)
- beginners (10)
- c++ program (9)
- calculations (7)
- computer science (30)
- downloadable (5)
- engineering syllabus (4)
- simple program (6)
Trend Posts
Blogger news
Author
Followers
Showing posts with label binary search. Show all posts
Showing posts with label binary search. Show all posts
Friday, February 21, 2014
ASSUME
DS: DATA,
CS: CODE
DATA
SEGMENT ;
start of data segment
NUM
DW 1234H,5678H,6256H,7321H,8454H ; 5 elements
stored in array NUM
COUNT
DW 05H ;
Total
number of elements stored in array (n)
KEY
DW 3456H ;
key element to be searched
MSG1 DB 10, 13, 'KEY
FOUND', '$' ;
message to be displayed on the screen
MSG2 DB 10, 13, 'KEY NOT FOUND', '$'
DATA
ENDS ; end of data
segment
CODE SEGMENT ;
start
of code segment
START: MOV
AX, DATA
MOV
DS, AX ; initializing
data segment
XOR
AX, AX ; clear
AX register contents to store
the mid value
MOV
BX, 1 ; Initialize BX =
1 (Low value)
MOV
DX, COUNT ; Store high value
in DX register (here n=5)
MOV
CX, KEY ; CX contains
value to be searched.
AGAIN: CMP BX, DX ; compare low and
high value
JA
NOTFOUND ; if low value > high value then display key not found
MOV
AX, BX ; AX contains low
value
ADD
AX, DX ; add low and
high value
SHR AX, 1 ; Divide by 2 to
find mid value
MOV
SI, AX ; move mid value
to SI
DEC SI ; decrement SI
ADD SI, SI ; add SI and SI to find the actual mid position of array
CMP
CX, NUM[SI] ;
compare Key
element and middle element of array
JE FOUND ; if equal,
display key has found.
JA
ABOVE ;
if key element is greater than middle element of
;
array,
then
jump to ABOVE
location, to search
;
element from mid to high end side
JMP
AGAIN ;
jump
to AGAIN, to compare next element
JMP AGAIN ; jump to AGAIN to compare next
element.
FOUND: MOV
DX, OFFSET MSG1 ; if found print message MSG1,
whose
offset is in DX
MOV
AH, 09H ; using 09H
service no. with DOS 21H intrpt.
INT
21H
JMP
STOP ;
jump
to stop and terminate
NOTFOUND: MOV DX, OFFSET MSG2 ; if
not found print message MSG2
MOV AH, 09H ; using 09H
service no. with DOS 21H intrpt.
INT 21H
INT 21H
CODE
ENDS ; end of code segment
END
START ;
end of program
Output:
Sample
input : 3456H
Sample
input : 1456H (Change the key
element used in the program and save it)
D:\MASM>
Masm 1a.asm;
D:\MASM>
Link 1a.obj;
D:\MASM>
1a.exe
KEY NOT FOUND
Or
we
can Debug the program using
D:\MASM>
TD 1a.exe
Then
press F7 to trace the program (line
by line execution) or press F9 to
run the program in one operation.
Tuesday, August 27, 2013
Design,develop and execute a program in C to input N integer numbers into a single dimensional array,sort them in ascending order using dimensional array and perform a binary search for a given key integer number and report success or failure in the form of suitable message
CLICK HERE TO DOWNLOAD PROGRAM FILE!
Program No:3
=================
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[100],i,n,key,first,last,mid;
clrscr();
printf("enter the limit number of array elements\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("My array elements...\n");
for(i=0;i<n;i++)
{
printf("%d \n",a[i]);
}
printf("enter your searching word\n");
scanf("%d",&key);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(key==a[mid])
{
printf("\n %d is found at location =%d\n",key,mid+1);
getch();
exit(0);
}
else
{
if(key>a[mid])
{
first=mid+1;
}
else
{
last=mid-1;
}
}
}
printf("The %d is not found in the list ",key);
getch();
}
===================
Program Ends
CLICK HERE TO DOWNLOAD PROGRAM FILE!
To referance wikipedia link
screenshots:
CLICK HERE TO DOWNLOAD PROGRAM FILE!
Program No:3
=================
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[100],i,n,key,first,last,mid;
clrscr();
printf("enter the limit number of array elements\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("My array elements...\n");
for(i=0;i<n;i++)
{
printf("%d \n",a[i]);
}
printf("enter your searching word\n");
scanf("%d",&key);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(key==a[mid])
{
printf("\n %d is found at location =%d\n",key,mid+1);
getch();
exit(0);
}
else
{
if(key>a[mid])
{
first=mid+1;
}
else
{
last=mid-1;
}
}
}
printf("The %d is not found in the list ",key);
getch();
}
===================
Program Ends
CLICK HERE TO DOWNLOAD PROGRAM FILE!
To referance wikipedia link
screenshots:
Subscribe to:
Posts (Atom)

