Sie sind auf Seite 1von 7

PROGRAM NO 6

ALGORITHM TO DELETE AN ELEMENT FROM ARRAY AT KTH LOCATION

DELETE(LA, N, K, ITEM)

Here LA is linear array with N elements and K is a positive integer such that K<=N. This algorithm deletes the Kth
element from LA.

Step1:-Set ITEM: =LA [K].


Step2:- Repeat for J=K to N-1.
[Move J+1st element upward] Set LA [J] =LA [J+1].
[End of loop].
Step3:- [Number of elements in array] Set N=N-1.
Step4:- Exit.

/*PROGRAM TO DELETE KTH ELEMENT FROM ARRAY*/

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],k=0,item,i,j,n;
cout<<"Enter value of n";
cin>>n;
cout<<"enter the element of the array";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"The elements of array are"<<endl;
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
cout<<"Enter the location to delete"<<endl;
cin>>k;
item=a[k];
for(j=k;j<=n;j++)
{
a[j]=a[j+1];
}
cout<<"After deleting the element are"<<endl;
for(i=1;i<=n-1;i++)
{
cout<<"\n"<<a[i];
}
getch();
}
OUTPUT:
PROGRAM NO 7

ALGORITHM TO INSERT AN ELEMENT AT KTH LOCATION IN AN ARRAY

INSERT( LA , N, K, ITEM)

Here LA is linear array with N elements and K is a positive integer such that K<=N . This algorithm inserts an
element ITEM inti Kth position in LA.

Step1:- [Initialize counter] Set J=N.


Step2:- Repeat steps 3 and 4 while J<=K.
Step3:- [Move Jth element downward] Set LA[J+1]=LA[J].
Step4:- [Decrement counter] Set J=J-1.
[End of step 2].
Step5:- [Insert element ] Set [ LA[K]= ITEM.
Step6:- [Reset N] Set N=N+1.
Step7:- Exit.

PROGRAM TO INSERT AN ELEMENT IN ARRAY AT KTH LOCATION

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,item,j,n,k;
clrscr();
cout<<"Enter value of N"<<endl;
cin>>n;
cout<<"Enter value of array"<<endl;
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"The array is"<<endl;
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
j=n;
while(j>=k)
{
a[j+1]=a[j];
j--;
}
cout<<"Enter position whr u want to insert"<<endl;
cin>>k;
cout<<"Enter elemt to insert"<<endl;
cin>>item;
a[k]=item;
n=n+1;

for(i=1;i<=j;i++)
{
cout<<"The updated array is"<<i<<" = "<<a[i]<<endl;
}
getch();
}
OUTPUT:
ALGORITHM OF LINEAR SEARCH

LINEARSEARCH(DATA, N, ITEM ,LOC)

Here DATA is linear array With N elements and ITEM is a given item of information. This algorithms
finds the location LOC of ITEM in DATA,or sets the LOC=0 if search is unsuccessful.

Step1:- [Insert ITEM at the end of DATA] Set DATA[N+1]=ITEM.


Step2:- [Initialize counter]Set LOC=0.
Step3:- [Search for ITEM]
Repeat while DATA[LOC]!=ITEM].
Set LOC=LOC+1.
[End of Loop].
Step4:- [Unsuccesful] if (LOC=N+1)Then
Set LOC=0.
Step5:- Exit.

PROGRAM OF LINEAR SEARCH


ALGORITHM OF BINARY SEARCH

BINARYSEARCH(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 and middle
locations of a segment of elements of DATA. This algorithms finds the location LOC of ITEM in DATA or
sets LOC=NULL.

Step1:- [Initialize variables]


Set BEG = LB, END = UB and MID = INT (BEG+END)/2.
Step2:- Repeat steps 3 and 4 while BEG<=END and DATA [MID]! =ITEM.
Step3:- IF [ITEM] <DATA [MID], then
Set END=MID-1.
Else
Set BEG =MID+1.
[End of if structure]
Step4:- Set MID=INT (BEG+END)/2.
[End of Step 2 loop]
Step5:- If DATA [MID] =ITEM then
Set LOC=MID
Else
Set LOC=NULL.
[End of if structure]
Step6:- Exit .

PROGRAM OF BINARY SEARCH


ALGORITHM OF BUBBLE SORT

BUBBLE (DATA, N)

Here DATA is an array with N elements . This algorithm sorts the elements in DATA.

Step1:- Repeat steps 2 and 3 for K=1 to N-1.


Step2:- Set PTR = 1 [Initialize pass pointer PTR]
Step3:- Repeat while PTR<=N-K[Execute pass]

a) If DATA[PTR]<DATA[PTR+1]
Interchange DATA [PTR] and DATA[PTR+1]
[End of if structure]
b) Set PTR=PTR+1.
[End of inner loop]
[End of step 1 outer loop]
Step4:- Exit.

PROGRAM OF BUBBLE SORT

Das könnte Ihnen auch gefallen