






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Sorts a list of elements such as integers or real numbers.
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.
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.
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.
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.
BubbleSort (Basic) is in-place. This means that the list is sorted by moving elements within the list, rather than creating a new list.
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.
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.
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 ).
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 ).
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:
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]