Bubblesort and permutations. Mike Atkinson. Michael Albert, Mathilde Bouvel,
Anders Claesson, Mark Dukes. New Zealand Mathematical Society Colloquium.
Bubble sort (1). • An easy to understand and implement algorithm. • Keep
passing through the input and exchange adjacent elements which are unsorted.
[Fine, Wilf, 1965] see Algebraic Combinatorics on Words [Lothaire, 2002] ...... 310
pages. [3] See also http://monge.univ-mlv.fr/ mac/REC/text-algorithms.pdf.
Buckyball from 120 of. Tom Hull's PHiZZ unit. Fullerene from 90 of. Robert
Neale's penultimate unit. Large icosahedron from. 270 Sonobe units.
Jeet Kune Do encourages development of a uniquely ... In comparison with JKD,
patterned styles of martial ... To get from A to B, a change agency*** will guide a.
NODC CICS-UMD Team. Gregg Foti. Yongsheng ... Gregg Foti. Stewardship-
CICS Activities. 6 .... free online oceanographic courses, etc. − To make all CICS
...
Patrick Fricker. Example: Feature Tree Imposed. Mutschler, E., G. Geisslinger,
H.K. Kroemer, and M. Schäfer-. Korting, Mutschler Arzneimittelwirkungen. 8 ed.
8 Aug 2013 ... new york times | square dance scottish country | dancing clubs | melbourne tony
hawk american wasteland | ps2 | cheats what causes | swollen ...
Other “programming-centric” courses, but not at the. AP CS level ... “Python
programming for the absolute beginner,” by Dawson, 2010. ○ “Introduction to ...
from this point of view, is the DCT. – in this example we see the amplitude spectra
of the image above. – under the DFT and DCT. – note the much more.
Theorem. The modal logic KD4+KD4 is sound and complete w.r.t. the bi-
topological rational plane QxQ with the horizontal and vertical topologies.
Algorithms and Computation in. Signal Processing special topic course 18-799B
spring 2005. 9th Lecture Feb. 8, 2005. Instructor: Markus Pueschel. TA: Srinivas ...
Stephan Dürr (Jülich, BMW). Andreas Jüttner (Mainz, RBC/UKQCD). Laurent
Lellouch (Marseille, BMW). Heiri Leutwyler (Bern). Vittorio Lubicz (Rome 3, ETM).
Motivation and Basic. Tenets. ⢠Consolidation. â Large-scale, holistic. ⢠Representative Eco-System ... Tracker and ticketing system (FI-WARE FusionForge).
of Gologras and Gawain, Territories in The Pendragon Adventure, A Storm of
Swords, Connor MacLeod, Paul Atreides her manner ___, being ___ by their,
___ ...
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. 1.
Overview of Storage and Indexing. Chapter 8. “How index-learning turns no
student ...
Local 0.234 story for simulated tempering. 5. Conclusions ... Given Xn, generate
Yn+1 from q(Xn,·). Now set Xn+1 = Yn+1 with probability α(Xn,Yn+1)=1 ∧.
Feb 22, 2012 ... fixed expressions that often have to be spoken. ▻ Salutation to Kubera reciting
the mantra arddha-mãsãh. Nikolina Koleva (UdS). 22. February ...
int var1 = 5; //declares an integer with value 5 var1++;. //increments var1 printf(â%dâ, var1); //prints out 6. Page
Point cloud is inadequate for AR. â User interaction? Reitmayr et al ISMAR 2007. Checklov et al ISMAR 2007. â Automa
Philosophical Logic. LECTURE ONE | MICHAELMAS 2017. Dr Maarten Steenhagen [email protected]. Page 2. (As always, Google w
Desktop Linux, OS X & Windows. • China overwhelmingly XP but ... Steam
Graphics Hardware. Direct3D. OpenGL. Steam Hardware Survey, Dec 2013 ...
Oct 27, 2011 ... [Winoto & Tang, 2008] Winoto, P., Tang, T. 2008. If You Like the Devil Wears
Prada the Book, Will You also Enjoy the Devil Wears. Prada the ...
Sorting. • Lots of algorithms for sorting. – Selection sort (video lecture and
textbook). – Insertion sort (textbook). – Bubble sort. – Merge sort. – Radix sort. – .
Bubble Sort
With thanks to Bill Leahy, GaTech for original slide deck
Sorting • Sorting takes an unordered collection and makes it an ordered one. 0
1
2
3
4
5
77
42
35
12
101
5
0
1
2
3
4
5
5
12
35
42
77
101
Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge sort – Radix sort – ...
"Bubbling Up" the Largest Element • Traverse a collection of elements (array) – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0
1
2
3
4
5
77
42
35
12
101
5
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0
1
Swap77 42 42 77
2
3
4
5
35
12
101
5
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0 42
1
2
35Swap35 77 77
3
4
5
12
101
5
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0
1
42
35
2
3
12Swap12 77 77
4
5
101
5
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0
1
2
3
4
5
42
35
12
77
101
5
No need to swap
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0
1
2
3
42
35
12
77
4
5
5 Swap101 101 5
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 0
1
2
3
4
5
42
35
12
77
5
101
Largest value correctly placed
The “Bubble Up” Algorithm Given some array, a for(int i = 0; i < a.length-1; i++) { if(a[i] > a[i+1]) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } }
Items of Interest • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 0
1
2
3
4
5
42
35
12
77
5
101
Largest value correctly placed
Repeat “Bubble Up” How Many Times? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements.
N-1
“Bubbling” All the Elements 0 42
1
2 12
3 77
4 5
5 101
35
0 35
1 12
2 42
3 5
4 77
5 101
0 12
2 5 2 35
3 42
4 77
5 101
0 12
1 35 1 5
3 42
4 77
5 101
0 5
1 12
2 35
3 42
4 77
5 101
Reducing the Number of Comparisons 0 77 0 42
1 42 1 35
2 35 2 12
3 12 3 77
4 101 4 5
5 5 5 101
0 35
1 12
2 42
3 5
4 77
5 101
0 12
1 35 1 5
2 5 2 35
3 42
4 77
5 101
3 42
4 77
5 101
0 12
Reducing the Number of Comparisons • On the Nth “bubble up”, we only need to do MAX-N comparisons. • For example: – This is the 4th “bubble up” – MAX is 6 (total number of elements in array) – Thus we have 2 comparisons to do 0 12
1 35
2 5
3 42
4 77
5 101
Putting It All Together
to_do = to_do - 1 } //end method
Outer loop
while (to_do != 0) { for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } }
Inner loop
public static void Bubblesort(int[] a){ int to_do = a.length-1;
Already Sorted Collections? • What if the collection was already sorted? • What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? • We want to be able to detect this and “stop early”! 0 5
1 12
2 35
3 42
4 77
5 101
Using a Boolean “Flag” • We can use a boolean variable to determine if any swapping occurred during the “bubble up.” • If no swapping occurred, then we know that the collection is already sorted! • This boolean “flag” needs to be reset after each “bubble up.”
public static void Bubblesort(int[] a){ int to_do = a.length-1; boolean did_swap = true; while (to_do != 0 && did_swap) { did_swap = false; for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { did_swap = true; int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } to_do = to_do - 1 } //end method
An Animated Example did_swap false to_do 7 i
0
98 23 45 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap false to_do 7 i
0 Swap 98 23 45 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 1 Swap 23 98 45 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 1
23 98 45 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 1 Swap 23 98 45 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 1 Swap 23 45 98 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 2
23 45 98 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 2 Swap 23 45 98 14 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 2 Swap 23 45 14 98 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 3
23 45 14 98 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 3 Swap 23 45 14 98 0
1
2
3
6 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 3 Swap 23 45 14 6 0
1
2
3
98 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 4
23 45 14 6 0
1
2
3
98 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 4 Swap 23 45 14 6 0
1
2
3
98 67 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 4 Swap 23 45 14 6 0
1
2
3
67 98 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 5
23 45 14 6 0
1
2
3
67 98 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 5 Swap 23 45 14 6 0
1
2
3
67 98 33 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 5 Swap 23 45 14 6 0
1
2
3
67 33 98 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 6
23 45 14 6 0
1
2
3
67 33 98 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 6 Swap 23 45 14 6 0
1
2
3
67 33 98 42 4
5
6
7
An Animated Example did_swap to_do i
true 7 6 Swap 23 45 14 6 0
1
2
3
67 33 42 98 4
5
6
7
After First Pass of Outer Loop did_swap to_do i
true 7 Finished first “Bubble Up”
7
23 45 14 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap false to_do 6 i
0
23 45 14 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap false to_do 6 i
0 No Swap 23 45 14 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap false to_do 6 i
1
23 45 14 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap false to_do 6 i
1 Swap 23 45 14 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 1 Swap 23 14 45 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 2
23 14 45 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 2 Swap 23 14 45 6 0
1
2
3
67 33 42 98 4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 2 Swap 23 14 6 0
1
2
45 67 33 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 3
23 14 6 0
1
2
45 67 33 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 3 No Swap 23 14 6 0
1
2
45 67 33 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 4
23 14 6 0
1
2
45 67 33 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 4 Swap 23 14 6 0
1
2
45 67 33 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 4 Swap 23 14 6 0
1
2
45 33 67 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 5
23 14 6 0
1
2
45 33 67 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 5 Swap 23 14 6 0
1
2
45 33 67 42 98 3
4
5
6
7
The Second “Bubble Up” did_swap to_do i
true 6 5 Swap 23 14 6 0
1
2
45 33 42 67 98 3
4
5
6
7
After Second Pass of Outer Loop did_swap to_do i
true 6 Finished second “Bubble Up”
6
23 14 6 0
1
2
45 33 42 67 98 3
4
5
6
7
The Third “Bubble Up” did_swap false to_do 5 i
0
23 14 6 0
1
2
45 33 42 67 98 3
4
5
6
7
The Third “Bubble Up” did_swap false to_do 5 i
0 Swap 23 14 6 0
1
2
45 33 42 67 98 3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 0 Swap 14 23 6 0
1
2
45 33 42 67 98 3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 1
14 23 6 0
1
2
45 33 42 67 98 3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 1 Swap 14 23 6 0
1
2
45 33 42 67 98 3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 1 Swap 14 6 0
1
23 45 33 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 2
14 6 0
1
23 45 33 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 2 No Swap 14 6 0
1
23 45 33 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 3
14 6 0
1
23 45 33 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 3 Swap 14 6 0
1
23 45 33 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 3 Swap 14 6 0
1
23 33 45 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 4
14 6 0
1
23 33 45 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 4 Swap 14 6 0
1
23 33 45 42 67 98 2
3
4
5
6
7
The Third “Bubble Up” did_swap to_do i
true 5 4 Swap 14 6 0
1
23 33 42 45 67 98 2
3
4
5
6
7
After Third Pass of Outer Loop did_swap to_do i
true 5 Finished third “Bubble Up”
5
14 6 0
1
23 33 42 45 67 98 2
3
4
5
6
7
The Fourth “Bubble Up” did_swap false to_do 4 i
0
14 6 0
1
23 33 42 45 67 98 2
3
4
5
6
7
The Fourth “Bubble Up” did_swap false to_do 4 i
0 Swap 14 6 0
1
23 33 42 45 67 98 2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 0 Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 1
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 1 No Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 2
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 2 No Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 3
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fourth “Bubble Up” did_swap to_do i
true 4 3 No Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
After Fourth Pass of Outer Loop did_swap to_do i
true 4 Finished fourth “Bubble Up”
4
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fifth “Bubble Up” did_swap false to_do 3 i
0
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fifth “Bubble Up” did_swap false to_do 3 i
0 No Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fifth “Bubble Up” did_swap false to_do 3 i
1
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fifth “Bubble Up” did_swap false to_do 3 i
1 No Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fifth “Bubble Up” did_swap false to_do 3 i
2
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
The Fifth “Bubble Up” did_swap false to_do 3 i
2 No Swap 6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
After Fifth Pass of Outer Loop did_swap false to_do 3 i
Finished fifth “Bubble Up”
3
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
Finished “Early” did_swap false to_do 3 i
We didn’t do any swapping, so all of the other elements must be correctly placed.
3
We can “skip” the last two passes of the outer loop.
6 0
14 23 33 42 45 67 98 1
2
3
4
5
6
7
Summary • “Bubble Up” algorithm will move largest value to its correct location (to the right/end of array) • Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs • We reduce the number of elements we compare each time one is correctly placed
LB
Truth in CS Act • NOBODY EVER USES BUBBLE SORT • NOBODY • NOT EVER • BECAUSE IT IS EXTREMELY INEFFICIENT