Professional Documents
Culture Documents
Lab 05
Lab 05
Lab 05
Lab 05
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
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
6 Lab Tasks
Comlete the Following Lab Taks.
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.