Lecture 10 - Searching and Sorting
Lecture 10 - Searching and Sorting
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 2
Searching Algorithms
• Linear Search:
• Brute Force search (a.k.a. British Museum algorithm).
• List of items does not have to be sorted.
• Bisection Search:
• List of items must be sorted to yield a correct answer.
• We have already seen two different implementations of this algorithm:
• Iterative implementation.
• Recursive implementation.
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 3
Linear Search: Recall
must look through the entire L must look through L until finding
to decide that e is not there a number that is greater than e
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 4
Bisection Search: Recall
• Step-by-Step:
1. Pick an index, i, that divides the list L in half.
2. Check if L[i] == e.
3. If not, check whether L[i] is larger or smaller than e.
4. Search:
• Left of list L if e < L[i].
• Right of list L if e > L[i].
• New variant of the divide-and-conquer principle:
• Break the search space into smaller ones and simply search in smaller spaces.
• Result of smaller space search is the result for the original larger search.
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 5
Bisection Search: Recall Implementation
def biSrch(L, e):
def biSrchHlpr(L, e, low, high):
if high == low: return L[low] == e
mid = (low + high) // 2
if L[mid] == e: return True
elif L[mid] > e:
if low == mid: return False #nothing left to search
else: return biSrchHlpr(L, e, low, mid - 1)
else:
return biSrchHlpr(L, e, mid + 1, high)
if len(L) == 0: return False
else: return biSrchHlpr(L, e, 0, len(L) – 1)
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 6
Sorting Algorithms
• Objective:
• Efficiently sort a list of entries (typically numbers).
• Approaches:
• Investigate a range of techniques.
• One of them is quite efficient.
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 7
Monkey Sort
• Also known as:
• Bogo Sort, Stupid Sort, Slow Sort, Permutation Sort, Shotgun Sort
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 8
Bubble Sort
• Principle:
• Compare consecutive pairs of elements.
• Swap elements in a pair such that smaller is first.
• When the end of the list is reached, start over again.
• Stop only when no swaps are made throughout a pass over the list.
• Largest unsorted element will always be at the end after a pass.
def bubbleSort(L):
swap = False
while not swap:
swap = True
for j in range (1, len(L):
if L[j – 1] > L[j]:
swap = False
(L[j–1],L[j])=(L[j],L[j–1])
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 9
Selection Sort
def selectionSort(L):
suffix = 0
• First Step: while suffix != len(L):
for i in range (suffix, len(L)):
• Extract minimum. if L[i] < L[suffix]:
• Swap it with element at index 0. (L[suffix],L[i])=(L[i],L[suffix]
• Subsequent Step:
• In remaining sublist, extract minimum.
• Swap it with element at index 1.
• Keep left portion of list sorted:
• At ith step, first i elements in L are sorted.
• All other elements are bigger than first i elements.
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 10
Merge Sort
• Use divide-and-conquer approach:
2. If list has length > 1, split into two lists and sort each list:
• Typically keeps on splitting in half until sublists have only one element.
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 11
Merge Sort: Example
List 1 List 2 Compare Result
1, 5, 12, 18, 19, 20 2, 3, 4, 17 1, 2 Empty
5, 12, 18, 19, 20 2, 3, 4, 17 5, 2 1
5, 12, 18, 19, 20 3, 4, 17 5, 3 1, 2
5, 12, 18, 19, 20 4, 17 5, 4 1, 2, 3
5, 12, 18, 19, 20 17 5, 17 1, 2, 3, 4
12, 18, 19, 20 17 12, 17 1, 2, 3, 4, 5
18, 19, 20 17 17, 18 1, 2, 3, 4, 5, 12
18, 19, 20 Empty No Comparison 1, 2, 3, 4, 5, 17, 18
19, 20 Empty No Comparison 1, 2, 3, 4, 5, 17, 18, 19
20 Empty No Comparison 1, 2, 3, 4, 5, 17, 18, 19, 20
Empty Empty No Comparison 1, 2, 3, 4, 5, 17, 18, 19, 20
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 12
Merge Sort: Code for Merging Two Lists
left and right sublists are ordered
def merge(left, right):
result = []; (i, j)=(0, 0)
while i < len(left) and j < len(right):
if left[i] < right[j]: move indices for sublists depending
result.append(left[i]); i += 1 on which sublist holds the next
smallest element
else:
result.append(right[j]); j += 1 when right sublist is empty,
while i < len(left): append the remaining left sublist’s
result.append(left[i]); i += 1 elements to result
while j < len(right): when left sublist is empty, append
the remaining right sublist’s
result.append(right[j]); j += 1 elements to result
return result
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 13
Merge Sort: Recursive Code Implementation
def mergeSort(L):
if len(L) < 2: return L[:]
else:
middle = len(L) // 2
left = mergeSort(L[:middle])
right = mergeSort(L[middle:])
return merge(left, right)
• Divide list successively into halves.
• Depth-first such that conquer smallest pieces down one branch first.
• Move to larger pieces next.
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 14
Recursive Merge Sort: Example
Original List
8 4 1 6 5 9 2 0
Merge
0 1 2 4 5 6 8 9