Data Structures and Algorithms Analysis: Arrays
Data Structures and Algorithms Analysis: Arrays
Data Structures and Algorithms Analysis: Arrays
ALGORITHMS ANALYSIS
ARRAYS
Data Structure: Definition
It is a collection of related data and a set of rules
for organizing and accessing it.
A 10 40 50 5 15 1 43 12 23 0
ptr
Assumptions:
A – array name (int type)
ptr – int variable that controls the index
n – size of the array
x – element to be searched (int type)
*** The elements of the array are unique
Array: Sequential Search
Illustration:
0 1 2 3 4 5 6 7 8 9
A 10 40 50 5 15 1 43 12 23 0
ptr
Procedure:
ptr = 0;
while(A[ptr]!=x && ptr<n)
ptr++;
Array Search
2. Binary Search
The data is “halved” at each step of the execution
– meaning, the midway observation of the
section of the array to be searched is used to
compare with the value of the element being
searched.
If the element being searched is greater than the
midway element, then the element (if ever it is in
the array) may exist at the right of the midway
element – never to the left, if the array is sorted
in ascending order.
Array Search
2. Binary Search
Searching is to be done again, but this time, on
the section (which is already “halved”) where the
observation may possibly exist.
The process of locating the midway element in
the constantly “halved” section continues until
the observation is found or the index of the
leftmost element of the sub-array exceeds the
index of the rightmost element of the sub-array.
Array: Binary Search
Illustration:
0 1 2 3 4 5 6 7 8 9
A 0 1 5 10 12 25 43 90 92 99