首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
辅导CSC111语言编程、讲解Algorithms程序、Python设计程序辅导 解析C/C++编程|讲解SPSS
项目预算:
开发周期:
发布时间:
要求地区:
CSC111 Tutorial 9: Iterative Sorting Algorithms
Algorithms
In this week’s lecture, you learned about selection sort and insertion sort, two iterative sorting
algorithms. Today, you’ll get some more practice with these sorting algorithms, some running-time
analysis, and develop a simple sorting visualization tool using pygame.
Part 1: Variations on selection sort and insertion
sort
When you first learn a new algorithm like selection sort or insertion sort, it can be tempting to just
memorize the code for that algorithm. However, blindly memorizing code isn’t actually a good strategy
for gaining deep understanding of what that algorithm is doing, or how to implement it. So in the first
part of this tutorial, we’ll use a different strategy to reinforce your learning of these two sorting
algorithms: implementing variations of them.
Download tutorial9_part1.py (starter-files/tutorial9_part1.py) and open it. Inside, you’ll see our
implementations of selection sort and insertion sort from lecture, as well as a few new functions to
implement. Each of these functions can be implemented using a very similar approach as one of our tw
sorting algorithms, but it’ll be up to you to figure out what changes you need to make.
As you complete each function, we recommend comparing your implementation against the code from
lecture, so that you can see both the similarities and differences in the code.
Part 2: Running-time analysis for binary search
In the prep reading for this week, you learned about the binary search algorithm, which can search for
an item in a list faster than the standard linear search algorithm, but only works on sorted list. Here’s
the implementation from Section 16.1 (https://www.teach.cs.toronto.edu/~csc110y/fall/notes/16-
sorting/01-sorting-intro.html):
3/25/2021 CSC111 Tutorial 9: Iterative Sorting Algorithms
https://www.teach.cs.toronto.edu/~csc111h/winter/tutorials/09-iterative-sorting/ 2/6
The course notes briefly discussed the running time of binary search, but only at a high level. Intuitivel
at each loop iteration the size of the sublist being searched decreases by a factor of 2, and so there can b
at most (approximately) iterations in total. But how do we formalize this intuition?
We run into trouble because we don’t have a predictable formula the values of variables b and e after
iterations, since how the search range changes depends on the contents of lst and the item being
searched for.
We can reconcile the intuition with our more formal approach by explicitly introducing and analysing
the behaviour of a new variable. Specifically: we let be a variable representing the size of the
search range ( stands for “range”).
def binary_search(lst: list, item: Any) -> bool:
"""Return whether item is in lst using the binary search algorithm.
Preconditions:
- is_sorted(lst)
"""
b = 0
e = len(lst)
while b < e:
# Loop invariants
assert all(lst[i] < item for i in range(0, b))
assert all(lst[i] > item for i in range(e, len(lst)))
m = (b + e) // 2
if item == lst[m]:
return True
elif item < lst[m]:
e = m
else: # item > lst[m]
b = m + 1
# If the loop ends without finding the item, the item is not in the list.
return False
log2 n
k
r = e − b
r
3/25/2021 CSC111 Tutorial 9: Iterative Sorting Algorithms
https://www.teach.cs.toronto.edu/~csc111h/winter/tutorials/09-iterative-sorting/ 3/6
Using this definition, answer the questions below. By the end of this part, you’ll find a good upper boun
on the worst-case running time of binary_search.
1. Let represent the length of the input list lst. What is the initial value of , in terms of ?
2. For what value(s) of will the loop terminate?
3. Prove that at each loop iteration, if the item x is not found then the value of decreases by at least
a factor of 2. More precisely, let and be the values of immediately before and after the -t
iteration, respectively, and prove that .
Hints: do a proof by cases. Use the property that for all , .
4. Find an exact upper bound number of iterations that could occur (in terms of ), and use this to
show that the worst-case running time of binary_search is .
Part 3: Visualizing sorting algorithms
In the final part of today’s tutorial, you’ll develop a cool way of visualizing our two different sorting
algorithms using pygame.
3/25/2021 CSC111 Tutorial 9: Iterative Sorting Algorithms
https://www.teach.cs.toronto.edu/~csc111h/winter/tutorials/09-iterative-sorting/ 4/6
To visualize a sorting algorithm being performed on a list of length , the pygame window is divided
into an -by- grid, where:
Each row of the grid represents the state of the list at a given loop iteration in the sorting
algorithm.
Each row is divided into grid cells, where each cell represents one element of the list. For
now, we’ll only consider lists of numbers between 0–255, and visualize each number as th
grayscale colour .
For selection sort, the rows show the state of the list at the end of each of the loop iterations.
But for insertion sort, the rows show the state of the list at the start of each loop iteration.
To start, please download the starter file tutorial9_part3.py (starter-files/tutorial9_part3.py). We’v
included the sorting algorithms from lecture (the versions including the key parameter), and then the
tutorial exercises divided into “grayscale” and “colour” parts.
1. Under “Tutorial exercises (grayscale verions)”, implement the function draw_list, which is
responsible for drawing a single row of the grid. You can test your work in our starter
implementation of visualize_selection_sort, which draws just the top row of the grid. Try
creating a random list of 20 numbers between 0–255, and calling visualize_selection_sort o
this list. You should see an image similar to the one pictured above, except only the top row is
displayed (and the rest of the window is white).
2. Implement the visualization functions visualize_selection_sort and
visualize_insertion_sort. Your implementations can copy-and-paste from our algorithms, bu
should also call your draw_list helper in the appropriate place in the loop body. See
visualize_selection_sort’s docstring for some implementation notes that apply to both of
these functions.
After you’re done, try calling each function, choosing a random list of size 10, 100, and 200. You
should see each sorting algorithm visualized in a top-down manner. Make sure you can identify
which part of the visualization is the “sorted” vs. “unsorted” part of each list, and how the two
algorithms differ in what makes up the sorted part.
3. Now repeat Questions 1 and 2 for the “Tutorial exercises (colour version)” section. For those
functions, the input list being sorted is now a list of tuples storing the RGB values of colours.
Unfortunately, just sorting these RGB tuples directly isn’t very visually pleasing, and so we’ve
provided a helper function rgb_to_hsv that converts each tuple to a different colour
representation known as Hue-Saturation-Value (HSV).
So in the visualization functions, you’ll need to run the sorting algorithms on this list of tuples, bu
also use this helper function as the “sort key”, using the generalized sorting technique we
introduced in lecture.
To test your work, try generating some random lists of colours and running your visualization
algorithms on them. (Remember, each random colour should be a random uple with 3 elements i
the range 0–255.) You should see a beautiful rainbow of colours in the “sorted part” of eac
algorithm!
Additional exercises
1. Implement a variation of selection sort and insertion sort that take a list lst and two additional
index parameters b and e, and only sorts the sublist lst[b:e].
3/25/2021 CSC111 Tutorial 9: Iterative Sorting Algorithms
https://www.teach.cs.toronto.edu/~csc111h/winter/tutorials/09-iterative-sorting/ 6/6
2. Prove that the worst-case running time of binary_search is . Note that your description
of the input family should talk about both the input list, lst, and the item being searched for, x.
3. In Part 3, you might notice that there was quite a bit of code duplication in the visualization
functions you implemented. Reduce this code duplication by defining a single visualization
function that can visualize both sorting algorithms, and take in either a list of integers or a list of
colour tuples.
1. This tutorial exercises is inspired by https://corte.si/posts/code/visualisingsorting/
(https://corte.si/posts/code/visualisingsorting/).↩
2. You might recall grayscale colours from our first tutorial all the way back in CSC110!↩
3. The reason for this is a bit subtle. Normally, we would show both the initial state of the list and th
state of the list after each of the loop iterations. But this results in rows total, which break
the symmetry of our visualization. But for selection sort, the initial list state and the state after the
first loop iteration are identical, and so we can skip drawing the initial state. For insertion sort, th
states of the list at the beginning and end of the last iteration are equal, and so we can skip drawin
the very last state. ↩
Ω(log n)
n n + 1
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
urba6006代写、java/c++编程语...
2024-12-26
代做program、代写python编程语...
2024-12-26
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!