Dsa Unit II Notes
Dsa Unit II Notes
Traversing refers to visiting or processing each element atleast once. Traversal is done by
starting with the first element of the array and reaching to the last.
For e.g. To print all the elements of an array, To count the number of elements of the
array.
Algorithm : TraverseLA(LA, LB, UB)
Description : Here, Consider ‘LA’ is the Linear array with Lower Bound ‘LB’ and Upper
Bound ‘UB’. This algorithm traverses array LA applying an operation ‘PROCESS’ to each
element of ‘LA’ .
OR
Step 1 : Repeat for K= LB to UB
Step 2 : Apply PROCESS to LA[K]
[End of Loop].
Step 3 : Exit
2
Write a program to traverse & print all the elements of a linear array.
#include<stdio.h>
#include<conio.h>
void main()
{
int LA[5];
clrscr();
for(int k=0;k<=4;k++)
{
printf(“%d”,LA[k]);
}
getch();
}
3
Step 1 : [Initialize counter k with index of last element] Set k :=N-1
Step 2 : While k>=LOC repeat steps 3 and 4
Step 3 : [Move the current element one position backwards]
DATA[k+1] :=DATA[k]
Step 4 : [Decrement counter k] Set k :=k-1
Step 5 : [Insert new element at the Location] DATA[LOC] :=ITEM
Step 6 : [Update total number of array elements] Set N:=N+1
4
Write a Program to insert new element in array.
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[50],N,ITEM,LOC=0;
clrscr();
cout<<"Enter size of Array : ";
cin>>N;
cout<<"Enter array elements : ";
for(int k=0;k<N;k++)
{
cin>>DATA[k];
}
cout<<"\nArray elements before insertion : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
for(k=N-1;k>=LOC;k--)
{
DATA[k+1]=DATA[k];
}
cout<<"\nEnter element to be inserted :";
cin>>ITEM;
DATA[LOC]=ITEM;
cout<<"\nArray elements after insertion : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
N++;
getch();
}
5
Deletion in Linear Array involves three cases, as :
1. At the beginning
2. At a given location
3. At the end
Delete the element and shift the rest of the elements to left by one position.
Algorithm : DeleteLA (DATA, N, ITEM, LOC)
Description : This algorithm deletes an element at given position in a linear array DATA
with N elements and stores in ITEM.
When LOC=1/0 it means the element to be deleted is at the beginning
When LOC =N it means the element to be deleted is at the end
When LOC = N-1 it means the element to be deleted is at J-th (given) location.
6
Write a Program to delete existing element from linear array.
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[50],N,ITEM,LOC=2;
clrscr();
cout<<"Enter Size of Array : ";
cin>>N;
cout<<"\nEnter Array elements : ";
for(int k=0;k<N;k++)
{
cin>>DATA[k];
}
cout<<"\nArray elements before deletion : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
ITEM=DATA[LOC];
for(k=LOC;k<N;k++)
{
DATA[k]=DATA[k+1];
}
cout<<"\nArray elements after deletion : ";
for(k=0;k<N;k++)
{
7
cout<<DATA[k]<<"\t";
}
getch();
}
If we need to search the element 20, it will go step by step in a sequential order, thus it is
also called sequential search.
8
Algorithm : LinearSearch (DATA, LB, UB, ITEM)
Description : This algorithm traverses & searches for the given element ITEM in the
linear array DATA with Lower Bound LB & Upper Bound UB.
Step 1 : [Initialize Counter k to the first index] Set k:=LB
Step 2 : Repeat Step 3 While k<=UB
Step 3 : [Check if the ITEM is equal with the array element]
IF DATA[k] = ITEM THEN
Set LOC := k
ELSE
Set k := k+1
Step 4 : If LOC != k then Set LOC :=NULL
Step 5 : Exit
9
cin>>ITEM;
k=0;
while(k<N)
{
if(DATA[k]==ITEM)
{
LOC=k;
break;
}
else
{
k++;
}
}
if(LOC!=0)
{
cout<<"Item to be searched is at location : "<<LOC;
}
else
{
cout<<"Search Unsuccessful";
}
getch();
}
10
2. Binary Search In Linear Array :
When the given data structure has large no. of data elements, the efficiency of linear
search decreases. Thus, another searching technique can be used as Binary Search.
Binary search follows divide and conquer approach in which, the list is divided into two
halves. The element to be searched may be either in the first half or the second half. The
algorithm goes on dividing each part into its two sub-parts for searching.
Due to this, the no. of comparisons to be performed are automatically reduced. For binary
search the data structure should be in the sorted form.
In Binary Search, the Smallest index is called BEGINNING and the Largest index is called
END, while the MIDDLE element index is called MID.
To divide the array into two halves, i.e. to get the middle index value of array, a formula is
used as :
MID = INT((BEG + END)/2)
IF the ITEM to be searched is equal to the value of MID, LOC is set to MID, as :
If ITEM=DATA[MID] then
LOC=MID
IF the ITEM to be searched is greater than the value of MID, Set BEG = MID + 1, as :
If ITEM>DATA[MID] then
BEG = MID + 1
IF the ITEM to be searched is less than the value of MID, Set END=MID – 1, as :
If ITEM<DATA[MID] then
END=MID – 1
For e.g. Let DATA be the Linear Array with 10 elements.
DATA[10] = { 11,22,30,33,40,44,50,60,66,77 }
Find LOC of 44 using Binary Search?
DATA[10] = { 11,22,30,33,40,44,50,60,66,77 }
Solution : Here ITEM = 44,
Set BEG=1, End=10
11
MID = INT((BEG + END)/2)
= INT((1+10)/2)
= INT(11/2)
= INT(5.5)
MID = 5, thus DATA[5] = 40
Since ITEM > DATA[MID]
i.e. 44 > 40
Thus, BEG = MID + 1
= 5+1
BEG = 6
Now,
MID = INT((BEG + END)/2)
= INT(( 6+10 )/2)
= INT (16 / 2)
= INT(8)
MID = 8, thus DATA[8] = 60
Since ITEM < DATA[MID]
i.e. 44 < 60
Thus, END = MID - 1
= 8-1
END = 7
Now,
MID = INT((BEG + END)/2)
= INT(( 6+7 )/2)
= INT (13 / 2)
= INT(6.5)
MID = 6, thus DATA[6] = 44
Since ITEM = DATA[MID]
i.e. 44 = 44
Thus, LOC = MID
LOC = 6
The search is successful.
Here, DATA is sorted array with lower bound LB & upper bound UB. The variables BEG,
END & MID denotes the Beginning, End & Middle locations of the array.
12
This algorithm finds the location of LOC or sets LOC=NULL if the search is unsuccessful.
MID:=INT((BEG+END)/2)
14
Sorting in Linear Array :
Sorting Operation is used to arrange the data items in some logical order. Sorting is
ordering a list of objects. A Sorting Algorithm is used to rearrange a given array or list
elements in ascending or descending order or either in alphanumeric order.
There are various methods for sorting linear arrays, some of them are :
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
Let us describe each of these methods in detail, as :
Bubble Sort : Bubble Sort is the simplest sorting algorithm for small list.
It compares the adjacent elements and swaps their positions if they are not in the
desired order.
Starting from the first index, it compares the first and the second element. If the
first element is greater than the second element, they are swapped. If we have
total N elements, we need to repeat this process for N-1 times.
It is known as bubble sort, because with every complete iteration the largest
element in the given array, bubbles up towards the last place or the highest index,
just like a water bubble rises up to the water surface.
For e.g. Let DATA be array in memory as follows :
DATA[4] = {9,5,7,11}
Sort the given array using Bubble sort.
15
Algorithm : BubbleSort(DATA, PTR, K, N)
Description : Here, DATA is the linear array with N elements. PTR is the pointer variable
that executes the passes and K is the counter variable that iterates the loop from lower
bound to upper bound of the array.
This algorithm sorts the given array in increasing numerical order.
Selection Sort : Selection sort is an algorithm that selects the smallest element from
an unsorted list in each iteration and places that element at the beginning.
First, find the smallest element of the array and place it on the first position. Then,
find the second smallest element of the array and place it on the second position.
The process continues until we get the sorted array.
The array with n elements is sorted by using n-1 pass of selection sort algorithm.
For e.g. Let DATA be array in memory as follows :
DATA[6] = {40,20,60,10,50,30}
Sort the given array using Selection sort.
17
Algorithm : SelectionSort(DATA, N, k, MIN, TEMP, PTR,LOC)
Description : Here, DATA is the linear array with N elements. PTR is the pointer variable to
execute the passes and TEMP is the temporary variable that stores the value of elements.
LOC stores the location of value to be swapped.
This algorithm sorts the array DATA by repeatedly finding the minimum element
(considering ascending order) from the list and putting it at the beginning.
Step 1 : Repeat Steps 2,3 and 4 for k=0 to N-1
Step 2 : Set MIN := DATA[k]
Step 3 : Repeat for ptr = k+1,k+2, _ _ _ _, k+N
If DATA[ptr] < MIN then
MIN = DATA[ptr] & LOC=ptr
Write a Program that implement Selection Sort, to sort a given list of integers
in ascending order.
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[50],N,TEMP,ptr,LOC,MIN;
clrscr();
18
cout<<"Enter size of array : ";
cin>>N;
cout<<"\nEnter Array Elements : ";
for(int k=0;k<N;k++)
{
cin>>DATA[k];
}
cout<<"\n\nArray before sorting : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
for(k=0;k<N;k++)
{
MIN=DATA[k];
for(ptr=k+1;ptr<N;ptr++)
{
if(DATA[ptr]<MIN)
{
MIN=DATA[ptr];
LOC=ptr;
}
}
if(MIN!=DATA[k])
{
TEMP=DATA[k];
DATA[k]=DATA[LOC];
DATA[LOC]=TEMP;
}
}
cout<<"\n\nArray after Selection Sort :";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
getch();
}
19
Insertion Sort : Insertion sort works similarly as we sort cards in our hand in a card
game. We assume that the first card is already sorted then we select an unsorted
card. If the unsorted card is greater than the card in hand, it is placed on the right
otherwise, to the left.
In the same way, other unsorted cards are taken and put at their right place. A
similar approach is used by insertion sort.
Insertion sort is a sorting algorithm that places an unsorted element at its suitable
place in each iteration. It is efficient for smaller data sets.
For e.g. Let DATA be array in memory as follows :
DATA[5] = {4,8,6,2,3}
Sort the given array using Insertion sort.
20
Step 1 : Repeat steps 2 to 4 for k=1,2,_ _ _, N-1
Step 2 : Set TEMP :=DATA[k] & PTR :=k-1
Step 3 : Repeat while PTR>=0 && TEMP < DATA[PTR]
a) Set DATA[PTR+1] := DATA[PTR]
b) Set PTR := PTR – 1
Step 4 : [Insert element at proper position]
Set DATA[PTR+1] :=TEMP
Step 5 : Exit
Write a Program that implement Insertion Sort, to sort a given list of integers
in ascending order.
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[50],N,TEMP,ptr;
clrscr();
cout<<"Enter size of array : ";
cin>>N;
cout<<"\nEnter Array Elements : ";
for(int k=0;k<N;k++)
{
cin>>DATA[k];
}
cout<<"\n\nArray before sorting : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
for(k=0;k<N;k++)
{
TEMP=DATA[k];
ptr=k-1;
while(ptr>=0 && TEMP<DATA[ptr])
{
DATA[ptr+1]=DATA[ptr];
21
ptr--;
}
DATA[ptr+1]=TEMP;
}
cout<<"\n\nArray after Insertion Sort : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
getch();
}
22
PROGRAMS FOR PRACTICE :
Q. Write a program to get input of size of array & array elements & sort them.
Also ask user which sorting technique to be used to sort the array.
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[50],N,TEMP,ptr,ch,MIN,LOC;
clrscr();
cout<<"Enter the size of array : ";
cin>>N;
cout<<"\nEnter Array Elements : ";
for(int k=0;k<N;k++)
{
cin>>DATA[k];
}
cout<<"\nArray before sorting : ";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
cout<<"\n\nEnter sorting technique to be used : 1.Bubble Sort\t2.Selection
Sort\t3.Insertion Sort : ";
cin>>ch;
switch(ch)
{
case 1:
for(k=0;k<N;k++)
{
for(ptr=0;ptr<N-k;ptr++)
{
if(DATA[ptr]>DATA[ptr+1])
{
TEMP=DATA[ptr];
DATA[ptr]=DATA[ptr+1];
DATA[ptr+1]=TEMP;
}
}
}
cout<<"\n\nArray after Bubble Sort : ";
for(k=0;k<N;k++)
23
{
cout<<DATA[k]<<"\t";
}
break;
case 2:
for(k=0;k<N;k++)
{
MIN=DATA[k];
for(ptr=k+1;ptr<N;ptr++)
{
if(DATA[ptr]<MIN)
{
MIN=DATA[ptr];
LOC=ptr;
}
}
if(MIN!=DATA[k])
{
TEMP=DATA[k];
DATA[k]=DATA[LOC];
DATA[LOC]=TEMP;
}
}
cout<<"\n\nArray after Selection Sort :";
for(k=0;k<N;k++)
{
cout<<DATA[k]<<"\t";
}
break;
case 3:
for(k=0;k<N;k++)
{
TEMP=DATA[k];
ptr=k-1;
while(ptr>=0 && TEMP<DATA[ptr])
{
DATA[ptr+1]=DATA[ptr];
ptr--;
}
DATA[ptr+1]=TEMP;
}
cout<<"\n\nArray after Insertion Sort : ";
for(k=0;k<N;k++)
24
{
cout<<DATA[k]<<"\t";
}
break;
default :
cout<<”Invalid Choice”;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[10],N,k;
clrscr();
cout<<”Enter the size of array : “;
cin>>N;
cout<<”\nEnter Array Elements : “;
for(k=0;k<N;k++)
{
cin>>DATA[k];
}
for(k=N-1;k>=0;k--)
{
cout<<DATA[k];
}
getch();
}
25
Q. Write a program to find the sum of ten numbers in a given array with its
algorithm.
Step 1 : Take input of 10 array elements.
Step 2 : Traverse the array
Step 3 : Add all the array elements in a variable sum.
#include<iostream.h>
#include<conio.h>
void main()
{
int DATA[10],k,sum=0;
clrscr();
cout<<”\nEnter Array Elements : “;
for(k=0;k<9;k++)
{
cin>>DATA[k];
}
for(k=0;k<9;k++)
{
sum=sum+DATA[k];
}
cout<<”Sum of all array elements = “<<sum;
getch();
}
26