0% found this document useful (0 votes)
9 views

Lecture 10 - Searching and Sorting

Uploaded by

milo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 10 - Searching and Sorting

Uploaded by

milo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ENG 202: Computers and Engineering

Object Oriented Programming in PYTHON


LECTURE 10 – Data Searching and Sorting in PYTHON

Maurice J. KHABBAZ, Ph.D.


Search Algorithms
• Approach for finding one or multiple items with specific properties
within a collection of items.
• Item collection could be implicit:
• Example: find the square root of a number (formulated as a search problem)
• Exhaustive Enumeration.
• Bisection Search.
• Newton-Raphson.
• Item collection could be explicit:
• Example: find student record in a stored database of records.

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

Applied on an Unsorted List Applied on a Sorted List


def LinearSearch_SL (L, e):
def LinearSearch_UL (L, e): for i in range(len(L)):
for i in range(len(L)): if L[i] == e:
if e == L[i]: return True
return True if L[i] > e:
return False return False
return False

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

• Principle: def bogoSort(L):


• To sort a deck of cards: while not is_sorted(L):
random.shuffle(L)
• Throw them in the air.
• Pick them up.
• Repeat as long as the picked cards are not sorted.

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:

1. If list has length 0 or 1 ⇒ already sorted.

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.

3. Merge sorted sublists:


1. Look at first element of each, move smaller to end of the result.
2. When one list is empty, just copy the rest of other list.
• Mainly merge sublists such that they will be sorted after the merge is complete.

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

Left Sublist Right Sublist


8 4 1 6 5 9 2 0
Merge Merge
1 4 6 8 0 2 5 9
Left Sublist Right Sublist Left Sublist Right Sublist
8 4 1 6 5 9 2 0
Merge Merge Merge Merge
4 8 1 6 5 9 0 2

Base Base Base Base Base Base Base Base


Case Case Case Case Case Case Case Case
8 4 1 6 5 9 2 0
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 15
Saturday, December 14, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 16

You might also like