View
226
Download
0
Category
Preview:
Citation preview
8/8/2019 ADITYA DSPM
1/23
1
MANISH
B.H.S.B.I.E.TDSPM FILE
SUBMITTED TO: SUBMITTED BY:
Mrs. Kiran arora Manish Singh
Bisht
CSE (3rd
sem)
90070302384
(9124)
8/8/2019 ADITYA DSPM
2/23
2
MANISH
ARRAY
8/8/2019 ADITYA DSPM
3/23
3
MANISH
1.TRAVERSING A LINEAR ARRAY
Traversing a linear array refers to accessing each element of an array at least one.
Each element of array visited or reached exactly once.
ALGORITHM:
Here LA is a linear array with lower bound LB and upper bound UB. This
algorithm traverses LA applying in operation PROCESS to each element of LA.
1. [Initialize counter.] Set K =LB.
2. Repeat steps 3 and 4 while K
8/8/2019 ADITYA DSPM
4/23
4
MANISH
PROGRAM
#include
#include
void main()
{
int a[10],n,i;
cout
8/8/2019 ADITYA DSPM
5/23
5
MANISH
8/8/2019 ADITYA DSPM
6/23
6
MANISH
INSERTION IN LINEAR ARRAY
Insertion means inserting or adding any element in collection.By insertion
operation we can insert any value to array.
ALGORITHM
INSERT (LA,N,K,ITEM)
Here LA is a linear array with N elements and K is a positive integer such that
K=N.This algorithm inserts an element ITEM into the Kth Position in LA
.
1 [Initialize counter.] Set J:=N.
2 Repeat Steps 3 and 4 while J >=k.
3 [Move jth element downward:] Set LA[J+1]:=LA[J].
4 [decrease Counter .] Set J:J-1.
[End of steps 2 loop.]
5 [Insert element.] Set LA[k]:=ITEM.
6 [Reset N.] Set N := N+1.
7 Exit
8/8/2019 ADITYA DSPM
7/23
7
MANISH
PROGRAM
#include#include
void main()
{
int a[10],n,g,i,item;
cout
8/8/2019 ADITYA DSPM
8/23
8
MANISH
cout
8/8/2019 ADITYA DSPM
9/23
9
MANISH
DELETION IN LINEAR ARRAY
Deletion is the operation used in array to delete any particular element at given
position.
ALGORITHM
DELETE (LA ,N ,K ,ITEM)
Here LA is a linear array with N element and K is a positive integer such that
K
8/8/2019 ADITYA DSPM
10/23
10
MANISH
PROGRAM
#include
#include
main()
{
int a[5],i,j,n,g;
cout
8/8/2019 ADITYA DSPM
11/23
11
MANISH
for(i=1;i
8/8/2019 ADITYA DSPM
12/23
12
MANISH
LINEAR SEARCH
Linear search is method to find the element in a linear array which is sorted orunsorted. This search is useful in limited strength order.
ALGORITHM
LINEAR (DATA,N,ITEM,LOC)
Here DATA is a linear array with N element , and ITEM is a given item of
information .This algorithm finds the the location LOC of ITEM in DATA ,or set
LOC=0,if search is unsuccessful.
1. set DATA [N+1]=ITEM [insert ITEM at the end of DATA]
2. SET LOC=1; [initialize counter]
3. REPEAT while DATA LOC=!ITEM; [search for ITEM]
Set LOC=LOC+1;
4. If LOC=n+1,then set LOC=0;
5. Exit
8/8/2019 ADITYA DSPM
13/23
13
MANISH
PROGRAM
#include
#include
void main()
{
int a[10],n,g,i,item,flag=0;
coutn;
for(i=1;i>a[i];
}
coutitem;
for(i=1;i
8/8/2019 ADITYA DSPM
14/23
14
MANISH
flag=1;
break;
}
}
if(flag==0)
cout
8/8/2019 ADITYA DSPM
15/23
15
MANISH
BINARY SEARCH
Binary search is used to search the element in a sorted list.As name tells it divides
the series in two half and reduce time complexity.
ALGORITHM
BINARY (DATA, LB, UB, ITEM, LOC)
Here data is a sorted array with lower bound lb and upper bound UB and item is a
given item of information. The variables BEG , END and MID denote
,respectively, the beginning ,END middle location of a segment of elements of
data. This algorithm finds the location LOC of ITEM in DATA or sets
LOC=NULL.
1. [INTIALIZE segment variables]
Set BEG=LB, END=UB and MID=INT ((BEG+END)/2).
2. Repeat steps 3 and 4 while BEG
8/8/2019 ADITYA DSPM
16/23
16
MANISH
4. set MID=INT ((BEG+END)/2).
[End of step 2 loop]
5. IF DATA [MID] =ITEM, then:
Set LOC=MID
Else:
Set LOC=NULL.
[End of if structure]
6. EXIT
8/8/2019 ADITYA DSPM
17/23
17
MANISH
PROGRAM
#include
#include
void main()
{
int a[10],n,g,i,item,flag=0,beg=1,mid,end;
coutn;
for(i=1;i>a[i];
}
coutitem;
end=n;
do
{
mid=(beg+end)/2;
if(item==a[mid])
{
cout
8/8/2019 ADITYA DSPM
18/23
18
MANISH
break;
}
else if(item
8/8/2019 ADITYA DSPM
19/23
19
MANISH
8/8/2019 ADITYA DSPM
20/23
20
MANISH
BUBBLE SORT
This method is used to sort the series in ascending or decending order.In this
elements are compare with each other and swap them if order does not fit to it.
ALGORITHM
BUBBLE(DATA,N)
Here DATA is an array with N elements.This algorithm sorts elements inDATA.
1.Repeat steps 2 and 3 for K=1 to N-1.
2.set PTR:=1 [initializes pass pointer PTR]
3.Repeat while PTRDATA[PTR+1],then:
Interchange DATA[PTR]andDATA[PTR+1].
[End of if structure.]
(b) Set PTR:=PTR+1.
[End of inner loop.]
[End of Step 1 outer loop.]
4.Exit.
PROGRAM
8/8/2019 ADITYA DSPM
21/23
21
MANISH
#include
#include
main()
{
int a[10],i,j,temp,n;
cout
8/8/2019 ADITYA DSPM
22/23
22
MANISH
cout
8/8/2019 ADITYA DSPM
23/23
23
MANISH
Recommended