0% found this document useful (0 votes)
2 views

Algorithms_ Bubble Sort Cheatsheet _ Codecademy

The document provides an overview of algorithms, specifically focusing on the Bubble Sort algorithm, which sorts a list of numbers in ascending order through iterative comparisons and swaps. It explains the structure of the algorithm, including its outer and inner iterations, and outlines its Big-O runtime complexity as O(N^2). Additionally, it discusses the variable swapping process required for the algorithm, highlighting differences in implementation across programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithms_ Bubble Sort Cheatsheet _ Codecademy

The document provides an overview of algorithms, specifically focusing on the Bubble Sort algorithm, which sorts a list of numbers in ascending order through iterative comparisons and swaps. It explains the structure of the algorithm, including its outer and inner iterations, and outlines its Big-O runtime complexity as O(N^2). Additionally, it discusses the variable swapping process required for the algorithm, highlighting differences in implementation across programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Algorithms

Bubble Sort

Algorithms

Algorithms are series of step-by-step instructions for a


computer to complete a task.

Well-Known Algorithms

There are many well-known algorithms that describe a


specific way to solve common problems.
For example, one such problem is being able to sort
data, which can be solved with the following algorithms:
Bubble Sort
Merge Sort
Quicksort

Bubble Sort Algorithm

The Bubble Sort algorithm is a simple algorithm to sort


a list of N numbers in ascending order. Bubble sort
works by iterating through a list and checking whether
the current element is larger or smaller than the next
element.
This algorithm consists of an outer iteration and an
inner iteration. In the inner iteration, the first and
second elements are first compared and swapped so
that the second element has a higher value than the
first. This is repeated for the subsequent second and
third element pairs and so forth until the last pair of (N-
2, N-1) elements is compared. At the end of the inner
iteration, the largest element appears last. This is
repeated for all elements of the list in the outer
iteration.
Bubble Sort Big-O Runtime

The Bubble Sort algorithm utilizes two loops: an outer


loop to iterate over each element in the input list, and
an inner loop to iterate, compare and exchange a pair
of values in the list. The inner loop takes (N-1) iterations
while the outer loop takes N iterations. Hence, the Big-
O runtime for the algorithm is the product of O(N) and
O(N-1), which is O(N^2).

Bubble Sort Swapping Variables

The Bubble Sort algorithm requires swapping of


variables in order to sort them. The swapping algorithm
is dependent on the programming language. For most
languages, a temporary variable is needed to hold one
of the values being swapped:
temp_variable = number_1
number_1 = number_2
number_2 = temp_variable

For others, the swapping can be done in a single


assignment:
number_1, number_2 = number_2, number_1

Print Share

You might also like