• Keine Ergebnisse gefunden

Reading in data and Sorting

N/A
N/A
Protected

Academic year: 2022

Aktie "Reading in data and Sorting"

Copied!
104
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Departement Informatik

Basics of CS

Reading in data and Sorting

Hans-Joachim Böckenhauer Dennis Komm

Autumn 2021 – November 4, 2021

(2)

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

(3)

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

(4)

Faster Primality Testing

What is the gain this time?

Time complexity is in O( 2 n ) = O(2 n/2 ) = O(1.415 n )

(5)

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

(6)

Faster Primality Testing

What is the gain this time?

What is the time complexity of this algorithm?

Loop is iterated √

x/2 times

(7)

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

(8)

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 )

(9)

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

(10)

5 10 15 20 25 30 35 40 45 0

2 000 000 4 000 000 6 000 000 8 000 000

2

n

(11)

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

2

n

1.415

n

Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 3 / 28

(12)

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

(13)

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

(14)

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

(15)

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

(16)

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

(17)

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

(18)

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

(19)

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

(20)
(21)

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

(22)

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

(23)

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

(24)

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)

(25)

What else can we do?

(26)

Test every number

between 1 and x

(27)

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

(28)

Test every number between 1 and x

Test every second number between 1 and x

Test every second number between 1 and √

x

(29)

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

(30)

Randomized Monte Carlo algorithm

Polynomial AKS

algorithm

(31)

Lists

Advanced Concepts

(32)

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] ]

(33)

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

(34)

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] ]

(35)

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

(36)
(37)

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

(38)

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)

(39)

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

(40)

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

(41)

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

(42)

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

(43)

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

(44)

Reading in Data

2. Extract entries from first line (separator symbol: comma)

tmp = lines[0].split(",")

"1, 6, 5, 9, 6" ]

(45)

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

(46)

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])

(47)

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

(48)

# 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

(49)

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

(50)

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

(51)

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

(52)
(53)

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

(54)

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

(55)

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

(56)
(57)

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

(58)

Bubblesort

5 1 4 3

(59)

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

(60)

Bubblesort

1 5 4 3

(61)

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

(62)

Bubblesort

1 5 4 3

(63)

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

(64)

Bubblesort

1 4 5 3

(65)

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

(66)

Bubblesort

1 4 3 5

(67)

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

(68)

Bubblesort

1 4 3 5

(69)

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

(70)

Bubblesort

1 4 3 5

(71)

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

(72)

Bubblesort

1 3 4 5

(73)

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

(74)

Bubblesort

1 3 4 5

(75)

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

(76)

Bubblesort

1 3 4 5

(77)

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

(78)

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

(79)

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

(80)

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

(81)

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

(82)

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

(83)

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

(84)

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

(85)

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

(86)

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]))

(87)

Sorting 1

Minsort

(88)

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

(89)

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

(90)

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

(91)

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

(92)
(93)

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 2n)/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

(94)

Time Complexity of Bubblesort

Count comparisons of two numbers n − 1 comparisons to find maximum

ï i=1 i = (n − 1) · n/2 = (n 2n)/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 )

(95)

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 2n)/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

(96)

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 )

(97)

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 2n)/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

(98)

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 2n)/2 comparisons in total

ï Quadratic number of comparisons

(99)

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 2n)/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

(100)

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 2n)/2 comparisons in total ï Quadratic number of comparisons

The time complexity of Bubblesort is in O(n 2 )

(101)

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

(102)

10 20 30 40 50 60 70 80 90 100 0

1 000 2 000 3 000 4 000

Bubblesort / Minsort

Compar isons

(103)

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

(104)

Referenzen

ÄHNLICHE DOKUMENTE

Basics of Computer Science for Human Medicine – Introduction to the Course Autumn 2021 Böckenhauer, Komm 1 / 36.?.

Basics of Computer Science for Human Medicine – Introduction to the Course Autumn 2021 Böckenhauer, Komm 3 / 36.. Goal of

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 2 / 21...

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 1 / 21.

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 1 / 36... Exercise –

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 7 / 36.

Basics of Computer Science for Human Medicine – Functions and return values Autumn 2021 Böckenhauer, Komm 2 / 26... but can in general not be

Basics of Computer Science for Human Medicine – numpy, matplotlib Autumn 2021 Böckenhauer, Komm 4 /