0% found this document useful (0 votes)
20 views6 pages

Lab01 Array

The document discusses array data structures and operations on arrays including traversal, insertion, deletion, searching, and sorting. It provides pseudocode for linear search and binary search algorithms. The objectives are to apply array operations and develop C++ programs to demonstrate linear and binary searching on an array.

Uploaded by

Swaira Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
20 views6 pages

Lab01 Array

The document discusses array data structures and operations on arrays including traversal, insertion, deletion, searching, and sorting. It provides pseudocode for linear search and binary search algorithms. The objectives are to apply array operations and develop C++ programs to demonstrate linear and binary searching on an array.

Uploaded by

Swaira Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Lab No.

01

Objectives: To perform/apply Array Data Structure Operations (Traversal, Insertion,


Deletion and Searching)

Introduction
Array is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their algorithms.
Following are the important terms to understand the concept of Array.
 Element − Each item stored in an array is called an element.
 Index − Each location of an element in an array has a numerical index, which is used
to identify the element.

Array Representation

datatype arrayName[array Size];

For example,
int x[6];
Here,
int - type of element to be stored
x - name of the array
6 - size of the arrayve illustration, following are the important points to be considered.

Basic Operations
Following are the basic operations supported by an array.
 Traversal: Process each element exactly once in array list.
 Search: Search location of element of an array list.
 Insertion: Add new element in an array list.
 Deletion: Remove element in an array list.
 Sorting: Arrange elements in an array list.
Tasks:
1. Run the programs 1, 2 and 3 and print the outputs in the space provided.

Program 1 (Traversal):
#include <iostream>
using namespace std;
int main()
{
int array[] = {4,3,2};
int n = 0; // traverse through array
while (n<3)
{
cout << array[n];
n++;
}}
Output:

Program 2 (Insertion):
#include<iostream>
using namespace std;
int main()
{ int i,a[5],no,pos;
cout<<"Enter data in Array: ";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n\nStored Data in Array: ";
for(i=0;i<5;i++)
{
cout<<a[i];
}
cout<<"\n\nEnter position to insert number: ";
cin>>pos;
if(pos>5)
{ cout<<"\n\nThis is out of range"; }
else
{ cout<<"\n\nEnter new number: ";
cin>>no;
--pos;
for(i=5;i>=pos;i--)
{ a[i+1]=a[i]; }
a[pos]=no;
cout<<"\n\nNew data in Array: ";
for(i=0;i<6;i++)
{ cout<<a[i]; } }
}
Output:

Program 3 (Deletion):
#include<iostream>
using namespace std;
int main()
{ int i,a[5],no,pos;
cout<<"Enter data in array: ";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n\nStored Data in array: ";
for(i=0;i<5;i++)
{
cout<<a[i];
}
cout<<"\n\nEnter poss. of element to delete: ";
cin>>pos;
if(pos>5)
{
cout<<"\n\nThis value is out of range: ";
}
else
{
--pos;
for(i=pos;i<=4;i++)
{
a[i]=a[i+1];
}
cout<<"\n\nNew data in array: ";
for(i=0;i<4;i++)
{
cout<<a[i];
}}
}
Output:
Searching
1. Linear search
Linear search is a very simple search algorithm. In this type of search, a sequential search is made
over all items one by one. Every item is checked and if a match is found then that particular item is
returned, otherwise the search continues till the end of the data collection.

Algorithm
Linear Search ( Array A, Value x)

Step 1: Set i to 1

Step 2: if i > n then go to step 7

Step 3: if A[i] = x then go to step 6

Step 4: Set i to i + 1

Step 5: Go to Step 2

Step 6: Print Element x Found at index i and go to step 8

Step 7: Print element not found

Step 8: Exit

Pseudocode
procedure linear_search (list, value)

for each item in the list

if match item == value

return the item's location

end if

end for

end procedure

Binary Search:

Binary Search is applied on the sorted array or list of large size. It's time complexity of O(log n)
makes it very fast as compared to other sorting algorithms. The only limitation is that the array or list
of elements must be sorted for the binary search algorithm to work on it.

Pseudocode Of Binary Search


Procedure binary_search

A ← sorted array

n ← size of array

x ← value to be searched

Set lowerBound = 1

Set upperBound = n

while x not found

if upperBound < lowerBound

EXIT: x does not exists.

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2

if A[midPoint] < x

set lowerBound = midPoint + 1

if A[midPoint] > x

set upperBound = midPoint - 1

if A[midPoint] = x

EXIT: x found at location midPoint

end while

end procedure .

EXERCISE
Develop a C++ Program using the provided Linear Search and Binary Search algorithms for
Searching an array and print the output.

Code:

Output:

You might also like