You are on page 1of 7

University of Engineering and Technology, Taxila

Experiment 2
Arrays: Implementation of Basic operations, Insertion, Linear Search,
Binary Search.

CLO-2: Use modern tools to solve problems of varying complexities in data


structures.
CLO-3: Construct the projects of varying complexities using different data
structures
CLO-4: Demonstrate a unique solution of problem under discussion
University of Engineering and Technology, Taxila 1

ARRAY
An array is a series of elements of the same type placed in contiguous memory locations
that can be individually referenced by adding an index to a unique identifier. As
discussed in the class/lecture room, there are different algorithms related to an array
(Traversing, Insertion, Deletion, Modify, Sorting, Searching (Linear, Binary). Following
algorithms helps you to understand and write programs of these algos.
Represent a Linear Array in memory
The elements of linear array are stored in consecutive memory locations. It is shown below:

Find the location of element in Linear Array:


The elements of linear array are stored in consecutive memory locations. The computer does
not keep track of address of each element of array. It only keeps track of the base address of
the array and on the basis of this base address the address or location of any element can be
found. We can find out the location of any element by using following formula:

LOC (LA [K]) = Base (LA) + w (K – LB)

Here LOC (LA [K]) is the location of the Kth element of LA.
Base (LA) is the base address of LA.
w is the number of bytes taken by one element.
K is the Kth element.
LB is the lower bound

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 2

Suppose we want to find out Loc (A [3]). For it, we have:


Base (A) = 1000
w = 2 bytes (Because an integer takes two bytes in the memory).
K=3
LB = 1

After putting these values in the given formula, we get:


LOC (A [3]) = 1000 + 2 (3 – 1)
= 1000 + 2 (2)
= 1000 + 4
= 1004
The size of an int is really compiler dependent. Back in the day, when processors
were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32 bits system
or 8 bytes on 64 bits system.
Basic Operations:
Following are the basic operations supported by an array.

 Traverse − print all the array elements one by one.


 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Update − Updates an element at the given index.

Algorithm: (Traversing a Linear Array)


Here LA is a linear array with lower bound LB and upper bound UB. This algorithm
traverses LA applying an operation PROCESS to each element of LA.
• 1. Repeat for k = LB to UB:
• Apply PROCESS to LA [k].
• [End of loop]
• 2. Exit.

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 3

• Caution: The operation PROCESS in the traversal algorithm may use certain
variables that must be initialize before PROCESS is applied to any of the elements in
the array. Accordingly, the algorithm may need to be proceeded by such an
initialization step
Task 1:
1: Declare an array and traverse all of its elements.

Linear search:
A linear search is the basic and simple search algorithm. A linear search searches an element
or value from an array till the desired element or value is not found and it searches in a
sequence order. It compares the element with all the other elements given in the list and if the
element is matched it returns the value index else it return -1. Linear Search is applied on the
unsorted or unordered list when there are fewer elements in a list.

To search the element 5 it will go step by step in a sequence order.

//call the function findIndex with array and number to be searched


findIndex([ 8 , 2 , 6 , 3 , 5 ] , 5) ;

function findIndex(values, target)


{
for(var i = 0; i < values.length; ++i)
{
if (values[i] == target)
{
return i;
}
}
return -1;
}

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 4

Task 2: Apply linear search algorithm to the code above (Task#01) so that user can search
number of its own choice, if does not exist your program must show the output line “Number
does not exist in record” your program must check for UB and LB conditions.

Binary Search:
Binary Search is applied on the sorted array or list. In binary search, we first compare the
value with the elements in the middle position of the array. If the value is matched, then we
return the value. If the value is less than the middle element, then it must lie in the lower half
of the array and if it's greater than the element then it must lie in the upper half of the array.
We repeat this procedure on the lower (or upper) half of the array. Binary Search is useful
when there are large numbers of elements in an array.

Task 3: Input an array from user and then provide search facility so that user can search
its desire number from array, apply binary search algorithm for search operation.

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 5

Insertion Operation:
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array.

Here, we see a practical implementation of insertion operation, where we add data at the end
of the array −

Algorithm:

Let Array be a linear (unordered )array of MAX elements.

Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that
K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −

1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop

Example

Following is the implementation of the above algorithm .When we compile and execute the
above program, it produces the following result −

Output:
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8

Task 4:
1: Declare an array whose size u can change at run time and insert new elements.

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 6

Exercise#01: (Upload your exercise work before next DSA lab on LMS portal, exercise-uploading
time will expire at 12:00am, Copied code will be marked 0).
Viva of Exercise will be in coming lab:
1- Write a Program to create a dynamic array has N elements. Get integer
values into array and then provide the following functionality:
a: Print all even numbers available in the array.
b: Print all Odd numbers available in the array.
c: Print all prime numbers available in the array.
d: Print all complete square numbers (e.g. 4, 9,16, 25 etc.) available in the
array.

2- Write a C++ program to find the duplicate values of an array of integer


values.
3- Write a C++ program to find the common elements between two arrays
of integers
4- Write a C++ program to get the difference between the largest and
smallest values in an array of integers.

Engr. Rabia Arshad

You might also like