Bubble Sort Algorithm: What it Does, How it Works, and Visualization, Summaries of Combinatorics

An in-depth explanation of the Bubble Sort algorithm, including its functionality, working process, visualization, pseudocode, time complexity, and stability. It covers both the basic and improved versions of the algorithm and includes Python test code with output.

Typology: Summaries

2021/2022

Uploaded on 09/12/2022

kiras
kiras 🇬🇧

4.7

(21)

292 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 351: BubbleSort (Basic)
Justin Wyss-Gallifent
February 3, 2022
1 WhatitDoes ............................. 2
2 HowitWorks............................. 2
3 Visualization ............................. 3
4 Pseudocode and Time Complexity . . . . . . . . . . . . . . . . . 4
5 AuxiliarySpace............................ 4
6 Stability................................ 5
7 In-Place ................................ 5
8 Notes ................................. 5
9 Bubble Sort (Variation) . . . . . . . . . . . . . . . . . . . . . . . 6
10 ClosingNotes............................. 7
11 Thoughts, Problems, Ideas . . . . . . . . . . . . . . . . . . . . . . 8
12 Python Test (Basic Version) with Output . . . . . . . . . . . . . 10
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Bubble Sort Algorithm: What it Does, How it Works, and Visualization and more Summaries Combinatorics in PDF only on Docsity!

CMSC 351: BubbleSort (Basic)

  • February 3, Justin Wyss-Gallifent
  • 1 What it Does
  • 2 How it Works
  • 3 Visualization
  • 4 Pseudocode and Time Complexity
  • 5 Auxiliary Space
  • 6 Stability
  • 7 In-Place
  • 8 Notes
  • 9 Bubble Sort (Variation)
  • 10 Closing Notes
  • 11 Thoughts, Problems, Ideas
  • 12 Python Test (Basic Version) with Output

1 What it Does

Sorts a list of elements such as integers or real numbers.

2 How it Works

We pass through the list from left to right swapping elements which are out of order. If we pass through once, the final entry is in the right place because the largest element will have been swapped repeatedly until it is at the end. Thus the second pass through can stop before the final entry. If we pass through twice, the final two entries are in the right place and so the third pass through can stop before the second-to-last entry. We continue onwards. Thus we pass through n − 1 times total since once the final n − 1 entries are in order, all are.

4 Pseudocode and Time Complexity

Here is the pseudocode with time assignments:

PRE: A is a list of length n. for i = 0 to n-2 n times for j = 0 to n-i-2 n − i − 2 − 0 + 1 = n − i − 1 times if A[j] > A[j+1]

swap A[j] and A[j+1] c end end end POST: A is sorted.

This is a terrible implementation. The time required is always the same in the best-, worst-, and average-cases:

T (n) =

n∑− 2

i=

n−∑i− 2

j=

c

n∑− 2

i=

(n − i − 2 + 1)c

n∑− 2

i=

c(n − 1) − c

n∑− 2

i=

i

= (n − 2 + 1)c(n − 1) − c

(n − 2)(n − 1) 2

= c(n − 1)^2 −

c(n − 2)(n − 1)

=

cn^2 −

cn

= Θ(n^2 )

A quick note here that if we were interested in every little detail of time then we would need to label separate time constants for both the conditional check and for the conditional body and then best-, worst-, and average-cases would have different total times depending upon the frequency with which the body of the conditional executed. However in all cases the conditional is still taking constant time, even if those constant times are different, and hence the time complexity does not change.

5 Auxiliary Space

Our BubbleSort (Basic) pseudocode uses O(1) auxiliary space - two indices and possibly, depending on the implementation, a swap variable This is of course

pretty much ideal.

6 Stability

BubbleSort (Basic) is stable. This means that the order of identical entries is preserved. In the opening example if we think of the two 4s as different, perhaps the one on the left is red and one on the right is blue, then the red one is still on the left at the end. The reason for this is that equal elements are never swapped, meaning that the order of equal elements is always preserved.

7 In-Place

BubbleSort (Basic) is in-place. This means that the list is sorted by moving elements within the list, rather than creating a new list.

8 Notes

