Departement Informatik
Basics of CS
Reading in data and Sorting
Hans-Joachim Böckenhauer Dennis Komm
Autumn 2021 – November 4, 2021
def primetest3(x):
if x < 2 or (x > 2 and x % 2 == 0):
return False d = 3
while d < x:
if x % d == 0:
return False
d += 2
Faster Primality Testing
from math import sqrt
def primetest3(x):
if x < 2 or (x > 2 and x % 2 == 0):
return False d = 3
wurzelx = sqrt(x) while d <= wurzelx:
if x % d == 0:
return False d += 2
return True
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 1 / 28
Faster Primality Testing
What is the gain this time?
Time complexity is in O( 2 n ) = O(2 n/2 ) = O(1.415 n )
Faster Primality Testing
What is the gain this time?
What is the time complexity of this algorithm?
Loop is iterated √
x/2 times Time complexity “grows” with √
x Time complexity is in O( √
2 n ) = O(2 n/2 ) = O(1.415 n )
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 2 / 28
Faster Primality Testing
What is the gain this time?
What is the time complexity of this algorithm?
Loop is iterated √
x/2 times
Faster Primality Testing
What is the gain this time?
What is the time complexity of this algorithm?
Loop is iterated √
x/2 times Time complexity “grows” with √
x
Time complexity is in O( √
2 n ) = O(2 n/2 ) = O(1.415 n )
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 2 / 28
What is the gain this time?
What is the time complexity of this algorithm?
Loop is iterated √
x/2 times Time complexity “grows” with √
x Time complexity is in O( √
2 n ) = O(2 n/2 ) = O(1.415 n )
Faster Primality Testing
5 10 15 20 25 30 35 40 45
0 2 000 000 4 000 000 6 000 000 8 000 000 10 000 000
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 3 / 28
5 10 15 20 25 30 35 40 45 0
2 000 000 4 000 000 6 000 000 8 000 000
2
nFaster Primality Testing
5 10 15 20 25 30 35 40 45
0 2 000 000 4 000 000 6 000 000 8 000 000 10 000 000
2
n1.415
nBasics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 3 / 28
Faster Primality Testing
Suppose our computer can do 1000 iterations of the loop per second
100 000 000 000 031 iterations 1000 iterations second
> 100 000 000 000 seconds
> 3100 years
√ 100 000 000 000 031 iterations 1000 iterations second
≈ 10 000 000 iterations 1000 iterations second
< 3 hours
Even if the computer that runs the slower program is 100 time faster, it still
needs 31 years
Faster Primality Testing
Suppose our computer can do 1000 iterations of the loop per second; for x = 100 000 000 000 031 this means:
. . . d < x . . .
100 000 000 000 031 iterations 1000 iterations second
> 100 000 000 000 seconds
> 3100 years
. . . d <= sqrt(x) . . .
√ 100 000 000 000 031 iterations 1000 iterations second
≈ 10 000 000 iterations 1000 iterations second
< 3 hours
Even if the computer that runs the slower program is 100 time faster, it still needs 31 years
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 4 / 28
Faster Primality Testing
Suppose our computer can do 1000 iterations of the loop per second; for x = 100 000 000 000 031 this means:
. . . d < x . . .
100 000 000 000 031 iterations 1000 iterations second
> 100 000 000 000 seconds
> 3100 years
. . . d <= sqrt(x) . . .
√ 100 000 000 000 031 iterations 1000 iterations second
≈ 10 000 000 iterations 1000 iterations second
< 3 hours
Faster Primality Testing
Suppose our computer can do 1000 iterations of the loop per second; for x = 100 000 000 000 031 this means:
. . . d < x . . .
100 000 000 000 031 iterations 1000 iterations second
> 100 000 000 000 seconds
> 3100 years
. . . d <= sqrt(x) . . .
√ 100 000 000 000 031 iterations 1000 iterations second
≈ 10 000 000 iterations 1000 iterations second
< 3 hours
Even if the computer that runs the slower program is 100 time faster, it still needs 31 years
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 4 / 28
Faster Primality Testing
Or the other way around. . .
Suppose we want to spend 10 minutes
1000 iterations second = 600 seconds
⇐⇒ x = 600 000
1000 iterations second = 600 seconds
⇐⇒ x = 600 000 2
⇐⇒ x = 360 000 000 000
Faster Primality Testing
Or the other way around. . .
Suppose we want to spend 10 minutes
Then there are at most “testable” primes in the magnitude of:
. . . d < x . . . x iterations
1000 iterations second = 600 seconds
⇐⇒ x = 600 000
. . . d <= sqrt(x) . . .
√ x iterations
1000 iterations second = 600 seconds
⇐⇒ x = 600 000 2
⇐⇒ x = 360 000 000 000
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 5 / 28
Faster Primality Testing
Or the other way around. . .
Suppose we want to spend 10 minutes
Then there are at most “testable” primes in the magnitude of:
. . . d < x . . . x iterations
1000 iterations second = 600 seconds
⇐⇒ x = 600 000
⇐⇒ x = 600 000 2
⇐⇒ x = 360 000 000 000
Faster Primality Testing
Or the other way around. . .
Suppose we want to spend 10 minutes
Then there are at most “testable” primes in the magnitude of:
. . . d < x . . . x iterations
1000 iterations second = 600 seconds
⇐⇒ x = 600 000
. . . d <= sqrt(x) . . .
√ x iterations
1000 iterations second = 600 seconds
⇐⇒ x = 600 000 2
⇐⇒ x = 360 000 000 000
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 5 / 28
Best and Worst Case Analysis
Which algorithm is faster?
def primetest3(x):
if x < 2 or (x > 2 and x % 2 == 0):
return False d = 3
while d <= sqrt(x):
if x % d == 0:
return False d += 2
return True
def primetest4(x):
if x < 2 or (x > 2 and x % 2 == 0):
return False d = 3
isprime = True while d <= sqrt(x):
if x % d == 0:
isprime = False d += 2
return isprime
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 6 / 28
Best and Worst Case Analysis
Suppose x is a multiple of 3
Then the left algorithm is faster ï Loop is left after first iteration
«Early Exit»
Right algorithm makes roughly 1.415 n /2 comparisons
Suppose x is prime
Best and Worst Case Analysis
Suppose x is a multiple of 3 Then the left algorithm is faster ï Loop is left after first iteration
«Early Exit»
Right algorithm makes roughly 1.415 n /2 comparisons
Suppose x is prime
Then both algorithms make 1.415 n /2 comparisons (Of course, still the left one should be implemented)
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 7 / 28
Then the left algorithm is faster ï Loop is left after first iteration
«Early Exit»
Right algorithm makes roughly 1.415 n /2 comparisons Suppose x is prime
Then both algorithms make 1.415 n /2 comparisons
(Of course, still the left one should be implemented)
What else can we do?
Test every number
between 1 and x
Primality test
Test every number between 1 and x
Test every second number between 1 and x
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 8 / 28
Test every number between 1 and x
Test every second number between 1 and x
Test every second number between 1 and √
x
Primality test
Randomized Monte Carlo algorithm
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 8 / 28
Randomized Monte Carlo algorithm
Polynomial AKS
algorithm
Lists
Advanced Concepts
2-Dimensional Lists
So far lists contain numbers or characters
Lists can contain lists
Such 2-dimensional lists store, e.g., tables and matrices
M =
2 0 3 0 6 3 9 5 1 1 0 0 7 2 7 3 9 5 8 0 8 2 0 3 2 1 6 5 9 6
M = [ [2, 0, 3, 0, 6],
[3, 9, 5, 1, 1],
[0, 0, 7, 2, 7],
[3, 9, 5, 8, 0],
[8, 2, 0, 3, 2],
[1, 6, 5, 9, 6] ]
2-Dimensional Lists
So far lists contain numbers or characters Lists can contain lists
Such 2-dimensional lists store, e.g., tables and matrices
M =
2 0 3 0 6 3 9 5 1 1 0 0 7 2 7 3 9 5 8 0 8 2 0 3 2 1 6 5 9 6
M = [ [2, 0, 3, 0, 6], [3, 9, 5, 1, 1], [0, 0, 7, 2, 7], [3, 9, 5, 8, 0], [8, 2, 0, 3, 2], [1, 6, 5, 9, 6] ]
Accessing line i and column j with M[i][j]
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 9 / 28
2-Dimensional Lists
So far lists contain numbers or characters Lists can contain lists
Such 2-dimensional lists store, e.g., tables and matrices
M =
2 0 3 0 6 3 9 5 1 1 0 0 7 2 7 3 9 5 8 0 8 2 0 3 2 1 6 5 9 6
M = [ [2, 0, 3, 0, 6],
[3, 9, 5, 1, 1],
[0, 0, 7, 2, 7],
[3, 9, 5, 8, 0],
[8, 2, 0, 3, 2],
[1, 6, 5, 9, 6] ]
2-Dimensional Lists
So far lists contain numbers or characters Lists can contain lists
Such 2-dimensional lists store, e.g., tables and matrices
M =
2 0 3 0 6 3 9 5 1 1 0 0 7 2 7 3 9 5 8 0 8 2 0 3 2 1 6 5 9 6
M = [ [2, 0, 3, 0, 6], [3, 9, 5, 1, 1], [0, 0, 7, 2, 7], [3, 9, 5, 8, 0], [8, 2, 0, 3, 2], [1, 6, 5, 9, 6] ]
Accessing line i and column j with M[i][j]
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 9 / 28
Reading in Data
Example: Matrix given in file Content of the file is a text Matrix stored line by line
Entries in each line separated by commas Entries are to be interpreted as numbers
Three Steps
1. Read in file line by line
2. Extract entries from the lines (separator symbol: comma) 3. Convert each entry to a number
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 10 / 28
Content of the file is a text Matrix stored line by line
Entries in each line separated by commas Entries are to be interpreted as numbers Three Steps
1. Read in file line by line
2. Extract entries from the lines (separator symbol: comma)
Reading in Data
1. Read in file line by line
with open("data.txt") as file:
lines = file.read().splitlines()
File data.txt is opened for the following block of instructions Accessible under the name file
lines = file.read() stores the whole content of data.txt in the variable lines
lines = file.read().splitlines() stores the individual lines of daten.txt in the list lines
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 11 / 28
Reading in Data
1. Read in file line by line
with open("data.txt") as file:
File data.txt is opened for the following block of instructions Accessible under the name file
lines = file.read() stores the whole content of data.txt in the variable lines
lines = file.read().splitlines() stores the individual lines of
daten.txt in the list lines
Reading in Data
1. Read in file line by line
with open("data.txt") as file:
lines = file.read().splitlines()
File data.txt is opened for the following block of instructions Accessible under the name file
lines = file.read() stores the whole content of data.txt in the variable lines
lines = file.read().splitlines() stores the individual lines of daten.txt in the list lines
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 11 / 28
with open("data.txt") as file:
lines = file.read().splitlines()
File data.txt is opened for the following block of instructions Accessible under the name file
lines = file.read() stores the whole content of data.txt in the variable lines
lines = file.read().splitlines() stores the individual lines of
daten.txt in the list lines
Reading in Data: Example
1. Read in file line by line
with open("data.txt") as file:
lines = file.read().splitlines()
data.txt 2, 0, 3, 0, 6 3, 9, 5, 1, 1 0, 0, 7, 2, 7 3, 9, 5, 8, 0 8, 2, 0, 3, 2 1, 6, 5, 9, 6
lines = [ "2, 0, 3, 0, 6",
"3, 9, 5, 1, 1",
"0, 0, 7, 2, 7",
"3, 9, 5, 8, 0",
"8, 2, 0, 3, 2",
"1, 6, 5, 9, 6" ]
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 12 / 28
Reading in Data
2. Extract entries from first line (separator symbol: comma)
tmp = lines[0].split(",")
"1, 6, 5, 9, 6" ]
Reading in Data
2. Extract entries from first line (separator symbol: comma)
tmp = lines[0].split(",")
lines = [ "2, 0, 3, 0, 6",
"3, 9, 5, 1, 1",
"0, 0, 7, 2, 7",
"3, 9, 5, 8, 0",
"8, 2, 0, 3, 2",
"1, 6, 5, 9, 6" ]
tmp = ["2", "0", "3", "0", "6"]
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 13 / 28
Reading in Data
3. Convert each entry to a number
data = [0] * len(tmp)
for i in range(0, len(tmp)):
data[i] = int(tmp[i])
Reading in Data
3. Convert each entry to a number
data = [0] * len(tmp)
for i in range(0, len(tmp)):
data[i] = int(tmp[i])
tmp = ["2", "0", "3", "0", "6"] data = [2, 0, 3, 0, 6]
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 14 / 28
# Read in file line by line with open(filename) as file:
lines = file.read().splitlines()
# Extract entries from first line (separator symbol: comma) tmp = lines[0].split(",")
# Convert each entry to a number data = [0] * len(tmp)
for i in range(0, len(tmp)):
data[i] = int(tmp[i])
return data
Exercise – Reading in Data
Extend the function so that all lines of the file are read and converted
the content is stored in a 2-dimensional list
def readfile(filename):
with open(filename) as file:
lines = file.read().splitlines() tmp = lines[0].split(",")
data = [0] * len(tmp) for i in range(0, len(tmp)):
data[i] = int(tmp[i]) return data
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 16 / 28
Reading in Data
def readfile2(filename):
# Read in file line by line with open(filename) as file:
lines = file.read().splitlines() data = []
# Process all lines successively for i in range(0, len(lines)):
tmp = lines[i].split(",") dataline = [0] * len(tmp) for j in range(0, len(tmp)):
dataline[j] = int(tmp[j])
data.append(dataline)
return data
Reading in Data
def readfile2(filename):
# Read in file line by line with open(filename) as file:
lines = file.read().splitlines() data = []
# Process all lines successively for i in range(0, len(lines)):
tmp = lines[i].split(",") dataline = [0] * len(tmp) for j in range(0, len(tmp)):
dataline[j] = int(tmp[j]) data.append(dataline) return data
Data is often supplied as such csv files (comma separated values)
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 17 / 28
Sorting and Searching
Sorting and searching data are two of the fundamental tasks of computer scientists
Standard reference only deals with these topics Given n positive integers
Specifically, unsorted list data with n = len(data) We consider n as input length
Numbers may appear multiple times Sort numbers in as little time as possible
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 18 / 28
Sorting and Searching
Sorting and searching data are two of the fundamental tasks of computer scientists
Standard reference only deals with these topics
Numbers may appear multiple times
Sort numbers in as little time as possible
Sorting and Searching
Sorting and searching data are two of the fundamental tasks of computer scientists
Standard reference only deals with these topics Given n positive integers
Specifically, unsorted list data with n = len(data) We consider n as input length
Numbers may appear multiple times Sort numbers in as little time as possible
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 18 / 28
Bubblesort
5
1
1
5 4 3
4
5 3 4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
5 1 4 3
Bubblesort
5
1
1
5 4 3
4
5 3 4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 5 4 3
Bubblesort
5
1
1
5
4 3
4
5 3 4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 5 4 3
Bubblesort
5
1
1 5
4
3 4
5
3 4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 4 5 3
Bubblesort
5
1
1 5
4
3 4
5
3 4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 4 3 5
Bubblesort
5
1
1 5
4
3 4 5
3
4 3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 4 3 5
Bubblesort
5
1
1 5
4
3 4 5
3
4 3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 4 3 5
Bubblesort
5
1
1 5
4
3 4 5
3
4 3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 3 4 5
Bubblesort
5
1
1 5 4
3
4 5 3
4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 3 4 5
Bubblesort
5
1
1 5 4
3
4 5 3
4
3
5
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 19 / 28
Bubblesort
1 3 4 5
Bubblesort
Idea
Sorting by repeatedly finding the maximum
Goal
Sort list data with n elements, i.e., range 0, . . . , n − 1
Find maximum and slide it to the last position
To this end, iteratively compare neighboring elements
Maximum travels through list to the last position – like a bubble Repeat with range 0, . . . , n − 2
Continue until data is sorted
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 20 / 28
Bubblesort
Idea
Sorting by repeatedly finding the maximum Goal
Sort list data with n elements, i.e., range 0, . . . , n − 1
Repeat with range 0, . . . , n − 2
Continue until data is sorted
Bubblesort
Idea
Sorting by repeatedly finding the maximum Goal
Sort list data with n elements, i.e., range 0, . . . , n − 1 Find maximum and slide it to the last position
To this end, iteratively compare neighboring elements
Maximum travels through list to the last position – like a bubble Repeat with range 0, . . . , n − 2
Continue until data is sorted
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 20 / 28
Bubblesort
Idea
Sorting by repeatedly finding the maximum Goal
Sort list data with n elements, i.e., range 0, . . . , n − 1 Find maximum and slide it to the last position
To this end, iteratively compare neighboring elements
Maximum travels through list to the last position – like a bubble
Bubblesort
Idea
Sorting by repeatedly finding the maximum Goal
Sort list data with n elements, i.e., range 0, . . . , n − 1 Find maximum and slide it to the last position
To this end, iteratively compare neighboring elements
Maximum travels through list to the last position – like a bubble Repeat with range 0, . . . , n − 2
Continue until data is sorted
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 20 / 28
Sorting by repeatedly finding the maximum Goal
Sort list data with n elements, i.e., range 0, . . . , n − 1 Find maximum and slide it to the last position
To this end, iteratively compare neighboring elements
Maximum travels through list to the last position – like a bubble
Repeat with range 0, . . . , n − 2
Exercise – One Bubble Sequence
Implement one Bubble Sequence Run through data one time Compare neighboring elements Swap if the first element is larger Maximum bubbles to the right
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 21 / 28
data = [6, 22, 61, 1, 89, 31, 9, 10, 76]
n = len(data)
for i in range(0, n-1):
if data[i] > data[i+1]:
tmp = data[i]
data[i] = data[i+1]
data[i+1] = tmp
Exercise – Bubblesort
Implement the complete algorithm Iterate bubble sequences
After ith sequence, the last k elements of data are sorted
Bubble sequences become shorter with each iteration
To this end, use outer loop
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 23 / 28
n = len(data)
for d in range(n, 1, -1):
for i in range(0, d-1):
if data[i] > data[i+1]:
tmp = data[i]
data[i] = data[i+1]
data[i+1] = tmp return data
print(bubblesort([6, 22, 61, 1, 89, 31, 9, 10, 76]))
Sorting 1
Minsort
Minsort
Idea
Sorting by repeatedly finding the minimum
If it is smaller, both are swapped
After one iteration, the minimum is copied to (current) first position
Continue until data is sorted
Minsort
Idea
Sorting by repeatedly finding the minimum
Unlike Bubblesort, we do not compare neighboring elements Current minimum is stored
Each element is compared to it If it is smaller, both are swapped
After one iteration, the minimum is copied to (current) first position Continue until data is sorted
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 25 / 28
Idea
Sorting by repeatedly finding the minimum
Unlike Bubblesort, we do not compare neighboring elements Current minimum is stored
Each element is compared to it If it is smaller, both are swapped
After one iteration, the minimum is copied to (current) first position
Continue until data is sorted
Minsort
def minsort(data):
n = len(data)
for current in range(0, n-1):
minimum = data[current]
for i in range(current+1, n):
if data[i] < minimum:
tmp = data[i]
data[i] = minimum minimum = tmp data[current] = minimum return data
print(minsort([6, 22, 61, 1, 89, 31, 9, 10, 76]))
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 26 / 28
Time Complexity of Bubblesort
Count comparisons of two numbers
n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element . . .
1 comparison to find smallest element
ï P n−1 i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total ï Quadratic number of comparisons
The time complexity of Bubblesort is in O(n 2 )
With similar arguments, the time complexity of Minsort is in O(n 2 )
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 27 / 28
Time Complexity of Bubblesort
Count comparisons of two numbers n − 1 comparisons to find maximum
ï i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total ï Quadratic number of comparisons
The time complexity of Bubblesort is in O(n 2 )
With similar arguments, the time complexity of Minsort is in O(n 2 )
Time Complexity of Bubblesort
Count comparisons of two numbers n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element
. . .
1 comparison to find smallest element
ï P n−1 i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total ï Quadratic number of comparisons
The time complexity of Bubblesort is in O(n 2 )
With similar arguments, the time complexity of Minsort is in O(n 2 )
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 27 / 28
Time Complexity of Bubblesort
Count comparisons of two numbers n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element . . .
The time complexity of Bubblesort is in O(n 2 )
With similar arguments, the time complexity of Minsort is in O(n 2 )
Time Complexity of Bubblesort
Count comparisons of two numbers n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element . . .
1 comparison to find smallest element
ï P n−1 i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total ï Quadratic number of comparisons
The time complexity of Bubblesort is in O(n 2 )
With similar arguments, the time complexity of Minsort is in O(n 2 )
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 27 / 28
Time Complexity of Bubblesort
Count comparisons of two numbers n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element . . .
1 comparison to find smallest element
ï P n−1 i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total
ï Quadratic number of comparisons
Time Complexity of Bubblesort
Count comparisons of two numbers n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element . . .
1 comparison to find smallest element
ï P n−1 i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total ï Quadratic number of comparisons
The time complexity of Bubblesort is in O(n 2 )
With similar arguments, the time complexity of Minsort is in O(n 2 )
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 27 / 28
n − 1 comparisons to find maximum
n − 2 comparisons to find second largest element . . .
1 comparison to find smallest element
ï P n−1 i=1 i = (n − 1) · n/2 = (n 2 − n)/2 comparisons in total ï Quadratic number of comparisons
The time complexity of Bubblesort is in O(n 2 )
Time Complexity of Bubblesort
10 20 30 40 50 60 70 80 90 100
0 1 000 2 000 3 000 4 000 5 000
Input length n
Compar isons
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 28 / 28
10 20 30 40 50 60 70 80 90 100 0
1 000 2 000 3 000 4 000
Bubblesort / Minsort
Compar isons
Time Complexity of Bubblesort
10 20 30 40 50 60 70 80 90 100
0 1 000 2 000 3 000 4 000 5 000
Bubblesort / Minsort
Goal
Input length n
Compar isons
Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 28 / 28