Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

‫بسم هللا الرحمن الرحيم‬

King Abdulaziz University


Faculty of Computing and information Technology
Computer Science Department

CPCS-204 Data Structures I


4- Searching Algorithms

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 1
Array Operations: Searching
• Through the array
• Depends on the algorithm
• Some algorithms are faster than others
• Linear search
• Binary search

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 2
Linear Search
• Array of size n
• Assume array is sorted

• Value you looking for: key


• Search from first element towards the last element
• Maximum of n searches
key

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 3
Linear Search
• Compare value in array with what you are looking for
• If see value searching for
• Return found it (true)

• Otherwise, iterated array & haven’t located value


• Return that the value is not found (false)
public static boolean linSearch(int[] a, int key) {
for (int i=0; i<a.length; i++) {
if (a[i] == key) {
return true;
}
}
return false;
}
CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 4
Linear Search
• If we want to know where the value was found?
• Meaning, at what index is the value found?
• found: return index
• not found: return -1

public static int linSearch(int[] a, int key) {


for (int i=0; i<a.length; i++) {
if (a[i] == key) {
return i;
}
}
return -1;
}
CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 5
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 6
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 7
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 8
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 9
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 10
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 11
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 12
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 13
Linear Search
0 1 2 3 4 5 6 7 8 9

2 5 6 12 1 7 15 8 3 18

15

Search Key: 15

Found value in element: 6

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 14
Linear Search: Analyze code
• Clearly, if array is unsorted, this algorithm is optimal
• The only way to be sure that a value isn’t in the array is to
look at every single spot of the array
• Could find an item in array faster if it were sorted?

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 15
Number Guessing Game
• Secret number: 1 to 100
• Make a guess, guess is H or L
• Guess again, continues until guess correct number
• Job is to minimize number of guesses made

1 2 3 … 24 25 26 … 49 50 51 … 74 75 76 … 98 99 100

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 16
Number Guessing Game
• What is the first guess of most people?
• Why?
• No matter response (H or L), most number of possible values for
remaining search is 50
• Any other first guess: risk (possible values > 50)
• Example: Guess: 76
– Respond: H
– Now guess between 1 and 75 (75 instead of 50)
H L

1 2 3 … 24 25 26 … 49 50 51 … 74 75 76 … 98 99 100

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 17
Basic Winning Strategy
• Always guess the number that is halfway between lowest and
highest possible values in the range
• Can we now adapt this idea to work for searching for a given
value in an array?

1 2 3 … 24 25 26 … 49 50 51 … 74 75 76 … 98 99 100

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 18
Array Search
• Given sorted array and searching for value: 12
• So where is halfway between?
• One guess: look at 10 and 88 and take average: 49
• But 49 isn’t even in the list

• And if we look at the number closest to 49


• Almost at the end of the array

0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 19
Array Search
• Realize that: to adapt number guessing game strategy to
searching an array, must search in middle index of array
• In this case
• lowest index: 0
• highest index: 8
• middle index: (0 + 8) /2 = 4

0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 20
Array Search
• Realize that: to adapt number guessing game strategy to
searching an array, must search in middle index of array
• In this case
• lowest index: 0
• highest index: 8
• middle index: (0 + 8) /2 = 4
• At index 4, value: 26
• not found
0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 21
Array Search: Correct Strategy
• Is number 12, > or < number stored at index 4 (26)?

0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 22
Array Search: Correct Strategy
• Is number 12, > or < number stored at index 4 (26)?
• Answer: "less than"

0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 23
Array Search: Correct Strategy
• Is number 12, > or < number stored at index 4 (26)?
• Answer: "less than"

• Modify search range to be between indices 0 and 3


• We then continue this process
• Second index to look at is index: (0 + 3) /2 = 1

0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 24
Array Search: Correct Strategy
• Is number 12, > or < number stored at index 4 (26)?
• Answer: "less than"

• Modify search range to be between indices 0 and 3


• We then continue this process
• Second index to look at is index: (0 + 3) /2 = 1
• At index 1, value: 12

0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 25
Array Search: Correct Strategy
• Is number 12, > or < number stored at index 4 (26)?
• Answer: "less than"

• Modify search range to be between indices 0 and 3


