Python Algorithm Study - Week 7, 8
2017-08-23
Problem Solving with Algorithms and Data Structures using Python ์ฑ ์ ์ฝ์ผ๋ฉฐ ์คํฐ๋ํ ๋ด์ฉ์ ์ ๋ฆฌํ์ฌ ์ฐ์ฌํฉ๋๋ค.
Case | Best Case | Worst Case | Average Case |
---|---|---|---|
item is present | 1 | n | n/2 |
item is not present | n | n | n |
Case | Best Case | Worst Case | Average Case |
---|---|---|---|
item is present | 1 | n | n/2 |
item is not present | 1 | n | n/2 |
O(logn)
์ด๋ค. Sequential search๋ณด๋ค ์๊ฐ๋ณต์ก๋๋ ๋ฎ์ง๋ง ๋ฌด์กฐ๊ฑด '์ ๋ ฌ'์ ๊ฑฐ์ณ์ผ ํ๊ธฐ ๋๋ฌธ์ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋น์ฉ์ด ๋ ๋ค์๋ ์๋ค. (If we can sort once and then search many times, the cost of the sort is not so significant. However, for large lists, sorting even once can be so expensive that simply performing a sequential search from the start may be the best choice.)# Recursive binary search
def binarySearch(alist, item):
if len(alist) == 0:
return False
else:
midpoint = len(alist)//2
if alist[midpoint]==item:
return True
else:
if item<alist[midpoint]:
return binarySearch(alist[:midpoint],item)
else:
return binarySearch(alist[midpoint+1:],item)
Hashing์ด๋?
O(1)
๊ณผ ๊ฐ์ฅ ๊ฐ๊น๊ฒ ๋ง๋ค์ด ๊ฒ์์๋๋ฅผ ๋น ๋ฅด๊ฒ ํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ด๋, ์์ ํ O(1)
์ ๋ถ๊ฐ๋ฅํ๋ค.SHA
(Secure Hash Algorithm)์ MD5
๊ฐ์ ์ํธํ์๋ ๊ด๋ จ์๋ค. (์ถ์ฒ)Hash table / Slot / Hash function / Load factor
Hash table
์ ๋ฐ์ดํฐ๋ค์ ๋ชจ์์ด๋ฉฐ ์ผ์ ํ ํฌ๊ธฐ(m)๋ฅผ ๊ฐ์ง๋ค.slot
์ด๋ผ๊ณ ํ๋ฉฐ slot์ 0๋ถํฐ (m-1)๊น์ง ์ธ๋ฑ์ค๋ฅผ ๊ฐ๋๋ค.hash function
์ด๋ผ๊ณ ํ๋ค.load factor
(๋ถํ๊ณ์)๋ผ๊ณ ํ๋ค. ํ
์ด๋ธ ํฌ๊ธฐ๊ฐ 11์ด๊ณ ๋ฐ์ดํฐ๊ฐ ์ฑ์์ง slot์ด 6๊ฐ๋ผ๋ฉด load factor(ฮป)๋ 6/11์ด๋ค.Perfect hash function
์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ๊ทธ๋ฌ๋ ์์์ ๋ฐ์ดํฐ๋ค์ ๋ชจ์์ด ์์ ๋, ์๋ฒฝํ ํด์ ํจ์๋ฅผ ๋ง๋๋ ๊ฒ์ ๋ฐ์ดํฐ ์ซ์๋งํผ ํด์ ํ
์ด๋ธ์ ์ซ์๋ฅผ ๋๋ฆฌ์ง ์๋ ์ด์ ๋ถ๊ฐ๋ฅํ๋ค.Perfect hash function
์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ๊ทธ๋ฌ๋ ์์์ ๋ฐ์ดํฐ๋ค์ ๋ชจ์์ด ์์ ๋, ์๋ฒฝํ ํด์ ํจ์๋ฅผ ๋ง๋๋ ๊ฒ์ ๋ฐ์ดํฐ ์ซ์๋งํผ ํด์ ํ
์ด๋ธ์ ์ซ์๋ฅผ ๋๋ฆฌ์ง ์๋ ์ด์ ๋ถ๊ฐ๋ฅํ๋ค.Collision Resolution (์ถฉ๋์ด ๋ฐ์ํ ๋ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ)
rehashing
์ด๋ผ๊ณ ๋ถ๋ฅด๊ธฐ๋ ํ๋ค.Map
์๋ฃ๊ตฌ์กฐ์ ๊ฐ๋ค.# Open addressing ๋ฐฉ์์ Hash๋ก Map ๋ฐ์ดํฐํ์
๋ง๋ค๊ธฐ
class HashTable:
def __init__(self):
self.size = 11
self.slots = [None] * self.size
self.data = [None] * self.size
def put(self,key,data):
hashvalue = self.hashfunction(key,len(self.slots))
if self.slots[hashvalue] == None:
self.slots[hashvalue] = key
self.data[hashvalue] = data
else:
if self.slots[hashvalue] == key:
self.data[hashvalue] = data #replace
else: # Open addressing ๋ฐฉ๋ฒ
nextslot = self.rehash(hashvalue,len(self.slots))
while self.slots[nextslot] != None and self.slots[nextslot] != key:
nextslot = self.rehash(nextslot,len(self.slots))
if self.slots[nextslot] == None:
self.slots[nextslot]=key
self.data[nextslot]=data
else:
self.data[nextslot] = data #replace
def hashfunction(self,key,size):
return key%size
def rehash(self,oldhash,size):
return (oldhash+1)%size
def get(self,key):
startslot = self.hashfunction(key,len(self.slots))
data = None
stop = False
found = False
position = startslot
while self.slots[position] != None and not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
position=self.rehash(position,len(self.slots))
if position == startslot:
stop = True
return data
def __getitem__(self,key):
return self.get(key)
def __setitem__(self,key,data):
self.put(key,data)
The Bubble Sort
def bubble_sort(list):
for passnum in range(len(list)-1, 0, -1):
for i in range(passnum):
if list[i] > list[i+1]:
list[i], list[i+1] = list[i+1], list[i]
The Selection Sort
The Insertion Sort
The Shell Sort
The Merge Sort ์ฌ๊ท
The Quick Sort ์ฌ๊ท