Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Start to CodeInstructor: Xiao Liu
Recipe● Sequence of simple steps● Flow of control process that specifies when each
step is executed● A means of determining when to stop
Recipe● Sequence of simple steps● Flow of control process that specifies when each
step is executed● A means of determining when to stop
Algorithm
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.
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]
Lab 4: Task 1Try to print the number with odd index in a list [1,2,3,4,5,6,7,8,9]
SortIf you want to sort the poker...
How will you do?
Random SortIf you want to sort the poker...
Randomly throw them in the air;
Pick them up;
Are they sorted?
Repeat if not sorted.
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
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?
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
Bubble Sort See a demo!
https://visualgo.net/en/sorting
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.
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
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
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.
Insertion Sort● Select the number at index N● Insert it to the right position before index N● Do it for every number
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
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
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))
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]
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]
Search● search algorithm–method for finding an item or group of items with
specific properties within a collection of items.
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
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
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
Lab 4: Task 7Return the index of the found number in the list.
Linear Search
Find 27 in this list:
Using Linear search : compare one by one
1 3 4 5 9 18 27
Linear Search
Find 27 in this list:
Using Linear search : 1 == 27 ?
1 3 4 5 9 18 27
Linear Search
Find 27 in this list:
Using Linear search : 3 == 27 ?
1 3 4 5 9 18 27
Linear Search
Find 27 in this list:
Using Linear search : 4 == 27 ?
1 3 4 5 9 18 27
Linear Search
Find 27 in this list:
Using Linear search : 18 == 27 ?
1 3 4 5 9 18 27
Linear Search
Find 27 in this list:
Using Linear search : 27 == 27 In total compare 7 times
1 3 4 5 9 18 27
Bisection Search
Find 27 in this list:
Any better solutions?
1 3 4 5 9 18 27
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
5 < 27, 27 must be right of 5
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
9 < 27, 27 must be right of 9
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
18 < 27, 27 must be right of 18
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
27 == 27 We find it
Bisection Search
Find 27 in this list:
1 3 4 5 9 18 27
In total 4 comparisons
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