Professional Documents
Culture Documents
Experiment 2
Arrays: Implementation of Basic operations, Insertion, Linear Search,
Binary Search.
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:
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
• 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.
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.
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 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.
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.