Arrays
Arrays
a. The lower bound is the smallest subscript you can use in the
array (usually 0)
b. The upper bound is the largest subscript you can use in the
array
** The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
Linear Arrays
Example :
A linear array DATA consisting of the name of six elements
Example :
An automobile company uses an array AUTO to record the number of
‘automobile’ sold each year from 1932 through 1984.
ALGORITHM
• That is first we test whether DATA[1 ]=ITEM, and then we test whether
DATA[2 ]=ITEM, and so on.
1. Read: ITEM.
2. Set LOC:=1.
3. Repeat while DATA[LOC]!= ITEM and LOC <=N:
Set LOC := LOC +1.
[ End of loop]
4. If LOC = N+1, then :
Write : ITEM is not in the array DATA.
Else :
Write : LOC is the location of ITEM.
5. Exit.
Linear Search Applications
• For searching operations in smaller arrays (<100
items).
Searching : Binary search
• Binary search is the most popular Search
algorithm. It is efficient and one of the most used
techniques that is used to solve problems.
Columns
1 2 3
1 A[1, 1] A[1, 2] A[1, 3]
Rows 2
A[2, 1] A[2, 2] A[2, 3]
3
A[3, 1] A[3, 2] A[3, 3]
• Formula:
Base(C) + w[(….((E1L2 + E2)L3 + E3)L4 +…. + EN-1)LN + EN]
Solution
• The lengths of the three dimensions of MAZE are:
L1 = 8 – 2 + 1 = 7
L2 = 1 –(-4) + 1 = 6
L3 = 10 – 6 + 1 = 5
• Total number of elements in MAZE = 7 x 6 x 5 = 210
• To find the address of the element MAZE[5, -1, 8] we
need to calculate the effective index (E)
E1 = 5 – 2 = 3
E2 = -1 – (-4) = 3
E3 = 8 – 6 = 2
• (E1L2 + E2)L3 + E3 = 107
• Therefore, LOC(MAZE[5, -1, 8]) = 200 + 4(107) = 628
Reference
• DSA Linear Search
• DSA Binary Search
• https://www.programiz.com/dsa/linear-search
• https://www.programiz.com/dsa/binary-search
• https://www.cs.usfca.edu/~galles/visualization/Search.html