0% found this document useful (0 votes)
105 views23 pages

Aditya DSPM

This document contains summaries of different algorithms for operations on linear arrays: 1) Traversing a linear array refers to accessing each element of an array at least once in a sequential order. The algorithm initializes a counter and repeats accessing and processing each element while incrementing the counter. 2) Insertion in a linear array involves inserting an element at a given position. The algorithm moves elements after the position down, inserts the new element, and increments the size. 3) Deletion removes an element from a given position by moving elements after it up and decrementing the size.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views23 pages

Aditya DSPM

This document contains summaries of different algorithms for operations on linear arrays: 1) Traversing a linear array refers to accessing each element of an array at least once in a sequential order. The algorithm initializes a counter and repeats accessing and processing each element while incrementing the counter. 2) Insertion in a linear array involves inserting an element at a given position. The algorithm moves elements after the position down, inserts the new element, and increments the size. 3) Deletion removes an element from a given position by moving elements after it up and decrementing the size.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

1

B.H.S.B.I.E.T
DSPM FILE

SUBMITTED TO: SUBMITTED BY:


Mrs. Kiran arora Manish Singh
Bisht
CSE (3rd sem)
90070302384
(9124)

MANISH
2

ARRAY

MANISH
3

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<= UB.

3. [Visit element.] Apply PROCESS to LA [K].

4. [Increase counter.] Set K=K+1.

[End of Step 2 loop.]

5. Exit.

MANISH
4

PROGRAM
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],n,i;
cout<<"ENETR NO. OF ARRAY ELEMENT "<<endl;
cin>>n;
cout<<"ENETR ARRAY ELEMENT "<<endl;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"ARRAY ELEMENT AFTER TRAVERSING ";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}

MANISH
5

MANISH
6

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

MANISH
7

PROGRAM

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],n,g,i,item;
cout<<”ENTER NO. OF ARRAY ELEMENT ";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"ENTER THE POSITION WHERE ELEMENT TO BE INSERTED ";
cin>>g;
cout<<"ENTER THE ELEMENT TO BE INSERTED ";
cin>>item;
for(i=n;i>=g;i--)
{
a[i+1]=a[i];
}
a[g]=item;
for(i=1;i<=n+1;i++)
{

MANISH
8

cout<<a[i]<<endl;
}
getch();
}

MANISH
9

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<=N.

This algorithm deletes the Kth element from LA.

1. Set ITEM =LA[K]

2. Repeat from j=K to N-1:

[Move j+1st element upward] set LA [J]:=LA [J+1].

[End of loop]

3. [Reset the number N of element in LA] set N=N-1

4. Exit

MANISH
10

PROGRAM
#include<iostream.h>

#include<conio.h>

main()

int a[5],i,j,n,g;

cout<<"ENTER ARRAY ELEMENT "<<endl;

for(i=1;i<=5;i++)

cin>>a[i];

cout<<"ENTER POSITION WHERE ELEMENT TO BE DELETED ";

cin>>g;

for(i=g;i<=5;i++)

a[i]=a[i+1];

n=n-1;

g=g-1;

cout<<"ARRAY ELEMENT AFTER DELETION "<<endl;

MANISH
11

for(i=1;i<=4;i++)

cout<<a[i]<<" ";

getch();

MANISH
12

LINEAR SEARCH

Linear search is method to find the element in a linear array which is sorted or
unsorted. 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

MANISH
13

PROGRAM

#include<iostream.h>

#include<conio.h>

void main()

int a[10],n,g,i,item,flag=0;

cout<<"ENTER NO. OF ARRAY ELEMENT ";

cin>>n;

for(i=1;i<=n;i++)

cin>>a[i];

cout<<"ENTER ELEMENT TO BE SEARCHED";

cin>>item;

for(i=1;i<=n;i++)

if(item==a[i])

cout<<"ELEMENT SEARCHED IS AT POSITION "<<i;

MANISH
14

flag=1;

break;

if(flag==0)

cout<<"ELEMENT NOT FOUND IN ARRAY";

getch();

MANISH
15

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<=END and DATA [MID]=!ITEM.

3. IF ITEM< DATA [MID], then:

Set END =MID-1.

Else:

Set BEG=MID+1.

[End of if structure]
MANISH
16

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

MANISH
17

PROGRAM
#include<iostream.h>

#include<conio.h>

void main()

int a[10],n,g,i,item,flag=0,beg=1,mid,end;

cout<<"ENTER NO. OF ARRAY ELEMENT ";

cin>>n;

for(i=1;i<=n;i++)

cin>>a[i];

cout<<"ENTERE ELEMENT TO BE SEARCHED";

cin>>item;

end=n;

do

mid=(beg+end)/2;

if(item==a[mid])

cout<<"ELEMENT FOUND AT POSITION "<<mid;

flag=1;

MANISH
18

break;

else if(item<a[mid])

end=mid-1;

else

beg=mid+1;

while(beg<=end);

if(flag==0)

cout<<"ELEMENT NOT FOUND IN ARRAY ";

getch();

MANISH
19

MANISH
20

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 PTR<=N-K:[Executive pass.]

(a) if DATA[PTR]>DATA[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

MANISH
21

#include<iostream.h>

#include<conio.h>

main()

int a[10],i,j,temp,n;

cout<<"ENTER NO. OF ARRAY ELEMENT TO BE SORTED"<<endl;

cin>>n;

cout<<”ENTER ARRAY ELEMENTS TO BE SORTED "<<endl;

for(i=1;i<=n;i++)

cin>>a[i];

for(i=1;i<n;i++)

for(j=1;j<=n;j++)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

} }

MANISH
22

cout<<"ELEMENTS ARRANGED IN ACENDING ORDER";

for(i=1;i<=n;i++)

cout<<a[i];

cout<<endl;

getch();

MANISH
23

MANISH

You might also like