October+28th+2016+by+David+Ye

Algorithms

 * 1) Sorting Algorithms
 * 2) Searching Algorithms

Data Structure

 * 1) Array
 * 2) Linked list
 * 3) Queue
 * 4) Stack
 * 5) Tree

Sorting Algorithms
1. Merge Sort Merge sort is a recursive algorithm that continually splits a list in half. The key point of it is "divide and conquer". Here's the code: code format="python" def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:]
 * = Complexity(N^2) ||= Complexity(N log N) ||
 * = Insertion Sort ||= Merge Sort ||
 * = Selection Sort ||= Quick Sort ||
 * = Bubble Sort ||=  ||

mergeSort(lefthalf) mergeSort(righthalf)

i=0 j=0 k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1

while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1

while j < len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1 print("Merging ",alist)

alist = [54,26,93,17,77,31,44,55,20] mergeSort(alist) print(alist) code 2. Quicksort Steps: 1.pivot 2.partition 3.Quicksort (1, pivot - 1) 4.Quicksort (Pivot + 1, end) 5.Quicksort list (only one element)-sorted, recursion Here's the code: code format="python" def quickSort(alist): quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last): if first= pivotvalue and rightmark >= leftmark: rightmark = rightmark -1

if rightmark < leftmark: done = True else: temp = alist[leftmark] alist[leftmark] = alist[rightmark] alist[rightmark] = temp

temp = alist[first] alist[first] = alist[rightmark] alist[rightmark] = temp

return rightmark

alist = [54,26,93,17,77,31,44,55,20] quickSort(alist) print(alist)

code Especially, quick sort is relatively low when processing a part-sorted list, but it is extremely good at sorting and completely random list.

3. Insertion Sort 1. a sorted list and the unsorted list 2. compare, make space and insert Here's the code: code format="python" def insertionSort(alist): for index in range(1,len(alist)):

currentvalue = alist[index] position = index

while position>0 and alist[position-1]>currentvalue: alist[position]=alist[position-1] position = position-1

alist[position]=currentvalue

alist = [54,26,93,17,77,31,44,55,20] insertionSort(alist) print(alist) code 4. Selection Sort 1. Compare 2. Swap Here's the code: code format="python" def selectionSort(alist): for fillslot in range(len(alist)-1,0,-1): positionOfMax=0 for location in range(1,fillslot+1): if alist[location]>alist[positionOfMax]: positionOfMax = location

temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp

alist = [54,26,93,17,77,31,44,55,20] selectionSort(alist) print(alist) code 5. Bubble Sorts 1. Two numbers 2. Compare ... 3. The larger to the bottom Here's the code: code format="python" def bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp

alist = [54,26,93,17,77,31,44,55,20] bubbleSort(alist) print(alist) code

2. Searching algorithms
1. linear search (waste of time) (not recommended) Here's the code: code format="python" code code format="python" def sequentialSearch(alist, item): pos = 0 found = False while pos < len(alist) and not found: if alist[pos] == item: found = True else: pos = pos+1 return found

testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0] print(sequentialSearch(testlist, 3)) print(sequentialSearch(testlist, 13)) code

2. Binary Search Complexity: log2(N) Key find the middle and cut the half we don't need Here's the code: code format="python" def binarySearch(alist, item): if len(alist) ==0 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)

testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,] print(binarySearch(testlist, 3)) print(binarySearch(testlist, 13)) code

3. Data Structure
<span style="color: #000000; font-family: 等线;">1.Array (direct access/ continuous/ difficult to insert/remove) (index and content) <span style="color: #000000; font-family: 等线;">Move every content downward by one

<span style="color: #000000; font-family: 等线;">2.Linked list <span style="color: #000000; font-family: 等线;">serial access <span style="color: #000000; font-family: 等线;">not continuous <span style="color: #000000; font-family: 等线;">ease to insert/ remove <span style="color: #000000; font-family: 等线;">head-node (data and reference)-tail <span style="color: #000000; font-family: 等线;">head and tail are references (Null) <span style="color: #000000; font-family: 等线;">Insertion: <span style="color: #000000; font-family: 等线;">1.new node <span style="color: #000000; font-family: 等线;">2.new link <span style="color: #000000; font-family: 等线;">3.change link

<span style="color: #000000; font-family: 等线;">3.Stack <span style="color: #000000; font-family: 等线;">First in last out <span style="color: #000000; font-family: 等线;">not continuous <span style="color: #000000; font-family: 等线;">Push (insert) and Pop (remove) (check if the stack is already full or empty) <span style="color: #000000; font-family: 等线;">Pointer (top-of-stack-pointer) <span style="color: #000000; font-family: 等线;">Push and Pop: move the pointer <span style="color: #000000; font-family: 等线;">Application: <span style="color: #000000; font-family: 等线;">function main <span style="color: #000000; font-family: 等线;">Function1 <span style="color: #000000; font-family: 等线;">Function2 <span style="color: #000000; font-family: 等线;">Function3 <span style="color: #000000; font-family: 等线;">This is the process called "Call stack" <span style="color: #000000; font-family: 等线;">Inversion of a list

<span style="color: #000000; font-family: 等线;">4.Queue <span style="color: #000000; font-family: 等线;">First in first out <span style="color: #000000; font-family: 等线;">Tail-in, head-out <span style="color: #000000; font-family: 等线;">ENQUEUE-tail-pointer (move to the next), DEQUEUE-head pointer (move to the next)

<span style="color: #000000; font-family: 等线;">5.Tree: (binary tree- left and right) <span style="color: #000000; font-family: 等线;">Root (node with one more reference), Internal node (has parent and children), leaf node (with parent but no children)

<span style="color: #000000; font-family: 等线;">Binary Tree <span style="color: #000000; font-family: 等线;">Left and right <span style="color: #000000; font-family: 等线;">5 <span style="font-family: 等线;">17 <span style="font-family: 等线;"> 36 <span style="color: #000000; font-family: 等线;">Build a tree – insertion