Lab 05

You might also like

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

Computer Science, Usman Institute of Technology

CS211 - Data Structures and Algorithms

Lab 05

Instructor: Dr. Sharaf Hussain


E-mail: shhussain@uit.edu
Semester: Fall, 2021

1 Objective
The purpose of this lab session is to practice various searching and sorting techniques as we have
discussed in class week.

2 Instructions
You have to perform the following tasks yourselves. Raise your hand if you face any difficulty in
understanding and solving these tasks. Plagiarism is an abhorrent practice and you should not
engage in it.

3 How to Submit
• Submit lab work in a single .py file on Google Classroom. (No other format will be accepted)

• Lab work file name should be saved with your roll number (e.g. 19a-001-SE_LW04.py)
• Submit home work in a single .py file on Google Classroom. (No other format will be accepted)
• Lab work file name should be saved with your roll number (e.g. 19a-001-SE_HW04.py)

4 Exercises - Searching
Task 1: Linear search on unsorted sequence
Implement Linear Search on an unsorted sequence.
1 def l i n e a r S e a r c h ( theValues , t a r g e t ) :
2 n = len ( t h e V a l u e s )
3 for i in r a n g e ( n ) :
4 # If the t a r g e t is in the ith element , r e t u r n True
5 if t h e V a l u e s [ i ] == t a r g e t
6 r e t u r n True
7 r e t u r n F a l s e # If not found , r e t u r n F a l s e .
8

Task 2: Linear search on a sorted sequence.


Implement Linear Search on a sorted sequence.
1 def s o r t e d L i n e a r S e a r c h ( theValues , item ) :
2 n = len ( t h e V a l u e s )
3 for i in r a n g e ( n ) :
4 # If the t a r g e t is f o u n d in the ith element , r e t u r n True
5 if t h e V a l u e s [ i ] == item :
6 r e t u r n True
Computer Science, Usman Institute of Technology

7 # If t a r g e t is l a r g e r than the ith element , it ’ s not in the s e q u e n c e


.
8 elif t h e V a l u e s [ i ] > item :
9 return False
10
11 r e t u r n F a l s e # The item is not in the s e q u e n c e .
12

Task 3: The Binary Search


Implement The Binary serach algorithm:
1 def b i n a r y S e a r c h ( theValues , t a r g e t ) :
2 # S t a r t with the e n t i r e s e q u e n c e of e l e m e n t s .
3 low = 0
4 high = len ( t h e V a l u e s ) - 1
5
6 # R e p e a t e d l y s u b d i v i d e the s e q u e n c e in half u n t i l the t a r g e t is f o u n d .
7 w h i l e low <= high :
8 # Find the m i d p o i n t of the s e q u e n c e .
9 mid = ( high + low ) // 2
10 # Does the m i d p o i n t c o n t a i n the t a r g e t ?
11 if t h e V a l u e s [ mid ] == t a r g e t :
12 r e t u r n True
13 # Or does the t a r g e t p r e c e d e the m i d p o i n t ?
14 elif t a r g e t < t h e V a l u e s [ mid ] :
15 high = mid - 1
16 # Or does it f o l l o w the m i d p o i n t ?
17 else :
18 low = mid + 1
19 # If the s e q u e n c e c a n n o t be s u b d i v i d e d further , we ’ re done .
20 return False
21

5 Exercises - Sorting
Task 1: Bubble Sort
Implement The Bubble Sort algorithm:
1 # S o r t s a s e q u e n c e in a s c e n d i n g o r d e r u s i n g the b u b b l e sort a l g o r i t h m .
2 def b u b b l e S o r t ( t h e S e q ) :
3 n = len ( t h e S e q )
4 # P e r f o r m n -1 b u b b l e o p e r a t i o n s on the s e q u e n c e
5 for i in r a n g e ( n - 1 ) :
6 # B u b b l e the l a r g e s t item to the end .
7 for j in r a n g e ( i + n - 1 ) :
8 if t h e S e q [ j ] > t h e S e q [ j + 1] : # swap the j and j +1 i t e m s .
9 tmp = t h e S e q [ j ]
10 t h e S e q [ j ] = t h e S e q [ j + 1]
11 t h e S e q [ j + 1] = tmp
12

Task 2: Selection Sort


Implement The Selection Sort algorithm:
1 # S o r t s a s e q u e n c e in a s c e n d i n g o r d e r u s i n g the s e l e c t i o n sort a l g o r i t h m
.
2 def s e l e c t i o n S o r t ( t h e S e q ) :
3 n = len ( t h e S e q )
4 for i in r a n g e ( n - 1 ) :
5 # A s s u m e the ith e l e m e n t is the s m a l l e s t .
6 smallNdx = i
7 # D e t e r m i n e if any o t h e r e l e m e n t c o n t a i n s a s m a l l e r v a l u e .
8 for j in r a n g e ( i + 1 , n ) :
9 if t h e S e q [ j ] < t h e S e q [ s m a l l N d x ] :
10 smallNdx = j
11 # Swap the ith v a l u e and s m a l l N d x v a l u e only if the s m a l l e s t v a l u e is
Computer Science, Usman Institute of Technology

12 # not a l r e a d y in its p r o p e r p o s i t i o n . Some i m p l e m e n t a t i o n s omit


testing
13 # the c o n d i t i o n and a l w a y s swap the two v a l u e s .
14 if s m a l l N d x != i :
15 tmp = t h e S e q [ i ]
16 theSeq [i] = theSeq [ smallNdx ]
17 t h e S e q [ s m a l l N d x ] = tmp
18

Task 3: Insertion Sort


Implement The Insertion Sort algorithm:
1 # S o r t s a s e q u e n c e in a s c e n d i n g o r d e r u s i n g the i n s e r t i o n sort a l g o r i t h m
.
2 def i n s e r t i o n S o r t ( t h e S e q ) :
3 n = len ( t h e S e q )
4 # S t a r t s with the f i r s t item as the only s o r t e d e n t r y .
5 for i in r a n g e ( 1 , n ) :
6 # Save the v a l u e to be p o s i t i o n e d .
7 value = theSeq [i]
8 # Find the p o s i t i o n w h e r e v a l u e fits in the o r d e r e d part of the list
.
9 pos = i
10 w h i l e pos > 0 and v a l u e < t h e S e q [ pos - 1] :
11 # S h i f t the i t e m s to the r i g h t d u r i n g the s e a r c h .
12 t h e S e q [ pos ] = t h e S e q [ pos - 1]
13 pos -= 1
14
15 # Put the s a v e d v a l u e into the open slot .
16 t h e S e q [ pos ] = v a l u e
17

6 Lab Tasks
Comlete the Following Lab Taks.

Task 1: Binary Set ADT using Binarry Search


Implement a new Set class using a sorted list with the binary search.

Task 2: Modify Binary Search Algorithm


Modify the binary search algorithm to find the position of the first occurrence of a value that can
occur multiple times in the ordered list.

Task 3: Return list of negative values


Design and implement a function to find all negative values within a given list. Your function should
return a new list containing the negative values.

Task 4: Modify Bag ADT using Binary Search


Implement the Bag ADT from Chapter 1 to use a sorted list and the binary search algorithm. Evaluate
the time complexities for each of the operations.

Task 5: Modify MAP ADT using a Sortd list and Binary Search
Implement a new version of the Map ADT from Section 3.2 to use a sorted list and the binary search
algorithm.

You might also like