Professional Documents
Culture Documents
Arrays
Arrays
2.1 Introduction
2
2.1 Introduction
Advantages of an Array:
Very simple
Economy – if full use of memory
Random accessed at the same time
Disadvantage of an Array:
wasting memory if not fully used
3
2.2 Linear Array
Homogeneous data:
a) Elements are represented through indexes.
b) Elements are saved in sequential in memory locations.
Number of elements, N –> length or size of an array.
If:
UB : upper bound ( the largest index)
LB : lower bound (the smallest index)
Then: N = UB – LB + 1
Length = N = UB when LB = 1
4
5
2.2.1 Representation of Array in a Memory
6
7
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.
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
8
2.2.1 Representation of Array in a Memory
Example:
If LB = 5,
Loc(X[LB]) = 1200,
and w = 4, find Loc(X[8]) ?
Loc(X[8])= Loc(X[5]) + 4*(8 – 5)
= 1212
9
2.2.2 Traversing Algorithm
10
Static vs Dynamic Arrays
A STATIC ARRAY is declared at compile time; thus, its size must be
a compile time constant. FORTRAN, COBOL and Pascal, use this
technique exclusively. These cannot have variable sizes because
allocation of the array is done at compile-time. These are usually
placed on the stack. No re-sizing is allowed.
A DYNAMIC ARRAY may be declared at compile-time but it is not
dimensioned (the number of elements determined) until run-time.
Thus, the value that determines the size may come from a constant or
variable. Furthermore, the array may be allocated and then de-
allocated multiple times and with different sizes. Java are languages
that only allow dynamic arrays.
Several languages, such as C, C++ and several forms of BASIC, allow
both.
2.2.3 Insertion Algorithm
2.2.3 Insertion Algorithm
Example 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 −
INSERT(LA, N, K, ITEM)
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
14
2.2.4 Deletion Algorithm
2.2.4 Deletion Algorithm
Algorithm: Deletion refers to removing an existing element from the
array and re-organizing all elements of an array.
Consider LA is a linear array with N elements and K is a positive
integer such that K<=N. Following is the algorithm to delete an
element available at the Kth position of LA.
DELETE(LA, N, K, ITEM)
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop 16
2.2.5 Linear Search
Linear search is a very basic and simple search algorithm. In Linear search,
we search an element or value in a given array by traversing the array
from the starting, till the desired element or value is found.
It compares the element to be searched with all the elements present in
the array and when the element is matched successfully, it returns the
index of the element in the array, else it return -1.
Linear Search is applied on unsorted or unordered lists, when there are
fewer elements in a list.
Features of Linear Search Algorithm
1. It is used for unsorted and unordered small list of elements.
2. It has a time complexity of O(n), which means the time is linearly
dependent on the number of elements, which is not bad, but not that
good too.
3. It has a very simple implementation.
17
Linear Search ( Array A, Value x)
Step 1: i=1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: i=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
2.2.5 Binary Search Algorithm
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.
Following are the steps of implementation that we will be following:
Start with the middle element:
If the target value is equal to the middle element of the array, then return the
index of the middle element.
If not, then compare the middle element with the target value,
If the target value is greater than the number in the middle index, then pick
the elements to the right of the middle index, and start with Step 1.
If the target value is less than the number in the middle index, then pick the
elements to the left of the middle index, and start with Step 1.
When a match is found, return the index of the element matched.
19
If no match is found, then return -1
2.2.5 Binary Search Algorithm
20
2.2.5 Binary Search Algorithm
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <=END
Step 3: SET MID = (BEG + END)/2
Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT 21
Merge of two sorted arrays
2.2.6 Merging
Suppose
Algorithm
1) A is a sorted list with R elements
2) B is a sorted list with S elements.
3) The operation that combines the element of A and B into a single sorted
list C with N=R + S elements is called merging.
24
2.2.6 Merging Algorithm
Algorithm: Here A and B be sorted arrays with R and S elements
respectively. This algorithm merges A and B into an array C with N=R+ S
elements.
Merging (A, R,B,S,C) Step 4: If NA >R, then:
Step 1: Start Repeat while NB ≤ S:
Step 2:Set NA=1, Set C[NC] = B[NB]
NB=1 Set NB = NB+1
NC=1 Set NC = NC +1
Step 3: Repeat while NA ≤ R and NB ≤ S: [End of Loop]
else
if A[NA] ≤ B[NB], then: Repeat while NA ≤ R:
Set C[NC] = A[NA] Set C[NC] = A[NA]
Set NA = NA +1 Set NC = NC + 1
else Set NA = NA +1
Set C[NC] = B[NB] [End of loop]
Set NB = NB +1 [End of if structure]
[End of if structure] Step 5: Return C[NC]
Set NC= NC +1 Step 6: Stop
[End of Loop]
26
2.2.6 Merging Algorithm
Complexity of merging: The input consists of the total number n=r+s elements in A
and B. Each comparison assigns an element to the array C, which eventually has n
elements. Accordingly, the number f(n) of comparisons cannot exceed n:
f(n) ≤ n = O(n)
27
Bubble Sort