Professional Documents
Culture Documents
Bubble Sort (With Code in Python - C++ - Java - C)
Bubble Sort (With Code in Python - C++ - Java - C)
(https://srv.carbonads.net/ads/click/x/GTND42JUCA
segment=placement:wwwprogramizcom;)
ADS VIA CARBON
(HTTP://CARBONADS.NET/?
M_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
Bubble Sort
In this tutorial, you will learn about the bubble sort
algorithm and its implementation in Python, Java, C, and
C++.
Just like the movement of air bubbles in the water that rise
up to the surface, each element of the array move to the
end in each iteration. Therefore, it is called a bubble sort.
2. Remaining Iteration
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO Preparation with
Thank you for printing our content at www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO
Claim Your Discount
The array is sorted if all elements are kept in the right order
bubbleSort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
end bubbleSort
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO Preparation with
Thank you for printing our content at www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO
# Bubble sort in Python
Claim Your Discount
def bubbleSort(array):
Search tutorials & examples
(/)
# loop to access each array element
www.domain-name.com
for i in range(len(array)):
bubbleSort(data)
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO Preparation with
Thank you for printing our content at www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO
Claim Your Discount
bubbleSort(array)
swapped <- false
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
swapped <- true
end bubbleSort
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO Preparation with
Thank you for printing our content at www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO Bubble sort in Python
# Optimized
Claim Your Discount
def bubbleSort(array):
Search tutorials & examples
(/)
# loop through each element of array
www.domain-name.com
for i in range(len(array)):
swapped = True
Time Complexity
Best O(n)
Worst O(n2)
Average O(n2)
Stability Yes
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO Complexity
Preparation
Thank you for printing with
our contentinatDetail
www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO
Bubble
Claim YourSort compares the adjacent elements.
Discount
2nd (n-2)
3rd (n-3)
....... ......
last 1
nearly equals to n2
1. Time Complexities
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO 2. Space
Preparation
Thank you for printing Complexity
with
our content at www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO
ClaimSpace complexity is O(1) because an extra variable is
Your Discount
used forSearch
swapping.
tutorials & examples
(/)
In the optimizedwww.domain-name.com
bubble sort algorithm, two extra
variables are used. Hence, the space complexity will
be O(2) .
Next Tutorial:
(/dsa/selection-sort)
Selection Sort
Previous Tutorial:
Bellman Ford's (/dsa/bellman-ford-
algorithm)
Algorithm
Share on:
Try hands-on
(https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO (https://www.facebook.com/sharer/sharer.php?
Preparation with
Thank you for printing our content at www.domain-name.com. (https://twitter.com/inte
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF u=https://www.programiz.com/dsa/bubble-sort)
Programiz PRO text=Check%20this%20
sort)
Claim Your Discount
Related Tutorials
DS & Algorithms
(/dsa/selection-sort)
DS & Algorithms
(/dsa/insertion-sort)
Try hands-on
DS & Algorithms (https://programiz.pro/course/python-interview-questions?
CODING Interview utm source=programiz-top-
utm_source programiz top
PRO Shell Sort
Preparation Algorithm
with
Thank you for printing our content at www.domain-name.com.
bar&utm_campaign=programiz&utm_medium=referral)
Please check back soon for new
contents.
36% OFF Programiz PRO
Claim Your Discount
(/dsa/counting-sort)