search and sorting algorithms
play

Search and Sorting Algorithms Ali Taheri Sharif University of - PowerPoint PPT Presentation

Fu Fundamentals of Pr Programming (Py Python) Search and Sorting Algorithms Ali Taheri Sharif University of Technology Spring 2019 Some slides have been adapted from Python Programming: An Introduction to Computer Science Outline 1.


  1. Fu Fundamentals of Pr Programming (Py Python) Search and Sorting Algorithms Ali Taheri Sharif University of Technology Spring 2019 Some slides have been adapted from β€œPython Programming: An Introduction to Computer Science”

  2. Outline 1. Searching 2. Linear Search 3. Binary Search 4. Sorting 5. Selection Sort 6. Merge Sort 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  3. Searching Searching is the process of looking for a particular value in a collection. We can test to see if a value appears in a sequence using in operator or index method. nums = [3, 1, 4, 2, 5] x = int(input()) if x in nums: # do something i = nums.index(x) 3 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  4. Linear Search Python searches through the list of items one by one until the target value is found This strategy is called Linear Search def search(x, nums): for i in range(len(nums)): if nums[i] == x: # item found, return the index value return i return -1 # loop finished, item was not in list In the worst case, the number of comparisons has a linear relationship with the number of elements 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  5. Binary Search If the data is sorted in ascending order (lowest to highest), we can skip checking some of the data Each time we check the middle item to try to narrow down the range If the middle item is greater than the value to be found, the lower half of the list is searched Otherwise, we search the upper half This strategy is called Binary Search 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  6. Binary Search The Binary Search algorithm follows the Divide and Conquer paradigm. def binary_search(x, num, low, high): if low >= high: return -1 mid = (low + high) // 2 if num[mid] == x: return mid elif num[mid] > x: return binary_search(x, num, low, mid) else : return binary_search(x, num, mid+1, high) 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  7. Binary Search Iterative Binary Search algorithm: def binary_search(x, nums): low = 0 high = len(nums) while low < high: mid = (low + high) // 2 if num[mid] == x: return mid elif x < nums[mid]: high = mid - 1 else : low = mid + 1 return -1 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  8. Binary Search In the worst case, the number of comparisons made by binary search has a logarithmic relationship with the number of elements If π‘œ is the number of elements and 𝑔(π‘œ) is the number of comparisons, for binary search we have: π‘œ 𝑔 π‘œ = 1 + 𝑔 , 𝑔 1 = 1 2 By solving the above equation, we get: 𝑔 π‘œ β‰ˆ log 2 π‘œ 8 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  9. Sorting Sorting problem is to take a list and rearrange it so that the values are in non-decreasing order. In Python, we can use the sort() method of a list to make it in order. >>> nums = [3, 1, 4, 2, 5] >>> nums.sort() >>> nums [1, 2, 3, 4, 5] 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  10. Selection Sort One simple method is to look through the list to find the smallest value and place that value at the front of the list. We can then sort the remaining values recursively This strategy is called Selection Sort 10 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  11. Selection Sort Selection Sort Algorithm: def selection_sort(nums, begin): # sort nums into ascending order n = len(nums) # Base Case if begin == n - 1: return # find the smallest item in nums[begin]..nums[n-1] mp = begin # begin is smallest initially for i in range(begin + 1, n): # look at each position if nums[i] < nums[mp]: # this one is smaller mp = i # remember its index # swap smallest item to the begin nums[begin], nums[mp] = nums[mp], nums[begin] selection_sort(nums, begin + 1) 11 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  12. Selection Sort In total, the number of comparisons made by Selection Sort has a quadratic relationship with the number of elements If π‘œ is the number of elements and 𝑔(π‘œ) is the number of comparisons, for binary search we have: 𝑔 π‘œ = (π‘œ βˆ’ 1) + 𝑔 π‘œ βˆ’ 1 , 𝑔 0 = 0 By solving the above equation, we get: 𝑔 π‘œ β‰ˆ π‘œ 2 12 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  13. Merge Sort Divide and Conquer can expedite the process of sorting. The solution is to split the list into two parts and then sort each part recursively. Then, merge two sorted parts to have the entire list sorted. This strategy is called Merge Sort 13 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  14. Merge Sort If we know how to merge, we have: def merge_sort(nums): n = len(nums) if n <= 1: return mid = n // 2 lower = nums[:mid] upper = nums[mid:] merge_sort(lower) merge_sort(upper) merge(lower, upper, nums) 14 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  15. Merge Sort The merge() function combines two sorted lists def merge(lower, upper, nums): nums.clear() i, j = 0, 0 while i < len(lower) and j < len(upper): if lower[i] <= upper[j]: nums.append(lower[i]) i += 1 else : nums.append(upper[j]) j += 1 while i < len(lower): nums.append(lower[i]) i += 1 while j < len(upper): nums.append(upper[j]) j += 1 15 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  16. Merge Sort In total, the number of comparisons made by Merge Sort has a linear-logarithmic relationship with the number of elements If π‘œ is the number of elements and 𝑔(π‘œ) is the number of comparisons, for binary search we have: π‘œ 𝑔 π‘œ = π‘œ + 2𝑔 , 𝑔 1 = 0 2 By solving the above equation, we get: 𝑔 π‘œ β‰ˆ π‘œ π‘šπ‘π‘• π‘œ 16 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

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