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. Searching 2. Linear Search 3. Binary Search 4. Sorting 5. Selection Sort 6. Merge Sort 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
Recommend
More recommend