0% found this document useful (0 votes)
13 views

algo5

Uploaded by

Minh Tâm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

algo5

Uploaded by

Minh Tâm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Algorithmics and Programming

Lecture 5

Cyrille Rosset
Université Paris Cité

11 November 2022

C. Rosset Algorithmics & Programming 5 11 November 2022 1 / 12


Outline

1 Searching algorithms
Sequential search
Binary search
Binary tree search

C. Rosset Algorithmics & Programming 5 11 November 2022 2 / 12


Outline

1 Searching algorithms
Sequential search
Binary search
Binary tree search

C. Rosset Algorithmics & Programming 5 11 November 2022 3 / 12


Introduction

Searching a specific element in group of items is very common.


For example, you may want to search for a word in a dictionary.
Typical operations related to searching are:
Initialize the data structure
Search for an item
Insert a new item
Delete a specified item
We will look at different algorithm:
Sequential search
Binary search
Binary tree search

C. Rosset Algorithmics & Programming 5 11 November 2022 4 / 12


Outline

1 Searching algorithms
Sequential search
Binary search
Binary tree search

C. Rosset Algorithmics & Programming 5 11 November 2022 5 / 12


Sequential search

Informal description
In the sequential search, we search for an item by iterating over each data element
and comparing to to the searched item.

Code
def sequential_search(x, array):
for i, item in enumerate(array):
if x == item:
return i # if we find it, we return its index
return None

Complexity
Space: O(1)
Time: it depends where is the searched element. We distinguish best case (1),
average case (N/2) and worst case (N).

C. Rosset Algorithmics & Programming 5 11 November 2022 6 / 12


Outline

1 Searching algorithms
Sequential search
Binary search
Binary tree search

C. Rosset Algorithmics & Programming 5 11 November 2022 7 / 12


Binary search

Informal description
When searching a word in the dictionary, you will usualli no go through each page,
but rather look directly in the middle, then check if the word you’re looking for is
before an after, and you will repeat the opration until finding the word.
Binary search is using this technic, dividing each time the search interval by 2.
Note that the input array must be sorted!

Code
def binary_search(x, array):
lo, hi = 0, len(array)-1
while lo <= hi:
i = (lo + hi) // 2
if array[i] == x:
return True
elif array[i] > x:
hi = i - 1
else:
lo = i + 1
return False

C. Rosset Algorithmics & Programming 5 11 November 2022 8 / 12


Binary search

Complexity
Average case: O(log N) (not taking into account the sorting).
Worst case: O(log N) as well.

C. Rosset Algorithmics & Programming 5 11 November 2022 9 / 12


Outline

1 Searching algorithms
Sequential search
Binary search
Binary tree search

C. Rosset Algorithmics & Programming 5 11 November 2022 10 / 12


Binary tree search

Description
Very important algorithm, though very simple.
For binary tree search, we build a binary tree where each node contain an element
of data (so we have N nodes), and each node is such that all nodes on its left
branch are smaller and all nodes on its right branch are larger or equal.

A R

C H

C. Rosset Algorithmics & Programming 5 11 November 2022 11 / 12


Binary tree search

Searching in binary tree


Once the binary tree is built, we can search into it with the following algorithm:
def btreesearch(node, x):
while node.item != x and node is not tree.TERM:
if x < node.item:
node = node.left
else:
node = node.right
if node.item == x:
return True
else:
return False

Building the binary tree


Before using the tree for searching, you need to build it.
initilialize a new tree
iterate over each input item and insert them in the tree (at the correct place!)
Exercise: create the functions to create and build a binary search tree.
C. Rosset Algorithmics & Programming 5 11 November 2022 12 / 12

You might also like