Sei sulla pagina 1di 17

Lab 04 : Ordered Arrays

EE-264 Data Structures & Algorithms


Objectives
• To show insertion, search and deletion in
ordered arrays
• Concept of Binary Search
• Linear Search v/s Binary Search

EE-264 Data Sturcutres & Algorithms,


2
Electrical Engineering Department
Ordered Arrays
• Ordered array is a special array in terms of
arrangement, an ordered array is array in
which data is stored in ascending (or
descending) key order.
• Due to this arrangement, searching process
speeds up.

EE-264 Data Sturcutres & Algorithms,


3
Electrical Engineering Department
EE-264 Data Sturcutres & Algorithms,
4
Electrical Engineering Department
Insertion in Ordered Array
• Inserting data into an ordered array is little
tricky as compared to Unordered Array.
• Data item in ordered array is not placed in the
next vacant place.
• It rather requires reorganizing the array so
that entire remains in the ordered fashion.
• Search for the right place – rearrange
remaining items
EE-264 Data Sturcutres & Algorithms,
5
Electrical Engineering Department
Consider this array, let us suppose
we wish to insert 565 in this array.

Since data keys must remain in order,


so 565 must be placed after 514 and
before 573.

This requires evacuating location 6,


so that it can be filled by 565.

Therefore moving 573 to location 7,


and so on. So that array size
becomes 10

EE-264 Data Sturcutres & Algorithms,


6
Electrical Engineering Department
• So insertion process in ordered array is a little
tricky and computationally expensive
• The advantage of Ordered Array becomes
evident when Search Process takes place.
• It is therefore, Ordered Array is preferred
when Searching is MORE FREQUENT then data
insertion.

EE-264 Data Sturcutres & Algorithms,


7
Electrical Engineering Department
Searching
• Searching process in ordered array is easier
than searching in Unordered array
• Consider this case, for example
If we wish to find a key 480
which certainly is not present
in the data set
We will look till 513 and
terminate the search process,
since the next data items are
greater than 480

EE-264 Data Sturcutres & Algorithms,


8
Electrical Engineering Department
• The approach of Linear Search however on
Average will take N/2 comparisons.
• This searching process can speed up, if we
apply the idea of Binary Search

EE-264 Data Sturcutres & Algorithms,


9
Electrical Engineering Department
Binary Search
• Binary Search is based on divide and conquer approach
• We find the mid point of the array.
• Since data is ordered, items on one hand side of mid
point will be less than the mid point and on the other
hand they will be greater.
• If mid point is the search, we are fortunate: if not then
look only to that side of the midpoint which is relevant,
i.e. if searchKey is greater than midpoint look on the
side with greater keys only otherwise look the other
side.
• Keep doing it until you find the item, or array can’t be
further broken down
EE-264 Data Sturcutres & Algorithms,
10
Electrical Engineering Department
Pseudo Code
1. Let min = 0 and max = n-1.
2. Compute guess as the average of max and min,
rounded down (so that it is an integer).
3. If array[guess] equals target, then stop. You
found it! Return guess.
4. If the guess was too low, that is,
array[guess] < target, then set min = guess + 1.
5. Otherwise, the guess was too high.
Set max = guess - 1.
6. Go back to step 2.
Source : https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/implementing-binary-search-of-an-array

EE-264 Data Sturcutres & Algorithms,


11
Electrical Engineering Department
Graphical Depiction

EE-264 Data Sturcutres & Algorithms,


12
Electrical Engineering Department
Time to code
• See lab_04_code_01.cpp

EE-264 Data Sturcutres & Algorithms,


13
Electrical Engineering Department
Time to analyze

EE-264 Data Sturcutres & Algorithms,


14
Electrical Engineering Department
EE-264 Data Sturcutres & Algorithms,
15
Electrical Engineering Department
In general, for Binary Search

EE-264 Data Sturcutres & Algorithms,


16
Electrical Engineering Department
“The wound is the place where the Light enters you.”
― Molana Jalal uddin Rumi

EE-264 Data Sturcutres & Algorithms,


17
Electrical Engineering Department

Potrebbero piacerti anche