sorting 2
play

Sorting 2 Complexity of Sorting Hans-Joachim Bckenhauer and Dennis - PowerPoint PPT Presentation

Sorting 2 Complexity of Sorting Hans-Joachim Bckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming The modules numpy and matplotlib Autumn 2019 November 21, 2019 Complexity of Sorting How does the running time change


  1. Sorting 2 Complexity of Sorting Hans-Joachim Böckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming The modules numpy and matplotlib Autumn 2019 – November 21, 2019 Complexity of Sorting How does the running time change for specific inputs? Already sorted Sorted in reverse Sorting 2 Randomly chosen Bucketsort For Mergesort (and also Bubble- and Minsort), the number of comparisons is always the same for a fixed n This is not always the case Different best, average, and worst cases Timsort , for instance, makes use of already sorted subarrays Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 1 / 24

  2. Sorting of Few Elements Bucketsort Sorting of data sets with respect to one attribute 1 g 3 f 1 g 3 f 1 a 1 a 2 b 1 c 1 c 1 d 1 d 2 b 2 e 2 e 3 h 3 h . . . Stable sorting: Elements with same attribute maintain order . . . . . . Example 1 g Name First name Grade 1 d Adleman Leonard 6 1 c 2 e 3 h Caesar Gaius Julius 3 de Vigenère Blaise 5 3 f 1 a 2 b Rivest Ronald 6 Shamir Adi 6 1 2 3 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 2 / 24 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 3 / 24 Exercise – Bucketsort Bucketsort def bucketsort(data): one = [] Implement Bucketsort two = [] as Python function three = [] for item in data: using three stacks one , two , and if item == 1: if item == 1: three for the possible values 1, 2, one.append(item) one.append(item) and 3 else: elif item == 2: filling the stacks according to if item == 2: two.append(item) two.append(item) elif item == 3: numbers in the list else: three.append(item) concatenating the stacks at the end if item == 3: three.append(item) return one + two + three Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 4 / 24 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 5 / 24

  3. Time Complexity of Bucketsort Let n denote the input length Sorting 2 Let k denote the number of distinct values When filling the buckets, at most k − 1 comparisons per element Time Complexity of Bucketsort ➯ Total number of comparisons: roughly k · n The time complexity of Bucketsort is in O ( n ) if there is a constant number of different values Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 6 / 24 Time Complexity of Bucketsort 5 , 000 4 , 000 Lists Comparisons 3 , 000 Bubblesort / Minsort Advanced Concepts 2 , 000 Mergesort 1 , 000 0 Bucketsort ( k = 3) 10 20 30 40 50 60 70 80 90 100 Input length n Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 7 / 24

  4. Listen List Comprehensions So far Now: List Comprehensions to initialize. . . Initializing a list: x = [] or x = [1, 4, 8] a list of the first ten natural numbers: x = [i for i in range(0, 10)] Initializing a list with ten zeros: x = [0] * 10 a list of the first ten even numbers: Appending elements: x.append(3) x = [i for i in range(0, 20, 2)] Merging lists: x = x + y or x = x + [5, 7, 9] a list of the squares of the first ten natural numbers: x = [i * i for i in range(0, 10)] Accessing (and removing) the first element: z = x.pop(0) a list of the squares of [8, 19, 71, 101] : Accessing (and removing) the last element: z = x.pop() x = [i * i for i in [8, 19, 71, 101]] Accessing i th element: z = x[i] Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 8 / 24 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 9 / 24 List Comprehensions Exercise – List Comprehensions [ � Expression depending on variable i � for i in � list � ] Initialize a list that [ � Expression depending on variable i � for i in range( . . . ) ] contains all prime numbers between Filter 1 and 1000 [ � Expression depending on variable i � for i in � list � if � Condition � ] uses the function primetest and list comprehensions List of all numbers from [8, 60, 3, 19, 21] that are larger than 8: x = [i for i in [8, 60, 3, 19, 21] if i > 8] List of all numbers from [9, 6, 10, 19] that are divisible by 5: [ � Expression depending on variable i � for i in range( . . . ) if � Condition � ] y = [9, 6, 10, 19] x = [i for i in y if i % 5 == 0] Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 10 / 24 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 11 / 24

  5. List Comprehensions Accessing Elements from math import sqrt Accessing element at position 0: x[0] def primetest(x): if x < 2 or (x > 2 and x % 2 == 0): Accessing element at last position: x[len(x) - 1] return False Accessing element at last position: x[-1] d = 3 while d <= sqrt(x): if x % d == 0: Accessing sublist from positions 4 to 9: z = x[4:10] return False d += 2 Accessing sublist from position 5 z = x[5:] return True Accessing sublist to position 3 z = x[:4] y = [i for i in range(1001) if primetest(i)] Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 12 / 24 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 13 / 24 Namespaces So far Including own / existing modules Square root function from math from math import sqrt Namespaces from math import * Problem if different modules use same function names Use namespaces This gives content of module a unique name import math as mymath print(mymath.sqrt(9)) Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 14 / 24

  6. numpy and matplotlib Two modules are frequently used in a scientific context numpy and matplotlib They allow a functionality similar to MATLAB The Modules numpy and matplotlib numpy Calculations with vectors and matrices Numerical methods Documentation: https://numpy.org/doc/ matplotlib Data visualization (Plots) Documentation: https://matplotlib.org/contents.html Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 15 / 24 The Module numpy numpy builds the foundation for many other scientific modules The Module numpy Focus on efficient processing of large vectors and matrices It contains its own data structures, e.g., numpy arrays These work similar to Python lists numpy arrays are faster numpy arrays allow for more operations Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 16 / 24

  7. The Module numpy The Module numpy Large range of functions import numpy as np Linear algebra Convert Python list into numpy array import numpy as np import numpy.linalg as npla x = np.array([1, 3, 4]) This also works for more dimensions a = np.array([[5, 3, 0], [1, 2, 0], [0, 2, 11]]) b = np.array([4, 8, 1]) y = np.array([[1, 3, 4], [6, 8, 1], [0, 9, 4]]) x = npla.solve(a, b) numpy arrays can be added and multiplied Statistics print(x + y) Interpolation (e.g., least square method, Project 3) print(x * y) . . . Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 17 / 24 Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 18 / 24 The Module matplotlib Module to generate plots Data visualization Submodule matplotlib.pyplot allows usage analogously to MATLAB The Module matplotlib Data given by, e.g., Python lists or numpy arrays import numpy as np import matplotlib.pyplot as plt plt.plot([1, 4, 9, 16, 25]) plt.show() Digital Medicine I: Introduction to Programming – numpy and matplotlib Autumn 2019 Böckenhauer, Komm 19 / 24

Recommend


More recommend