sorting 1
play

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


  1. Sorting 1 Bubblesort Hans-Joachim Böckenhauer 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 data with n elements, i.e., range 0 , . . . , n − 1 1 5 1 5 3 4 4 5 3 4 5 3 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 1 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 2 / 27

  2. Exercise – One Bubble Sequence One Bubble Sequence One Bubble Sequence in Python Implement one Bubble Sequence data = [6, 22, 61, 1, 89, 31, 9, 10, 76] Run through data one time n = len(data) Compare neighboring elements for i in range(0, n-1): Swap if the first element is larger if data[i] > data[i+1]: tmp = data[i] Maximum bubbles to the right data[i] = data[i+1] data[i+1] = tmp Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 3 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 4 / 27 Exercise – Bubblesort Bubblesort def bubblesort(data): Implement the complete Algorithm n = len(data) for d in range(n, 1, -1): Iterate bubble sequences for i in range(0, d-1): After i th sequence, the last k if data[i] > data[i+1]: elements of data are sorted tmp = data[i] data[i] = data[i+1] Bubble sequences become shorter data[i+1] = tmp with each iteration return data To this end, use outer loop print(bubblesort([6, 22, 61, 1, 89, 31, 9, 10, 76])) Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 5 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 6 / 27

  3. Minsort Idea Sorting by repeatedly finding the minimum Sorting 1 Unlike Bubblesort, we do not compare neighboring elements Minsort 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): Sorting 1 minimum = data[current] for i in range(current+1, n): Time Complexity of Bubblesort 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

  4. Time Complexity of Bubblesort Time Complexity of Bubblesort Count comparisons of two numbers 5 , 000 n − 1 comparisons to find maximum 4 , 000 n − 2 comparisons to find second largest element Comparisons . . . Bubblesort / Minsort 3 , 000 1 comparison to find smallest element 2 , 000 i =1 i = ( n − 1) · n/ 2 = ( n 2 − n ) / 2 comparisons in total ➯ � n − 1 Goal ➯ Quadratic number of comparisons 1 , 000 The time complexity of Bubblesort is in O ( n 2 ) 0 10 20 30 40 50 60 70 80 90 100 With similar arguments, the time complexity of Minsort is in O ( n 2 ) Input length n Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 9 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 10 / 27 Stacks and Queues So far access to arbitrary elements in lists by brackets Stack Stacks and Queues 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

  5. Queues Stacks Queue – Two Operations Stack – Two Operations append(x) inserts element x at last position append(x) inserts element x at last position pop(0) removes first element and returns it pop() removes last element and returns it In Python, lists can be used like queues In Python, lists can also be used like stacks data = [1, 4, 5] data = [1, 4, 5] data.append(8) data.append(8) data = [1, 4, 5, 8] data = [1, 4, 5, 8] data.pop(0) data.pop() data.pop(0) data = [5, 8] data.pop() data = [1, 4] Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 12 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 13 / 27 Stacks Stacks Write function that def checkpar(x): gets a string stack = [] for i in range(0, len(x)): that only consists of ( and ) if x[i] == "(": stack.append("(") returns True if the parentheses in the string are in a correct order and False else: otherwise if len(stack) == 0: return False (A compiler should be able do that) else: stack.pop() if len(stack) == 0: "()" and "()(())" is correct return True else: "(()" and "()(" is incorrect return False Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 14 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 15 / 27

  6. Time Complexity of Bubblesort 5 , 000 4 , 000 Sorting 2 Comparisons Bubblesort / Minsort 3 , 000 Mergesort 2 , 000 Goal 1 , 000 0 10 20 30 40 50 60 70 80 90 100 Input length n Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 16 / 27 How Fast Can We Sort? Merging of Sorted Lists Idea 1 3 4 2 4 7 Merging two sorted list is simple First sort small lists Merge them 1 2 3 4 4 7 Repeat ➯ Divide and Conquer Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 17 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 18 / 27

  7. Merging of Sorted Lists Mergesort Divide and Conquer def merge(left, right): Iteratively merge sorted lists result = [] While not both while len(left) > 0 and len(right) > 0: lists are empty if left[0] > right[0]: First merge “lists” of length 1 to lists of length 2 One of the two given result.append(right.pop(0)) Append the smaller Merge lists of length 2 to lists of length 4 sorted lists may still else: of both elements Merge lists of length 4 to lists of length 8 contain elements result.append(left.pop(0)) return result + left + right Merge lists of length 8 to lists of length 16 . . . Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 19 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 20 / 27 Mergesort Merge Step 8 8 3 3 1 1 5 5 6 6 2 2 4 4 7 7 Single Merge Step 3 8 1 5 2 6 4 7 Get a 2-dimensional list, i.e., list that contains lists Each two successive lists are merged using the function merge() 1 3 5 8 2 4 6 7 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 1 2 3 4 5 6 7 8 [[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] Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 21 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 22 / 27

  8. Merge Step Mergesort – Complete Algorithm Complete Algorithm def mergestep(data): Input is given as list data result = [] While there are still Convert every element into a list with one element while len(data) > 1: at least two lists left = data.pop(0) If there is a list left This way get 2-dimensional list Merge the first right = data.pop(0) at the end, append it Apply mergestep() repeatedly to this list two lists result.append(merge(left, right)) At the end, there will only be one element in the list return result + data This element corresponds to a sorted list Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 23 / 27 Digital Medicine I: Introduction to Programming – Sorting 2 Autumn 2019 Böckenhauer, Komm 24 / 27 Mergesort – Complete Algorithm def mergesort(data): Sorting 2 result = [] for item in data: Time Complexity of Mergesort 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend