Sorting 1 Bubblesort Hans-Joachim Bckenhauer and Dennis Komm - - PowerPoint PPT Presentation

sorting 1
SMART_READER_LITE
LIVE PREVIEW

Sorting 1 Bubblesort Hans-Joachim Bckenhauer and Dennis Komm - - PowerPoint PPT Presentation

Sorting 1 Bubblesort Hans-Joachim Bckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming Sorting 2 Autumn 2019 November 14, 2019 Bubblesort Bubblesort Idea Sorting by repeatedly finding the maximum Goal Sort list


slide-1
SLIDE 1

Hans-Joachim Böckenhauer and Dennis Komm

Digital Medicine I: Introduction to Programming

Sorting 2

Autumn 2019 – November 14, 2019

Sorting 1

Bubblesort Bubblesort

5 1 1 5 4 3 4 5 3 4 3 5

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 1 / 27

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

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 2 / 27

slide-2
SLIDE 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

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 3 / 27

One Bubble Sequence

One Bubble Sequence in Python

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

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 4 / 27

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

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 5 / 27

Bubblesort

def bubblesort(data): 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]))

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 6 / 27

slide-3
SLIDE 3

Sorting 1

Minsort 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

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 7 / 27

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

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 8 / 27

Sorting 1

Time Complexity of Bubblesort

slide-4
SLIDE 4

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 ➯ n−1

i=1 i = (n − 1) · n/2 = (n2 − n)/2 comparisons in total

➯ Quadratic number of comparisons

The time complexity of Bubblesort is in O(n2) With similar arguments, the time complexity of Minsort is in O(n2)

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 9 / 27

Time Complexity of Bubblesort

10 20 30 40 50 60 70 80 90 100 1,000 2,000 3,000 4,000 5,000

Bubblesort / Minsort Goal

Input length n Comparisons

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 10 / 27

Stacks and Queues

Stacks and Queues

So far access to arbitrary elements in lists by brackets Stack Last-In First-Out Elements can be inserted at the end Elements can be extracted from the same end Queue First-In First-Out Elements can be inserted at the end Elements can be extracted from the front

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 11 / 27

slide-5
SLIDE 5

Queues

Queue – Two Operations

append(x) inserts element x at last position pop(0) removes first element and returns it

In Python, lists can be used like queues

data = [1, 4, 5] data.append(8) data.pop(0) data.pop(0)

data = [1, 4, 5, 8] data = [5, 8]

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 12 / 27

Stacks

Stack – Two Operations

append(x) inserts element x at last position pop() removes last element and returns it

In Python, lists can also be used like stacks

data = [1, 4, 5] data.append(8) data.pop() data.pop()

data = [1, 4, 5, 8] data = [1, 4]

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 13 / 27

Stacks

Write function that gets a string that only consists of ( and ) returns True if the parentheses in the string are in a correct order and False

  • therwise

(A compiler should be able do that)

"()" and "()(())" is correct "(()" and "()(" is incorrect

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 14 / 27

Stacks

def checkpar(x): stack = [] for i in range(0, len(x)): if x[i] == "(": stack.append("(") else: if len(stack) == 0: return False else: stack.pop() if len(stack) == 0: return True else: return False

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 15 / 27

slide-6
SLIDE 6

Sorting 2

Mergesort Time Complexity of Bubblesort

10 20 30 40 50 60 70 80 90 100 1,000 2,000 3,000 4,000 5,000

Bubblesort / Minsort Goal

Input length n Comparisons

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 16 / 27

How Fast Can We Sort?

Idea Merging two sorted list is simple First sort small lists Merge them Repeat

➯ Divide and Conquer

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 17 / 27

Merging of Sorted Lists

1 3 4 2 4 7 1 2 3 4 4 7

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 18 / 27

slide-7
SLIDE 7

Merging of Sorted Lists

def merge(left, right): result = [] while len(left) > 0 and len(right) > 0: if left[0] > right[0]: result.append(right.pop(0)) else: result.append(left.pop(0)) return result + left + right While not both lists are empty Append the smaller

  • f both elements

One of the two given sorted lists may still contain elements

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 19 / 27

Mergesort

Divide and Conquer Iteratively merge sorted lists First merge “lists” of length 1 to lists of length 2 Merge lists of length 2 to lists of length 4 Merge lists of length 4 to lists of length 8 Merge lists of length 8 to lists of length 16 . . .

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 20 / 27

Mergesort

[8, 3, 1, 5, 6, 2, 4, 7] [[8], [3], [1], [5], [6], [2], [4], [7]] [[3, 8], [1, 5], [2, 6], [4, 7]] [[1, 3, 5, 8], [2, 4, 6, 7]] [[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7 8 3 1 5 6 2 4 7 3 8 1 5 2 6 4 7 1 3 5 8 2 4 6 7 1 2 3 4 5 6 7 8

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 21 / 27

Merge Step

Single Merge Step Get a 2-dimensional list, i.e., list that contains lists Each two successive lists are merged using the function merge() The last list is simply appended if there is an odd number of lists The result is again a 2-dimensional list that contains the merged lists

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 22 / 27

slide-8
SLIDE 8

Merge Step

def mergestep(data): result = [] while len(data) > 1: left = data.pop(0) right = data.pop(0) result.append(merge(left, right)) return result + data While there are still at least two lists Merge the first two lists If there is a list left at the end, append it

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 23 / 27

Mergesort – Complete Algorithm

Complete Algorithm Input is given as list data Convert every element into a list with one element This way get 2-dimensional list Apply mergestep() repeatedly to this list At the end, there will only be one element in the list This element corresponds to a sorted list

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 24 / 27

Mergesort – Complete Algorithm

def mergesort(data): result = [] for item in data: result.append([item]) while len(result) > 1: result = mergestep(result) return result[0]

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 25 / 27

Sorting 2

Time Complexity of Mergesort

slide-9
SLIDE 9

Time Complexity of Mergesort

Time complexity of Mergesort is proportional to Number of merge steps × Comparisons per merge step Length of sorted lists doubles with each merge step

➯ Roughly log2 n merge steps for n elements

In a merge step, one element is written into result with every comparison

➯ At most n comparisons per merge step

Time complexity of Mergesort is in O(n log2 n)

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 26 / 27

Time Complexity of Mergesort

10 20 30 40 50 60 70 80 90 100 1,000 2,000 3,000 4,000 5,000

Bubblesort / Minsort Mergesort

Input length n Comparisons

Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 27 / 27