Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For...
Transcript of Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For...
![Page 1: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/1.jpg)
Start to CodeInstructor: Xiao Liu
![Page 2: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/2.jpg)
Recipe● Sequence of simple steps● Flow of control process that specifies when each
step is executed● A means of determining when to stop
![Page 3: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/3.jpg)
Recipe● Sequence of simple steps● Flow of control process that specifies when each
step is executed● A means of determining when to stop
Algorithm
![Page 4: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/4.jpg)
Basic Algorithm in Real life● Rank the top 5 football players
○ Sorting with their current performance and potentials
● Looking up a name in an alphabetically sorted list○ Linear: start at the top○ Binary search: start in the middle
● Standing in line at a bank, supermarket, customs & immigration○ Performance analysis of task scheduling
● Cooking a gourmet meal○ Parallel processing: You don’t want the meat to get cold while you’re cooking the
vegetables.
![Page 5: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/5.jpg)
List# AssignmentA = [1, 5, 7, 2, 4, 6]# Accessfor i in range(6):
print A[i]# Swap A[1], A[2]A[1], A[2] = A[2], A[1]
![Page 6: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/6.jpg)
Lab 4: Task 1Try to print the number with odd index in a list [1,2,3,4,5,6,7,8,9]
![Page 7: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/7.jpg)
SortIf you want to sort the poker...
How will you do?
![Page 8: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/8.jpg)
Random SortIf you want to sort the poker...
Randomly throw them in the air;
Pick them up;
Are they sorted?
Repeat if not sorted.
![Page 9: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/9.jpg)
Random SortIf you want to sort the poker...
Randomly throw them in the air;
Pick them up;
Are they sorted?
Repeat if not sorted.
Monkey Sort
![Page 10: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/10.jpg)
Random SortIf you want to sort the poker...
Randomly throw them in the air;
Pick them up;
Are they sorted?
Repeat if not sorted.
Can we do in a wise way?
![Page 11: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/11.jpg)
Bubble Sort● Lighter goes Higher● Compare consecutive pairs ● Swap the elements, smaller first● When reach end of list, start over● Stop when no more swaps
![Page 12: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/12.jpg)
Bubble Sort See a demo!
https://visualgo.net/en/sorting
![Page 13: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/13.jpg)
Lab 4: Task 2How many times does the bubble sort scan from the beginning to end?
For [1,3,5,7,2,6,25,18,13]?For [1,9,4,6,8]?
Answer it by print out the sorted list in each round.
![Page 14: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/14.jpg)
Selection Sort● Extract minimum element● Swap it with element at index 0● In remaining sublist, extract minimum element● Swap it with the element at index 1
![Page 15: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/15.jpg)
Selection Sort● Extract minimum element● Swap it with element at index 0● In remaining sublist, extract minimum element● Swap it with the element at index 1
See a demo!
https://visualgo.net/en/sorting
![Page 16: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/16.jpg)
Lab 4: Task 3How many times does the selection sort scan from the beginning to end?
For [1,3,5,7,2,6,25,18,13]?For [1,9,4,6,8]?
Answer it by print out the sorted list in each round.
![Page 17: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/17.jpg)
Insertion Sort● Select the number at index N● Insert it to the right position before index N● Do it for every number
![Page 18: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/18.jpg)
Insertion Sort● Select the number at index N● Insert it to the right position before index N● Do it for every number
See a demo!
https://visualgo.net/en/sorting
![Page 19: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/19.jpg)
Lab 4: Task 4def insertionSort(L): for index in range(1,len(L)): currentvalue = L[index] position = index while position>0 and ??: L[position]=L[position-1] position = position-1
L[position]=currentvalue
![Page 20: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/20.jpg)
Understand EfficiencyCompare the three sorting algorithm
Let’s time the execution by -
import timedef bubble_sort(L):
...start_time = time.time()bubble_sort(testList)print("bubble_sort: " + str(time.time() - start_time))
![Page 21: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/21.jpg)
Lab 4: Task 5Time the execution of bubble sort on the list -testList = [1,3,5,7,2,6,25,18,13]
testList = [9,8,7,6,5,4,3,2,1]
testList = [1,2,3,4,5,6,7,8,9]
![Page 22: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/22.jpg)
Lab 4: Task 6Time the execution of all sort algorithms on the list -testList = [1,3,5,7,2,6,25,18,13]
testList = [9,8,7,6,5,4,3,2,1]
testList = [1,2,3,4,5,6,7,8,9]
![Page 23: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/23.jpg)
Search● search algorithm–method for finding an item or group of items with
specific properties within a collection of items.
![Page 24: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/24.jpg)
Search● search algorithm–method for finding an item or group of items with
specific properties within a collection of items.● Collection could be explicit
○ Is a student record in a stored collection of data?
● Collection could be implicit○ Find the square root of a given number
![Page 25: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/25.jpg)
Search Algorithm● Linear Search
○ Brute force search○ List does not have to be sorted
● Bisection search○ List must be sorted to give a correct answer○ See two implementations of the algorithm
![Page 26: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/26.jpg)
Linear Searchdef linear_search(L, e): found = False for i in range(len(L)): if e == L[i]: found = True return found
testList = [1, 3, 4, 5, 9, 18, 27]
1 3 4 5 9 18 27
![Page 27: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/27.jpg)
Lab 4: Task 7Return the index of the found number in the list.
![Page 28: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/28.jpg)
Linear Search
Find 27 in this list:
Using Linear search : compare one by one
1 3 4 5 9 18 27
![Page 29: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/29.jpg)
Linear Search
Find 27 in this list:
Using Linear search : 1 == 27 ?
1 3 4 5 9 18 27
![Page 30: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/30.jpg)
Linear Search
Find 27 in this list:
Using Linear search : 3 == 27 ?
1 3 4 5 9 18 27
![Page 31: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/31.jpg)
Linear Search
Find 27 in this list:
Using Linear search : 4 == 27 ?
1 3 4 5 9 18 27
![Page 32: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/32.jpg)
Linear Search
Find 27 in this list:
Using Linear search : 18 == 27 ?
1 3 4 5 9 18 27
![Page 33: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/33.jpg)
Linear Search
Find 27 in this list:
Using Linear search : 27 == 27 In total compare 7 times
1 3 4 5 9 18 27
![Page 34: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/34.jpg)
Bisection Search
Find 27 in this list:
Any better solutions?
1 3 4 5 9 18 27
![Page 35: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/35.jpg)
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
5 < 27, 27 must be right of 5
![Page 36: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/36.jpg)
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
9 < 27, 27 must be right of 9
![Page 37: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/37.jpg)
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
18 < 27, 27 must be right of 18
![Page 38: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/38.jpg)
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
27 == 27 We find it
![Page 39: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/39.jpg)
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
In total 4 comparisons
![Page 40: Start to CodeLab 4: Task 2 How many times does the bubble sort scan from the beginning to end? For [1,3,5,7,2,6,25,18,13]? ...](https://reader035.fdocuments.net/reader035/viewer/2022070708/5eb94d492b65d65b3e2da467/html5/thumbnails/40.jpg)
Lab 4: Task 8def binary_search(x, search_list): left = 0 right = len(search_list)-1 mid = (right + left)/2 while search_list[mid] != x: if ??: left = mid + 1 else: right = mid - 1 mid = (right + left)/2 return mid