- Object Oriented Programming
- A different way of thinking about programming
- Classes
- Objects
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
31/05/2023
def lin_search(L, e): """Assumes L is a list. Returns True if e is in L and False otherwise"""
e in L
?def lin_search(L, e): """Assumes L is a list. Returns True if e is in L and False otherwise""" for i in range(len(L)): if L[i] == e: return True return False
len(L)
timeslen(L)
times
len(L)
-> Size of input -> linear time algorithmli = [1,3,4,5,7,12,17,18,19,24,32,33,35,40]
len(li)
## 14
items in the list
li[len(li) // 2]
## 18
li = li[0:(len(li)//2)] li
## [1, 3, 4, 5, 7, 12, 17]
li[len(li) // 2]
## 5
li = li[(len(li)//2+1):len(li)] li
## [7, 12, 17]
li[len(li) // 2]
## 12
li = li[0:(len(li)//2)] li
## [7]
li[len(li) // 2]
## 7
def bsearch(L, e, start, end): # Search e in L[start:end] if start == end: return L[start] == e: else:
def bsearch(L, e, start, end): # Search e in L[start:end] if start == end: return L[start] == e: else: middle = (start+end)//2 # middle item of the list if L[middle] == e: return True ...
def bsearch(L, e, start, end): # Search e in L[start:end] if start == end: return L[start] == e else: middle = (start+end)//2 # middle item of the list if L[middle] == e: return True elif e < L[middle]: return bsearch(L, e, start, middle) # keep searching in the left half else: return bsearch(L, e, middle + 1, end) # keep searching in the right half
L = [1,3,4,5,7,12,17,18,19,24,32,33,35,40] bsearch(L, 7, 0, len(L)-1)
## True
bsearch(L, 1, 0, len(L)-1)
## True
bsearch(L, 40, 0, len(L)-1)
## True
bsearch(L, 0, 0, len(L)-1)
## False
bsearch(L, 50, 0, len(L)-1)
## False
bsearch(L, 9, 0, len(L)-1)
## False
bsearch(L, 7, 0, len(L) - 1)
def bin_search(li, it): return bsearch(li, it, 0, len(L) - 1) bin_search(L, 7)
## True
bin_search(L, 45)
## False
bin_search
really faster than lin_search
?
import time from random import gauss li = [gauss(0,1) for i in range(1000000)] # Create a list of one million random numbers li.sort() # sort the list start = time.process_time() # mark the start of lin_search for i in range(1, 20): # search for 20 different numbers res = lin_search(li, li[50000*i]) lin_elapsed = time.process_time() - start # mark the end start = time.process_time() # do the same for bin_search for i in range(1, 20): res = bin_search(li, li[50000*i]) bin_elapsed = time.process_time() - start print("Time spent in linear search:", lin_elapsed) # print the results
## Time spent in linear search: 1.25
print("Time spent in binary search:", bin_elapsed)
## Time spent in binary search: 0.0
L = prefix + suffix
prefix = L[0:i]
suffix = L[i:len(L)]
def selSort(L): """Assumes that L is a list of elements that can be compared using >. Sorts L in ascending order""" suffixStart = 0 while suffixStart != len(L): #look at each element in suffix for i in range(suffixStart, len(L)): if L[i] < L[suffixStart]: #swap position of elements L[suffixStart], L[i] = L[i], L[suffixStart] suffixStart += 1 li = [9,8,7,6,5,4,3,2,1] selSort(li) li
## [1, 2, 3, 4, 5, 6, 7, 8, 9]
li = [i for i in range(10000, 1, -1)] li2 = li.copy() start = time.process_time() selSort(li) elapsed = time.process_time() - start print(elapsed)
## 4.515625
start = time.process_time() li2.sort() elapsed = time.process_time() - start print(elapsed)
## 0.0