After k iterations the last k elements are correctly placed and sorted. If Bubble- Sort were running on a very long list and it was forced to stop early this means that some amount of the end of the list would be sorted but the beginning would not. This is the opposite of SelectionSort, as we’ll see.

10 Closing Notes

While BubbleSort is fairly useless in practice since it is slow and inefficient it is nonetheless useful for basic algorithmic understanding and some of the ideas therein can be used in other algorithms.

11 Thoughts, Problems, Ideas

  1. Fill in the calculation:

T (n) =

n∑− 1

i=

n−∑i− 2

j=

c 1 = ... = Θ(n^2 )

At the end, provide a rigorous proof from the definition of Θ that the result is Θ(n^2 ).

  1. Fill in the calculation:

T (n) = c 1 +

n∑− 1

i=

c 3 +

n−∑i− 2

j=

c 2

 (^) = ... = Θ(n^2 )

At the end, provide a rigorous proof from the definition of Θ that the result is Θ(n^2 ).

  1. Suppose BubbleSort (Basic) is used to sort the list [34,2,17,5,10]. Show the list after each swap and count the number of swaps.
  2. Modify the BubbleSort (Basic) pseudocode to produce an algorithm which moves all 0 s (if any) to the right and otherwise preserves order.
  3. BubbleSort (Basic) is stable. Explain in your own words what that means. What single-character change in the pseudocode would make it not stable?
  4. How many comparisons occur in BubbleSort (Basic)?
  5. In BubbleSort (Basic) how many swaps occur in a worst-case scenario? How about a best-case scenario?
  6. In BubbleSort (Basic) think about how many swaps might occur in an average-case scenario. What if the list was mostly sorted? This is a qualitative rather than a quantitative question.
  7. Each integer greater than 2 has a unique prime factorization. For example 243936 = 2^5 · 32 · 71 · 112. Suppose you want to write an algorithm which sorts the powers using BubbleSort and modifies the number accordingly, so we’d have, for example:

243936 = 2^5 · 32 · 71 · 112 =⇒ 21 · 32 · 72 · 115 = 142046982

You wish to use BubbleSort on the exponents but you are only given the integer n and you cannot construct or use a mutable list. You do have access to the commands:

  • ppow(n,p) which gives the exponent corresponding to the power of p in n, so ppow(243936,3) returns 2.

12 Python Test (Basic Version) with Output

Code:

import random A = [] for i in range (0 ,10) : A. append ( random. randint (0 ,100) ) n = len ( A )

print ( ’ Start : ’+ str ( A ) ) for i in range (0 , n ) : for j in range (0 ,n -i -1) : if A [ j ] > A [ j +1]: temp = A [ j ] A [ j ] = A [ j +1] A [ j +1] = temp print ( ’ Iterate : ’+ str ( A ) ) print ( A )

Output:

Start : [74 , 85 , 2 , 13 , 82 , 36 , 29 , 66 , 31 , 12] Iterate : [74 , 2 , 13 , 82 , 36 , 29 , 66 , 31 , 12 , 85] Iterate : [2 , 13 , 74 , 36 , 29 , 66 , 31 , 12 , 82 , 85] Iterate : [2 , 13 , 36 , 29 , 66 , 31 , 12 , 74 , 82 , 85] Iterate : [2 , 13 , 29 , 36 , 31 , 12 , 66 , 74 , 82 , 85] Iterate : [2 , 13 , 29 , 31 , 12 , 36 , 66 , 74 , 82 , 85] Iterate : [2 , 13 , 29 , 12 , 31 , 36 , 66 , 74 , 82 , 85] Iterate : [2 , 13 , 12 , 29 , 31 , 36 , 66 , 74 , 82 , 85] Iterate : [2 , 12 , 13 , 29 , 31 , 36 , 66 , 74 , 82 , 85] Iterate : [2 , 12 , 13 , 29 , 31 , 36 , 66 , 74 , 82 , 85] Iterate : [2 , 12 , 13 , 29 , 31 , 36 , 66 , 74 , 82 , 85] [2 , 12 , 13 , 29 , 31 , 36 , 66 , 74 , 82 , 85]