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

β–Ά
search and sorting algorithms
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 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”

slide-2
SLIDE 2

Outline

  • 1. Searching
  • 2. Linear Search
  • 3. Binary Search
  • 4. Sorting
  • 5. Selection Sort
  • 6. Merge Sort

2

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-3
SLIDE 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.

3

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

nums = [3, 1, 4, 2, 5] x = int(input()) if x in nums: # do something i = nums.index(x)

slide-4
SLIDE 4

Linear Search

Python searches through the list of items one by

  • ne until the target value is found

This strategy is called Linear Search In the worst case, the number of comparisons has a linear relationship with the number of elements

4

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-6
SLIDE 6

Binary Search

The Binary Search algorithm follows the Divide and Conquer paradigm.

6

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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)

slide-7
SLIDE 7

Binary Search

Iterative Binary Search algorithm:

7

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

slide-8
SLIDE 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 + 𝑔

π‘œ 2

, 𝑔 1 = 1

By solving the above equation, we get:

𝑔 π‘œ β‰ˆ log2 π‘œ

8

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-9
SLIDE 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.

9

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> nums = [3, 1, 4, 2, 5] >>> nums.sort() >>> nums [1, 2, 3, 4, 5]

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-11
SLIDE 11

Selection Sort

Selection Sort Algorithm:

11

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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)

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-14
SLIDE 14

Merge Sort

If we know how to merge, we have:

14

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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)

slide-15
SLIDE 15

Merge Sort

The merge() function combines two sorted lists

15

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

slide-16
SLIDE 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𝑔

π‘œ 2

, 𝑔 1 = 0

By solving the above equation, we get:

𝑔 π‘œ β‰ˆ π‘œ π‘šπ‘π‘• π‘œ

16

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019