Download presentation
Presentation is loading. Please wait.
Published byMyrtle Alexander Modified over 5 years ago
1
Sorting: part 1 Barak Obama on sorting, bubble sort, insertion sort,
selection sort, merge, merge sort, ... demos, demos, demos …
2
Sorting, what’s that then?
Given a collection of data, get it into order …
3
Given a collection of data, get it into order …
This is NOT in order
4
Given a collection of data, get it into order …
This is IS in order
5
Given a collection of data, get it into order …
This is IS in order It’s in ascending order
6
Sorting is the process of going from this
to that
7
Who cares? Sorting, why bother? You can find things quicker!
8
You can find things quicker! Use “binary search” (or even linear search and stop early)
9
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort
10
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort quadratic
11
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort logarithmic
12
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort linear
13
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort Our example
14
Example sorting 9 2 6 4 3 2 7 5 Bubble Sort
15
Example sorting 9 2 6 4 3 2 7 5 compare Bubble Sort
16
Example sorting 2 9 6 4 3 2 7 5 swap Bubble Sort
17
Example sorting 2 9 6 4 3 2 7 5 compare Bubble Sort
18
Example sorting 2 6 9 4 3 2 7 5 swap Bubble Sort
19
Example sorting 2 6 9 4 3 2 7 5 compare Bubble Sort
20
Example sorting 2 6 9 4 3 2 7 5 swap Bubble Sort
21
Example sorting 2 6 9 4 3 2 7 5 compare Bubble Sort
22
Example sorting 2 6 4 9 3 2 7 5 swap Bubble Sort
23
Example sorting 2 6 4 9 3 2 7 5 compare Bubble Sort
24
Example sorting 2 6 4 3 9 2 7 5 swap Bubble Sort
25
Example sorting 2 6 4 3 9 2 7 5 compare Bubble Sort
26
Example sorting 2 6 4 3 2 9 7 5 swap Bubble Sort
27
Example sorting 2 6 4 3 2 9 7 5 compare Bubble Sort
28
Example sorting 2 6 4 3 2 7 9 5 swap Bubble Sort
29
Example sorting 2 6 4 3 2 7 9 5 compare Bubble Sort
30
Example sorting 2 6 4 3 2 7 5 9 swap Bubble Sort
31
Example sorting 2 6 4 3 2 7 5 9 Bubble Sort
32
Example sorting 2 6 4 3 2 7 5 9 compare Bubble Sort
33
Example sorting 2 6 4 3 2 7 5 9 compare Bubble Sort
34
Example sorting 2 6 4 3 2 7 5 9 swap Bubble Sort
35
Example sorting 2 6 4 3 2 7 5 9 compare Bubble Sort
36
Example sorting 2 4 6 3 2 7 5 9 swap Bubble Sort
37
Example sorting 2 4 6 3 2 7 5 9 compare Bubble Sort
38
Example sorting 2 4 3 6 2 7 5 9 swap Bubble Sort
39
Example sorting 2 4 3 6 2 7 5 9 compare Bubble Sort
40
Example sorting 2 4 3 2 6 7 5 9 swap Bubble Sort
41
Example sorting 2 4 3 2 6 7 5 9 compare Bubble Sort
42
Example sorting 2 4 3 2 6 7 5 9 compare Bubble Sort
43
Example sorting 2 4 3 2 6 5 7 9 swap Bubble Sort
44
Example sorting 2 4 3 2 6 5 7 9 Bubble Sort
45
Example sorting 2 4 3 2 6 5 7 9 Are you tired yet? Bored? Bubble Sort
46
Example sorting 2 4 3 2 6 5 7 9 compare Bubble Sort
47
Example sorting 2 4 3 2 6 5 7 9 swap Bubble Sort
48
Example sorting 2 4 3 2 6 5 7 9 compare Bubble Sort
49
Example sorting 2 4 3 2 6 5 7 9 compare Bubble Sort
50
Example sorting 2 3 4 2 6 5 7 9 swap Bubble Sort
51
Example sorting 2 3 4 2 6 5 7 9 compare Bubble Sort
52
Example sorting 2 3 2 4 6 5 7 9 swap Bubble Sort
53
Example sorting 2 3 2 4 6 5 7 9 compare Bubble Sort
54
Example sorting 2 3 2 4 6 5 7 9 compare Bubble Sort
55
Example sorting 2 3 2 4 5 6 7 9 swap Bubble Sort
56
Example sorting 2 3 2 4 5 6 7 9 Bubble Sort
57
Example sorting 2 3 2 4 5 6 7 9 compare Bubble Sort
58
Example sorting 2 3 2 4 5 6 7 9 compare Bubble Sort
59
Example sorting 2 3 2 4 5 6 7 9 compare Bubble Sort
60
Example sorting 2 2 3 4 5 6 7 9 swap Bubble Sort
61
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
62
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
63
Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
64
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
65
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
66
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
67
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
68
Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
69
Example sorting 2 2 3 4 5 6 7 9 Phew! Bubble Sort
70
On an Array sorting Bubble Sort
71
Example sorting i=8 9 2 6 4 3 2 7 5 Bubble Sort
72
Example sorting i=8 j=0 9 2 6 4 3 2 7 5 compare Bubble Sort
73
Example Example sorting i=8 j=0 2 9 6 4 3 2 7 5 swap Bubble Sort
74
Example Example Example sorting i=8 j=1 2 9 6 4 3 2 7 5 compare Bubble Sort
75
Example Example Example Example sorting i=8 j=1 2 6 9 4 3 2 7 5 swap Bubble Sort
76
Example Example Example Example Example sorting i=8 j=2 2 6 9 4 3 2 7 5 compare Bubble Sort
77
Example Example Example Example Example Example sorting i=8 j=2 2 6 9 4 3 2 7 5 swap Bubble Sort
78
Example Example Example Example Example Example Example sorting i=8 j=3 2 6 9 4 3 2 7 5 compare Bubble Sort
79
Example Example Example Example Example Example Example Example sorting i=8 j=3 2 6 4 9 3 2 7 5 swap Bubble Sort
80
Example Example Example Example Example Example Example Example Example sorting i=8 j=4 2 6 4 9 3 2 7 5 compare Bubble Sort
81
Example Example Example Example Example Example Example Example Example Example sorting i=8 j=4 2 6 4 3 9 2 7 5 swap Bubble Sort
82
Example Example Example Example Example Example Example Example Example Example Example sorting i=8 j=5 2 6 4 3 9 2 7 5 compare Bubble Sort
83
Example Example Example Example Example Example Example Example Example Example Example Example sorting i=8 j=5 2 6 4 3 2 9 7 5 swap Bubble Sort
84
Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=6 i=8 2 6 4 3 2 9 7 5 compare Bubble Sort
85
Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=6 i=8 2 6 4 3 2 7 9 5 swap Bubble Sort
86
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=7 i=8 2 6 4 3 2 7 9 5 compare Bubble Sort
87
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=7 i=8 2 6 4 3 2 7 5 9 swap Bubble Sort
88
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting 2 6 4 3 2 7 5 9 Bubble Sort
89
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=0 2 6 4 3 2 7 5 9 compare Bubble Sort
90
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting i=7 j=1 2 6 4 3 2 7 5 9 compare Bubble Sort
91
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting i=7 j=1 2 6 4 3 2 7 5 9 swap Bubble Sort
92
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting i=7 j=2 2 6 4 3 2 7 5 9 compare Bubble Sort
93
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=2 2 4 6 3 2 7 5 9 swap Bubble Sort
94
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=3 2 4 6 3 2 7 5 9 compare Bubble Sort
95
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=3 2 4 3 6 2 7 5 9 swap Bubble Sort
96
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=4 2 4 3 6 2 7 5 9 compare Bubble Sort
97
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=4 2 4 3 2 6 7 5 9 swap Bubble Sort
98
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=5 i=7 2 4 3 2 6 7 5 9 compare Bubble Sort
99
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=6 i=7 2 4 3 2 6 7 5 9 compare Bubble Sort
100
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=6 i=7 2 4 3 2 6 5 7 9 swap Bubble Sort
101
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 4 3 2 6 5 7 9 Bubble Sort
102
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=0 i=6 2 4 3 2 6 5 7 9 compare Bubble Sort
103
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=0 i=6 2 4 3 2 6 5 7 9 swap Bubble Sort
104
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=1 i=6 2 4 3 2 6 5 7 9 compare Bubble Sort
105
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=6 2 4 3 2 6 5 7 9 compare Bubble Sort
106
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=6 2 3 4 2 6 5 7 9 swap Bubble Sort
107
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=6 2 3 4 2 6 5 7 9 compare Bubble Sort
108
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=6 2 3 2 4 6 5 7 9 swap Bubble Sort
109
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=4 i=6 2 3 2 4 6 5 7 9 compare Bubble Sort
110
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=5 i=6 2 3 2 4 6 5 7 9 compare Bubble Sort
111
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=5 i=6 2 3 2 4 5 6 7 9 swap Bubble Sort
112
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 3 2 4 5 6 7 9 Bubble Sort
113
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=0 i=5 2 3 2 4 5 6 7 9 compare Bubble Sort
114
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=1 i=5 2 3 2 4 5 6 7 9 compare Bubble Sort
115
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=5 2 3 2 4 5 6 7 9 compare Bubble Sort
116
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=5 2 2 3 4 5 6 7 9 swap Bubble Sort
117
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=5 2 2 3 4 5 6 7 9 compare Bubble Sort
118
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=4 i=5 2 2 3 4 5 6 7 9 compare Bubble Sort
119
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
120
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=0 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
121
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=1 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
122
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
123
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
124
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
125
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
126
Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
127
Example sorting 2 2 3 4 5 6 7 9 Phew! Bubble Sort
128
On an Array sorting Can we visualize this? Bubble Sort
129
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last Bubble Sort
130
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 2 7 5 Bubble Sort
131
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] Bubble Sort
132
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] We could plot A[i] on a graph as a point with x/y coordinates i/A[i] Bubble Sort
133
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] We could plot A[i] on a graph as a point with x/y coordinates i/A[i] Do this as the algorithm progress to see how it progresses. Bubble Sort
134
Ready? On an Array sorting Can we visualize this?
Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] We could plot A[i] on a graph as a point with x/y coordinates i/A[i] Do this as the algorithm progress to see how it progresses. Bubble Sort
135
On an Array sorting Bubble Sort
136
sorting Show me more! Bubble Sort
137
sorting Show me more! different sorting algorithms and relative performance a beauty contest what is the “sound” of sorting? can you show me it by mime? Expressive dance? Bubble Sort
138
sorting NOTE to self: Bubble sort is a quadratic time algorithm. If I have to sort a data set of N objects the run time will be proportional to N2 To sort 10 numbers will take 100 units of time To sort 100 numbers will take 10,000 units of time Bubble Sort
139
Barak on Sorting
140
Links sorting algorithm animation sound of sorting (part 1) sound of sorting (part 2) bubble sort by folk dancing
143
sorting O(n^2) algorithms bubble sort insertion sort selection sort
144
On an Array sorting Bubble Sort O(n^2) algorithms bubble sort
insertion sort selection sort On an Array sorting Bubble Sort
145
On an Array sorting Selection Sort O(n^2) algorithms bubble sort
insertion sort selection sort On an Array sorting Selection Sort
146
On a linked list sorting insertion sort O(n^2) algorithms bubble sort
selection sort On a linked list sorting insertion sort
147
merge
148
merge sorting You have already done this! (QueueSort) Demo with cards?
149
merge sorting On linked lists
150
where l1 is of size n1 and l2 is of size n2
merge sorting On linked lists O(n1 + n2) where l1 is of size n1 and l2 is of size n2
151
sorting merge sort
152
merge sort sorting msort(L) if (size(L) <= 1) return L return merge(msort(leftHalf(L)),msort(rightHalf(L)))
153
merge sort sorting msort(L) if (size(L) <= 1) return L return merge(msort(leftHalf(L)),msort(rightHalf(L))) Divide and Conquer divide: divide input data into smaller parts recur: solve the small parts conquer: combine the subproblems once solved (merge)
155
merge sort sorting
156
merge sort sorting
157
merge sort sorting
158
merge sort sorting
159
merge sort sorting Stopping condition
160
merge sort sorting Divide list into Left and Right parts
161
merge sort sorting recurse & merge
162
a way of “thinking” about merge sort
sorting merge sort tree a way of “thinking” about merge sort
163
merge sort sorting
164
merge sort sorting
165
merge sort sorting
166
merge sort sorting
167
merge sort sorting
168
merge sort sorting
169
merge sort sorting
170
merge sort sorting
171
merge sort sorting
172
merge sort sorting
173
merge sort sorting
174
merge sort sorting
175
merge sort sorting
176
merge sort sorting
177
merge sort sorting
178
merge sort sorting
179
merge sort sorting
180
merge sort sorting
181
merge sort sorting
182
merge sort sorting abbreviated
183
merge sort sorting
184
merge sort sorting
185
merge sort sorting
186
merge sort sorting
187
merge sort sorting n
188
merge sort sorting n n/2 n/2
189
merge sort sorting n 2.T(n/2) n/2 n/2
190
merge sort sorting n 2.T(n/2) n/2 n/2 T(n) is time to cut the data into 2 parts make 2 recursive calls to msort merge the 2 msorted parts and this is linear in n
191
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4
192
merge sort sorting n n/2 n/2 4.T(n/4) n/4 n/4 n/4 n/4
193
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8
194
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8
195
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 This goes on h times where h = log(n) … base 2
196
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 This goes on h times where h = log(n) … base 2 Therefore O(n) at each level
197
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 This goes on h times where h = log(n) … base 2 Therefore O(n) at each level Therefore O(n.log(n))
198
fin
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.