Docsity

Implementation of Insertion, Deletion, Linear Search, and Binary Search on Arrays in C++, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
18-EE-21
Lab # 01
Title:
To implement insertion, deletion, linear search and
binary search on an array type data structure
Objective:
To know how to insertion, deletion, linear search and binary search on an array type data
structure
Equipment Required:
PC/ Laptop
Dev C++ software
Arrays:
An array is a series of elements of the same type place din contiguous memory locations that can be
individually referenced by adding an index to a unique identifier
That means that, for example, five values of type int can be declared as an array without having to declare
5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in
contiguous memory locations, and all five can be access during the same identifier, with the proper index.
For example, an array containing 5 integer values of type int called A could be represented as:
Declaring 1-D Arrays:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements
required by an array as follows:
Type array Name [array Size];
This is called a single-dimension array. The array Size must be an integer constant greater than zero and
type can be any valid C++ data type. For example, to declare a 10-element array called balance of type
double, use this statement:
Double balance [10];
Initializing 1-D
Arrays:
You can initialize C++ array elements either one by one or using a single statement as follows:
Double balance [5] = {1000.0, 2.0, 3.4, 17.0, and 50.0};
The number of values between braces { } cannot be larger than the number of elements that we declare for
the array between square brackets [ ].Following is an example to assign a single element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementation of Insertion, Deletion, Linear Search, and Binary Search on Arrays in C++ and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 01

 Title:

To implement insertion, deletion, linear search and

binary search on an array type data structure

 Objective:

 To know how to insertion, deletion, linear search and binary search on an array type data

structure

 Equipment Required:

 PC/ Laptop

 Dev C++ software

 Arrays:

An array is a series of elements of the same type place din contiguous memory locations that can be

individually referenced by adding an index to a unique identifier

That means that, for example, five values of type int can be declared as an array without having to declare

5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in

contiguous memory locations, and all five can be access during the same identifier, with the proper index.

For example, an array containing 5 integer values of type int called A could be represented as:

 Declaring 1-D Arrays:

To declare an array in C++, the programmer specifies the type of the elements and the number of elements

required by an array as follows:

Type array Name [array Size];

This is called a single-dimension array. The array Size must be an integer constant greater than zero and

type can be any valid C++ data type. For example, to declare a 10-element array called balance of type

double, use this statement:

Double balance [10];

 Initializing 1 - D

Arrays:

You can initialize C++ array elements either one by one or using a single statement as follows:

Double balance [5] = {1000.0, 2.0, 3.4, 17.0, and 50.0};

The number of values between braces { } cannot be larger than the number of elements that we declare for

the array between square brackets [ ].Following is an example to assign a single element of the array:

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if

you write:

Double balance [ ] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example. Balance [4] =50.0;

The above statement assigns element number 5 thin the array a value of 50.0.Array with 4th index will be

5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base

index. Following is the pictorial representation of the same array we discussed above:

 Accessing Array

Elements:

An element is accessed by indexing the array name. This is done by placing the index of the element

within square brackets after the name of the array. For example:

Double salary = balance [9];

Example: 1

#include #include usingnamespacestd; intmain( ) {int n [10]; for(inti =0;i <10;i++) { n[i ]= i +100; } cout<<"Element"<<setw(13)<<"Value"<<endl; for(intj=0;j<10;j++) { cout<<setw(7)<<j<<setw(13)<<n[j]<<endl; } System(“pause”); return0; }

cout<<"\n------------------------------------------------------\n"; // Loop to store largest number to arr[0] for(i = 1;i < n; ++i) { // Change < to > if you want to find the smallest element if(arr[0][0] < arr[i][j]) arr[0][0] = arr[i][j];} cout << "\n\n\t\tLargest element = " << arr[0][0]<<"\n\n"; cout<<"\n------------------------------------------------------\n"; return 0;}

Output:

Task # 2: Write a program in which user input an element to delete, the element is then

searched in the array, if it is found it is deleted and new array is displayed. If the element is

not present in the array then a not found message is displayed.

Program Code:

#include #include<process.h> using namespace std; int main() { int a[50],x,n,i,j,b[50];

cout<<"\n\tEnter total number of elements : "; cin>>n; cout<<"-----------------------------------------------\n"; cout<<"\nEnter elements of Array\n\n"; for(i=0;i<n;++i) cin>>a[i]; cout<<"\n-----------------------------------------------\n"; cout<<"\n\tEnter element to delete: "; cin>>x; cout<<"\n-----------------------------------------------\n"; for(i=0,j=0;i<n;++i) {if(a[i]!=x) b[j++]=a[i];} if(j==n) {cout<<"\n\tSoory !!! Element is not in the Array"; exit(0);}else { cout<<"\n\tNew Array is : "; for(i=0;i<j;i++) cout<<b[i]<<" ";} return 0;}

Output:

cin>>insert; cout<<"------------------------------------------------------\n"; cout<<"At which position (Enter index number) : "; cin>>pos; cout<<"------------------------------------------------------"; // now create a space at the required position for(i=size; i>pos; i--) {arr[i]=arr[i-1];} arr[pos]=insert; cout<<"\n\t Element inserted successfully...!!\n"; cout<<"------------------------------------------------------\n"; cout<<"Now the new array is : "; for(i=0; i<size+1; i++) {cout<<arr[i]<<" "; } cout<<"\n------------------------------------------------------\n";} else {cout<<"\n------------------------------------------------------\n"; cout<<"\n\n\t\tNo Space for new element max limit 10\n"; } }

Output:

Task # 4: Implement the Linear Search Algorithm on an array. Calculate the total time

required to search an element?

Linear search is implemented using following steps:

Step 1 - Read the search element from the user.  Step 2 - Compare the search element with the first element in the list.  Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function  Step 4 - If both are not matched, then compare search element with the next element in the list.  Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.  Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and terminate the function

Program Code:

#include using namespace std; class LS{ public: void LinearSearch(int arr[], int value, int i, int n) { int found = 0; for (i = 0; i < n ; i++) {if (value == arr[i] ) {found = 1;break;} }if (found == 1) {cout<<"\nElement is present in the array at position : "<<i+1<<endl;} else {

21.87 second required to search an element in linear search

Task # 5: Implement the Binary Search Algorithm on an array. Calculate the total time

required to search an element?

Binary search is implemented using following steps:

Step 1 - Read the search element from the user.  Step 2 - Find the middle element in the sorted list.  Step 3 - Compare the search element with the middle element in the sorted list.  Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.  Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element.  Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.  Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.  Step 8 - Repeat the same process until we find the search element in the list or until sublist contains only one element.  Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the function.

Program Code:

#include using namespace std; int main() {

int count, i, arr[30], num, first, last, middle; cout<<"\n\tHow many elements would you like to enter :"; cin>>count; cout<<"\n------------------------------------------------------\n"; for (i=0; i<count; i++) { cout<<"\tEnter number "<<(i+1)<<": "; cin>>arr[i];} cout<<"\n------------------------------------------------------\n"; cout<<"\tEnter the number that you want to search: "; cin>>num; cout<<"\n------------------------------------------------------\n"; first = 0; last = count-1; middle = (first+last)/2; while (first <= last) { if(arr[middle] < num) {first = middle + 1; } else if(arr[middle] == num) {cout<<"\n\t"<<num<<" Found in the array at the location "<<middle+1<<"\n"; break; } else { last = middle - 1; } middle = (first + last)/2; } if(first > last) { cout<<"\n\t"<<num<<" Not found in the array "; } }