sorting: bubble sort -...

14
Sorting: Bubble Sort Queens College, CUNY CS90: Computational Thinking In High Schools Instructor: Kent Chin

Upload: doanphuc

Post on 12-Mar-2018

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Sorting: Bubble SortQueens College, CUNY

CS90: Computational Thinking In High Schools

Instructor: Kent Chin

Page 2: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

SortingGiven an unsorted array…

5 3 8 1 6

…we can easily sort it by eye!

1 3 5 6 8

To have our computers do this, we must implement some sorting algorithm!

Page 3: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Bubble Sort Idea

1) Start looking at the first pair of numbers in the array.

2) If the left number is larger than the right number, swap them.

3) Move on to the next pair of numbers.

4) Repeat Steps 2 – 3 until there are no more numbers to examine.

5 3 8 1 6

Assuming we want ascending (smallest-to-largest) order…

5 > 3 ? Yes, swap! 3 5 8 1 6

5 > 8 ? No, don’t swap! 3 5 8 1 6

8 > 1 ? Yes, swap!3 5 8 1 6 3 5 1 8 6

8 > 6 ? Yes, swap!3 5 1 8 6 3 5 1 6 8

Page 4: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Observation

The array above isn’t sorted, but notice that the biggest number in the array is now at the end – we say that the biggest number bubbles up!

3 5 1 6 8

3 5 1 6 8

We now re-run the algorithm on the previous side on this portion since the biggest number is where it needs to be!

Bubble sort gets its name from the fact that numbers bubble up (or down when dealing with descending order)!

Page 5: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Bubble Sort Algorithm (Ascending)

1) Set stopper to length of the array

2) Set i to 1

3) If A[i] > A[i+1], swap A[i] and A[i+1]

4) Increase i by 1

5) Repeat Steps 3 – 4 until i = stopper

6) Decrease stopper by 1

7) Repeat Steps 2 – 6 until stopper = 1

Given: Array A

Variables: i, stopper

Page 6: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Bubble Sort Run

stopper = 5

i = 1

4 > 1? Yes, swap

i = 2

4 > 9? No

i = 3

9 > 1? Yes, swap

i = 4

9 > 3? Yes, swap

i = 5

4 1 9 1 3Given:

1 4 9 1 3

1 4 1 9 3

1 4 1 3 9

stopper = 4

i = 1

1 > 4? No

i = 2

4 > 1? Yes, swap

i = 3

4 > 3? Yes, swap

i = 4

1 1 4 3 9

1 1 3 4 9

(1) (2)

Page 7: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Bubble Sort Run, continued

stopper = 3

i = 1

1 > 1? No

i = 2

1 > 3? No

i = 3

(3)

stopper = 2

i = 1

1 > 1? No

i = 2

(4)

stopper = 1

We’re done!

(5)

• We could save ourselves some computation by recognizing when the array is sorted (more on this later on)

• When a swap occurs, two elements are not in the right order

• When a swap doesn’t occur during Steps 3 – 4, all elements are in order!

Page 8: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Swapping Two ValuesGiven two variables, x and y, intuitively, one would swap the two variables as follows:

x = y

y = x

Observe the first line: once x gets y’s value, the second line would not change y’s value! The solution is to hold x’s initial value in another variable!

temp = x

x = y

y = temp

Page 9: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Bubble Sort In Scratch

stopper begins at the end of the array

until stopper reaches beginning of array

i starts at 0

until i reaches the value of stopper

if A[i] > A[i+1]:

temp = A[i]

A[i+1] = A[i]

A[i] = temp

move onto the next index

after moving biggest number to the end, we no longer need to consider the old value of stopper, so decrease it

Page 10: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Bubble Sort Time Complexity

2 (accessing an array element costs 1 operation)

1

4

2

3

3

1

1

1 *

n *

(4

+ 2

+ 3

+ 3

+ 1

) =

13

n

1 *

n *

(1

+ 1

3n

+ 1

) =

13

n2

+ 2

n

T(n) = 13n2 + 2n + 2

O(T(n)) = O(n2)

Page 11: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Modified Bubble Sort Algorithm (Ascending)

1) Set stopper to length of the array

2) Set i to 1 and set swaps to 0

3) If A[i] > A[i+1], swap A[i] and A[i+1] and increase swaps by 1

4) Increase i by 1

5) Repeat Steps 3 – 4 until i = stopper

6) Decrease stopper by 1

7) Repeat Steps 2 – 6 until stopper = 1 or until swaps = 0

Given: Array A

Variables: i, stopper, swaps

Page 12: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Modified Bubble Sort Run

stopper = 5

i = 1, swaps = 0

4 > 1? Yes, swap, increase swaps by 1

i = 2

4 > 9? No

i = 3

9 > 1? Yes, swap, increase swaps by 1

i = 4

9 > 3? Yes, swap, increase swaps by 1

i = 5

4 1 9 1 3Given:

stopper = 4

i = 1, swaps = 0

1 > 4? No

i = 2

4 > 1? Yes, swap, increase swaps by 1

i = 3

4 > 3? Yes, swap, increase swaps by 1

i = 4

(1) (2)

Page 13: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Modified Bubble Sort Run, continued

stopper = 3

i = 1, swaps = 0

1 > 1? No

i = 2

1 > 3? No

i = 3

(3)

• We save on computation just by recognizing when swaps are no longer necessary, which means the array is sorted!

• A swap count was kept here because Scratch does not exactly support boolean variables (at the time of this writing)

stopper = 2

We’re done since swaps = 0

(4)

Page 14: Sorting: Bubble Sort - venus.cs.qc.cuny.eduvenus.cs.qc.cuny.edu/~kchin/past/spring2015/cs90/slides/Bubble Sort... · Bubble Sort Idea 1) Start looking at the first pair of numbers

Modified Bubble Sort In Scratch

can be any non-zero number; this is to allow the loop to run the first time around

• This code has the same worst-case time complexity as our original bubble-sort code

• Best-Case is now O(n), or linear time (best-case is the scenario in which the algorithm will perform the least number of steps possible)