• We then continue this process
• Second index to look at is index: (0 + 3) /2 = 1
• At index 1, value: 12
• found
0 1 2 3 4 5 6 7 8
10 12 18 23 26 30 33 57 88

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 26
Binary Search
public static boolean binSearch(int[] a, int value) {
int low = 0;
int high = a.length -1;
while (low <= high) {
int mid = (low + high)/2;
if (value == a[mid])
return true; // found
else if (value < a[mid]) // high
high = mid - 1;
else // low
low = mid + 1;
}
return false; // if not found
}

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 27
Binary Search: Comments
• As with the basic Array Search, this binSearch method
returns a boolean value
• Better
• if return index where the value was found
• if not found, return -1

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 28
Binary Search
public static boolean binSearch(int[] a, int value) {
int low = 0;
int high = a.length -1;
while (low <= high) {
int mid = (low + high)/2;
if (value == a[mid])
return true; // found
else if (value < a[mid]) // high
high = mid - 1;
else // low
low = mid + 1;
}
return false; // if not found
}

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 29
Binary Search
public static int binSearch(int[] a, int value) {
int low = 0;
int high = a.length -1;
while (low <= high) {
int mid = (low + high)/2;
if (value == a[mid])
return mid; // the index of value
else if (value < a[mid]) // high
high = mid - 1;
else // low
low = mid + 1;
}
return -1; // if not found
}

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 30
Binary Search Analysis
• At end of each array iteration, all we do is update either L or H
• Modifies our search region
• Essentially halving it
• How many comparisons (guesses) are necessary when running
this algorithm on an array of n items

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 31
Binary Search Analysis
• Assume n = 100, after
• 1 guess, 50 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 32
Binary Search Analysis
• Assume n = 100, after
• 1 guess, 50 items left
• 2 guesses, 25 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 33
Binary Search Analysis
• Assume n = 100, after
• 1 guess, 50 items left
• 2 guesses, 25 items left
• 3 guesses, 12 items left
• 4 guesses, 6 items left
• 5 guesses, 3 items left
• 6 guesses, 1 item left
• 7 guesses, 0 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 34
Binary Search Analysis
• Reason for last iteration is because number of items left
represent number of other possible values to search
• We need to reduce this to 0

• Also, when n is odd, such as when n=25, we search the middle


element, # 13
• There are 12 elements smaller than 13, and 12 elements bigger than 13
• This is why number of items is slightly less than ½ in those cases

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 35
Binary Search Analysis
• General case for n items: after
• 1 guess, n/4 = n/22 items left
• 2 guesses, n/4 = n/22 items left
• 3 guesses, n/8 = n/23 items left
• 4 guesses, n/16 = n/24 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 36
Binary Search Analysis
• General case for n items: after
• 1 guess, n/4 = n/22 items left
• 2 guesses, n/4 = n/22 items left
• 3 guesses, n/8 = n/23 items left
• 4 guesses, n/16 = n/24 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 37
Binary Search Analysis
• General case for n items: after
• 1 guess, n/2 = n/21 items left
• 2 guesses, n/4 = n/22 items left
• 3 guesses, n/8 = n/23 items left
• 4 guesses, n/16 = n/24 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 38
Binary Search Analysis
• General case for n items: after
• 1 guess, n/2 = n/21 items left
• 2 guesses, n/4 = n/22 items left
• 3 guesses, n/8 = n/23 items left
• 4 guesses, n/16 = n/24 items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 39
Binary Search Analysis
• General case for n items: after
• 1 guess, n/2 = n/21 items left
• 2 guesses, n/4 = n/22 items left
• 3 guesses, n/8 = n/23 items left
• 4 guesses, n/16 = n/24 items left
• …

• After k guesses, n/2k items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 40
Binary Search Analysis
• General case for n items: after
• 1 guess, n/2 = n/21 items left
• 2 guesses, n/4 = n/22 items left
• 3 guesses, n/8 = n/23 items left
• 4 guesses, n/16 = n/24 items left
• …

• After k guesses, n/2k items left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 41
Binary Search Analysis
• Question: How many k guesses do we need to make in order to
find our answer?
• Or until we have one and only one guess left to make?
• So, we want to get only 1 item left
• If we can find the value that makes the above fraction equal
to 1, then we know that in one more guess, we’ll narrow down
the item

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 42
Efficiency of Binary Search
• So, after k guesses, we have n/2k items left
• Again, we want only 1 item left

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 43
Efficiency of Binary Search
• So, after k guesses, we have n/2k items left
• Again, we want only 1 item left
• So set this equal to 1 and solve for k

𝒏
𝒌
=𝟏 𝒏 = 𝟐𝒌 𝒌 = 𝐥𝐨𝐠 𝟐 𝒏
𝟐
• Binary search roughly takes log2 n comparisons when
searching in sorted array of size n

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 44
Efficiency of Binary Search
• Runs in logarithmic log n time
• Compare to n time
n log n
4 2
• MUCH faster than linear search 8 3
64 6
512 9
• Any log n algorithm is SUPER FAST 1024 10
1048476 20

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 45
Efficiency
• Linear search
• O(n)

• Binary search
• O(log n)

• Binary search is faster than linear search

CPCS204, Data Structures I, Updated by Pr. Abdullah Basuhail, CS, FCIT, KAU, 1442H-2021G 46

You might also like