algorithms – bubble sort (1b)...2018/04/06 · algorithms – bubble sort (1b) 3 young won lim...
TRANSCRIPT
![Page 1: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/1.jpg)
Young Won Lim4/5/18
Algorithms – Bubble Sort (1B)
![Page 2: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/2.jpg)
Young Won Lim4/5/18
Copyright (c) 2017 – 2018 Young W. Lim.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".
Please send corrections (or suggestions) to [email protected].
This document was produced by using LibreOffice and Octave.
![Page 3: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/3.jpg)
Algorithms – Bubble Sort (1B) 3 Young Won Lim4/5/18
Bubble Sort Algorithm
procedure bubblesort(a1, … , a
n: real numbers with n ≥ 2)
for i := 1 to n-1
for j := 1 to n – i
if aj > a
j+1 then interchange a
j and a
j+1
{a1, …, a
n is in increasing order}
![Page 4: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/4.jpg)
Algorithms – Bubble Sort (1B) 4 Young Won Lim4/5/18
Nested loop iterations
i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
![Page 5: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/5.jpg)
Algorithms – Bubble Sort (1B) 5 Young Won Lim4/5/18
Input and Ouput
a1
a2
a3
a4
a5
a6
a7
a8
44
55
22
88
66
11
77
33
a1, … , a
n : real numbers
with n ≥ 2
11
22
33
44
55
66
77
88
a1
a2
a3
a4
a5
a6
a7
a8
{a1, …, a
n is in increasing order}
![Page 6: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/6.jpg)
Algorithms – Bubble Sort (1B) 6 Young Won Lim4/5/18
Step i=1
55
22
88
66
11
77
33
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
44
22
88
66
11
77
33
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
44
22
88
66
11
77
33
44
22
55
66
11
77
33
44
22
55
66
11
77
33
44
22
55
66
11
77
33
44
22
55
66
11
77
33
44
22
55
66
11
77
33
88
44
55
55
88
88
88
88
![Page 7: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/7.jpg)
Algorithms – Bubble Sort (1B) 7 Young Won Lim4/5/18
Step i=2
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
22
55
66
11
77
33
88
22
55
66
11
77
33
88
22
44
66
11
77
33
88
22
44
55
11
77
33
88
22
44
55
11
77
33
88
22
44
55
11
66
33
88
22
44
55
11
66
33
77
88
44
44
55
66
66
77
![Page 8: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/8.jpg)
Algorithms – Bubble Sort (1B) 8 Young Won Lim4/5/18
Step i=3
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
33
44
55
11
66
77
88
33
22
55
11
66
77
88
33
22
44
11
66
77
88
33
22
44
11
66
77
88
33
22
44
11
55
77
88
66
22
44
11
55
33
77
88
22
44
55
55
66
![Page 9: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/9.jpg)
Algorithms – Bubble Sort (1B) 9 Young Won Lim4/5/18
Step i=4
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
66
44
11
55
33
77
88
66
22
11
55
33
77
88
66
22
11
55
33
77
88
66
22
11
44
33
77
88
66
22
11
44
33
55
77
88
22
44
44
55
![Page 10: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/10.jpg)
Algorithms – Bubble Sort (1B) 10 Young Won Lim4/5/18
Step i=5
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
66
11
44
33
55
77
88
66
11
44
33
55
77
88
66
11
22
33
55
77
88
66
11
22
33
44
55
77
88
22
22
44
![Page 11: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/11.jpg)
Algorithms – Bubble Sort (1B) 11 Young Won Lim4/5/18
Step i=6
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
66
22
33
44
55
77
88
66
11
33
44
55
77
88
66
11
22
33
44
55
77
88
11
22
![Page 12: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/12.jpg)
Algorithms – Bubble Sort (1B) 12 Young Won Lim4/5/18
Step i=7
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
66
11
22
33
44
55
77
88
66
11
22
33
44
55
77
88
![Page 13: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/13.jpg)
Algorithms – Bubble Sort (1B) 13 Young Won Lim4/5/18
Summary
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
for i := 1 to n-1
for j := 1 to n – i
if aj> a
j+1 then interchange a
j and a
j+1
44
22
55
66
11
77
33
88
22
44
55
11
66
33
77
88
66
22
44
11
55
33
77
88
66
22
11
44
33
55
77
88
66
11
22
33
44
55
77
88
66
11
22
33
44
55
77
88
66
11
22
33
44
55
77
88
i=1 i=2 i=3 i=4 i=5 i=6 i=7
55
22
88
66
11
77
33
44
![Page 14: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/14.jpg)
Young Won Lim4/5/18
References
[1] http://en.wikipedia.org/[2]
![Page 15: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/15.jpg)
Young Won Lim4/5/18
Algorithms – Insertion Sort (1C)
![Page 16: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/16.jpg)
Young Won Lim4/5/18
Copyright (c) 2017 – 2018 Young W. Lim.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".
Please send corrections (or suggestions) to [email protected].
This document was produced by using LibreOffice and Octave.
![Page 17: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/17.jpg)
Algorithms – Insertion Sort (1C) 3 Young Won Lim4/5/18
Insertion Sort Algorithm
procedure insertion sort(a1, … , a
n: real numbers with n ≥ 2)
for j := 2 to n
i := 1
while aj > a
i
i := i + 1
m := aj
for k := 0 to j– i–1
aj-k
= aj-k-1
ai := m
{a1, …, a
nis in increasing order}
![Page 18: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/18.jpg)
Algorithms – Insertion Sort (1C) 4 Young Won Lim4/5/18
Nested loop k – constraints
for k := 0 to j– i–1
aj-k
= aj-k-1
a j−k = a j−k−1
a j−0= a j−0−1
a j−1= a j−1−1
a j−2= a j−2−1
a j−( j−i−1) = a j−( j−i−1)−1
⋮ = ⋮
a j = a j−1
a j−1= a j−2
a j−2= a j−3
ai+1= ai
⋮ = ⋮
j−i−1≥0 j≥i+1 i≤ j−1 i< j
(k=0)
(k=1)
(k=2)
(k= j−i−1)
![Page 19: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/19.jpg)
![Page 20: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/20.jpg)
Algorithms – Insertion Sort (1C) 5 Young Won Lim4/5/18
Nested loop k – rearranging
for k := 0 to j– i–1
aj-k
= aj-k-1
a j−k = a j−k−1
a j = a j−1
a j−1= a j−2
a j−2= a j−3
ai+1= ai
⋮ = ⋮
j−i−1≥0 j≥i+1 i≤ j−1 i< j
ai+1= ai
⋮ = ⋮a j−2
= a j−3
a j−1= a j−2
a j = a j−1
![Page 21: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/21.jpg)
Algorithms – Insertion Sort (1C) 6 Young Won Lim4/5/18
Nested loop k – data movement
m := aj
for k := 0 to j– i–1
aj-k
= aj-k-1
ai := m
i< j
ai
⋮a j−3
a j−2
a j−1
ai+1
⋮a j−2
a j−1
a ja j
ai
![Page 22: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/22.jpg)
Algorithms – Insertion Sort (1C) 7 Young Won Lim4/5/18
Nested loop i – finding out of order ai
i := 1
while aj > a
i
i := i + 1
If ai < a
j increment I
If ai >= a
j break the loop
aiis the 1st one that is greater than a
j
ai
⋮a j−3
a j−2
a j−1
ai+1
⋮a j−2
a j−1
a ja j
ai
a1
a1
a2
a2
![Page 23: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/23.jpg)
Algorithms – Insertion Sort (1C) 8 Young Won Lim4/5/18
Nested loop i – inserting ai at the correct position
ai
⋮a j−3
a j−2
a j−1
ai+1
⋮a j−2
a j−1
a ja j
ai
a1
a1
a2
a2
ai
⋮a j−3
a j−2
a j−1
a j
a1
a2
< a j
< a j
< a j
≥ a j
< a j
⋮⋮
![Page 24: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/24.jpg)
Algorithms – Insertion Sort (1C) 9 Young Won Lim4/5/18
Nested loop iterations
j=2 j=3 j=4 j=5 j=6 j=7 j=8
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
for j := 2 to n
i := 1
while aj > a
i
i := i + 1
m := aj
for k := 0 to j– i–1
aj-k
= aj-k-1
ai := m
a2
a3
a4
a5
a6
a7
a8
![Page 25: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/25.jpg)
Algorithms – Insertion Sort (1C) 10 Young Won Lim4/5/18
Step j=2
55
22
88
66
11
77
33
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
44
55
22
88
66
11
77
33
44
![Page 26: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/26.jpg)
Algorithms – Insertion Sort (1C) 11 Young Won Lim4/5/18
Step j=3
55
22
88
66
11
77
33
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
44
44
55
88
66
11
77
33
22
![Page 27: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/27.jpg)
![Page 28: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/28.jpg)
Algorithms – Insertion Sort (1C) 12 Young Won Lim4/5/18
Step j=4
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
44
55
88
66
11
77
33
22
44
55
88
66
11
77
33
22
![Page 29: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/29.jpg)
Algorithms – Insertion Sort (1C) 13 Young Won Lim4/5/18
Step j=5
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
44
55
88
66
11
77
33
22
44
55
66
88
11
77
33
22
![Page 30: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/30.jpg)
Algorithms – Insertion Sort (1C) 14 Young Won Lim4/5/18
Step j=6
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
44
55
66
88
11
77
33
22
22
44
55
66
88
77
33
11
![Page 31: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/31.jpg)
Algorithms – Insertion Sort (1C) 15 Young Won Lim4/5/18
Step j=7
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
22
44
55
66
88
77
33
11
22
44
55
66
77
88
33
11
![Page 32: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/32.jpg)
Algorithms – Insertion Sort (1C) 16 Young Won Lim4/5/18
Step j=8
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
22
44
55
66
77
88
33
11
22
33
44
55
66
77
88
11
![Page 33: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/33.jpg)
Algorithms – Insertion Sort (1C) 17 Young Won Lim4/5/18
Nested loop iterations
![Page 34: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/34.jpg)
Young Won Lim4/5/18
References
[1] http://en.wikipedia.org/[2]
![Page 35: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/35.jpg)
Copyright (c) 2015 - 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of theGNU Free Documentation License, Version 1.2 or any later version published by the Free SoftwareFoundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy ofthe license is included in the section entitled "GNU Free Documentation License".
Bubble Sort
used some pictures and codes from http://people.cs.vt.edu/shaffer/Book/C++3elatest.pdfData Structures and Algorithm Analysisby Clifford A. Schaffer
20170411
![Page 36: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/36.jpg)
![Page 37: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/37.jpg)
![Page 38: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/38.jpg)
![Page 39: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/39.jpg)
![Page 40: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/40.jpg)
![Page 41: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/41.jpg)
![Page 42: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/42.jpg)
![Page 43: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/43.jpg)
![Page 44: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/44.jpg)
![Page 45: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/45.jpg)
#include <stdio.h>
void bubbleSort(int a[], int size) { int p, j, tmp;
for (p=1; p< size; ++p) { for (j=0; j< size-1; ++j) if ( a[j] > a[j+1] ) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } }}
int main(void) { int i; int a[] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
bubbleSort(a, 10);
for (i=0; i<10; ++i) printf("a[%d]=%d \n", i, a[i]);
} a[0]=2 a[1]=4 a[2]=6 a[3]=8 a[4]=10 a[5]=12 a[6]=37 a[7]=45 a[8]=68 a[9]=89
a[0]=89 a[1]=68 a[2]=45 a[3]=37 a[4]=12 a[5]=10 a[6]=8 a[7]=6 a[8]=4 a[9]=2
![Page 46: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/46.jpg)
void bubbleSort(int a[], int size) { int p, j, tmp;
for (p=1; p< size; ++p) { for (j=0; j< size-1; ++j) if ( a[j] > a[j+1] ) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } }}
![Page 47: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/47.jpg)
![Page 48: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/48.jpg)
Copyright (c) 2015 - 2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of theGNU Free Documentation License, Version 1.2 or any later version published by the Free SoftwareFoundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy ofthe license is included in the section entitled "GNU Free Documentation License".
Insertion Sort
used some pictures and codes from http://people.cs.vt.edu/shaffer/Book/C++3elatest.pdfData Structures and Algorithm Analysisby Clifford A. Schaffer
20170411
![Page 49: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/49.jpg)
![Page 50: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/50.jpg)
![Page 51: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/51.jpg)
![Page 52: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/52.jpg)
![Page 53: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/53.jpg)
![Page 54: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/54.jpg)
template <class T> T square(T x) { return (x*x);}
template <typename T> T square(T x) { return (x*x);}
square<float>(f);
int square(float x) { return (x*x);}
square<int>(i); int square(int x) { return (x*x);}
square<double>(d); double square(double x) { return (x*x);}
![Page 55: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/55.jpg)
![Page 56: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/56.jpg)
![Page 57: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/57.jpg)
![Page 58: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/58.jpg)
![Page 59: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/59.jpg)
![Page 60: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/60.jpg)
![Page 61: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/61.jpg)
![Page 62: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/62.jpg)
Copyright (c) 2015 - 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of theGNU Free Documentation License, Version 1.2 or any later version published by the Free SoftwareFoundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy ofthe license is included in the section entitled "GNU Free Documentation License".
Binary Search
used some pictures and codes from http://people.cs.vt.edu/shaffer/Book/C++3elatest.pdfData Structures and Algorithm Analysisby Clifford A. Schaffer
20161104
![Page 63: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/63.jpg)
![Page 64: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/64.jpg)
![Page 65: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/65.jpg)
![Page 66: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/66.jpg)
![Page 67: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/67.jpg)
![Page 68: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/68.jpg)
int binary (int A[], int n, int K) {int l = 0;int r = n-1;int i ;while ( l <= r) {
i = (l+r)/2;if (K < A[i]) r = i-1;if (K == A[i]) return i;if (K > A[i]) l = i+1;
}return n;
}
![Page 69: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/69.jpg)
![Page 70: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/70.jpg)
![Page 71: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/71.jpg)
#include <stdio.h>
void bubbleSort(int a[], int size) { int p, j, tmp;
for (p=1; p< size; ++p) { for (j=0; j< size-1; ++j) if ( a[j] > a[j+1] ) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } }}
int main(void) { int i; int a[] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
bubbleSort(a, 10);
for (i=0; i<10; ++i) printf("a[%d]=%d \n", i, a[i]);
} a[0]=2 a[1]=4 a[2]=6 a[3]=8 a[4]=10 a[5]=12 a[6]=37 a[7]=45 a[8]=68 a[9]=89
a[0]=89 a[1]=68 a[2]=45 a[3]=37 a[4]=12 a[5]=10 a[6]=8 a[7]=6 a[8]=4 a[9]=2
![Page 72: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/72.jpg)
void bubbleSort(int a[], int size) { int p, j, tmp;
for (p=1; p< size; ++p) { for (j=0; j< size-1; ++j) if ( a[j] > a[j+1] ) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } }}
![Page 73: Algorithms – Bubble Sort (1B)...2018/04/06 · Algorithms – Bubble Sort (1B) 3 Young Won Lim 4/5/18 Bubble Sort Algorithm procedure bubblesort(a 1, … , a n: real numbers with](https://reader034.vdocuments.site/reader034/viewer/2022050406/5f83294019837b49295a0768/html5/thumbnails/73.jpg)