05 Arrays - Searching
05 Arrays - Searching
Data structure
A particular way of storing and organising data in a
computer so that it can be used efficiently
Types of data structures
Based on memory allocation
o Static (or fixed sized) data structures (Arrays)
o Dynamic data structures (Linked lists)
Based on representation
o Linear (Arrays/linked lists)
o Non-linear (Trees/graphs)
Array: motivation
You want to store 5 numbers in a computer
Define 5 variables, e.g. num1, num2, ..., num5
What, if you want to store 1000 numbers?
Defining 1000 variables is a pity!
Requires much programming effort
Any better solution?
Yes, some structured data type
o Array is one of the most common structured data types
o Saves a lot of programming effort (cf. 1000 variable names)
What is an Array?
A collection of data elements in which
all elements are of the same data type, hence
homogeneous data
o An array of students’ marks
o An array of students’ names
o An array of objects (OOP perspective!)
elements (or their references) are stored at
contiguous/ consecutive memory locations
Array is a static data structure
An array cannot grow or shrink during program
execution – its size is fixed
Basic concepts
Array name (data)
Index/subscript (0...9)
The slots are numbered sequentially
starting at zero (Java, C++)
If there are N slots in an array, the
index will be 0 through N-1
Array length = N = 10
Array size = N x Size of an element = 40
Direct access to an element
Homogeneity
All elements in the array must have the same
data type
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4
Value: 5.5 10.2 18.5 45.6 60.5
Index: 0 1 2 3 4
Value: ‘A’ 10.2 55 ‘X’ 60.5 Not an array
Contiguous Memory
Array elements are stored at contiguous memory
locations
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4
Value: 20 40 10 30 60
Target = 30
Step 1: Compare 30 with value at index 0
Step 2: Compare 30 with value at index 1
Step 3: Compare 30 with value at index 2
Step 4: Compare 30 with value at index 3 (success)
Sequential Search
Index: 0 1 2 3 4
Value: 20 40 10 30 60
Target = 45
Step 1: Compare 45 with value at index 0
Step 2: Compare 45 with value at index 1
Step 3: Compare 45 with value at index 2
Step 4: Compare 45 with value at index 3
Step 5: Compare 45 with value at index 4
Failure
Sequential Search Algorithm
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Target (Key) = 30
First iteration: whole list (search space), compare with mid value
Low Index (LI) = 0; High Index (HI) = 9
Choose element with index (0 + 9) / 2 = 4
Compare value at index 4 (45) with the key (30)
30 is less than 45, so the target must be in the lower half of the
list
Binary Search: An Example (Key List)
Second Iteration: Lookup in the reduced search space
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Target (Key) = 40
First iteration: Lookup in the whole list (search space)
Low Index (LI) = 0; High Index (HI) = 9
Choose element with index (0 + 9) / 2 = 4
Compare value at index 4 (45) with the key (40)
40 is less than 45, so the target must be in the lower half of
the list
Binary Search: An Example (Key List)
Second Iteration: Lookup in the reduced search space
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80