PSEUDOCODE Code
Pseudocode
=
<-
==
=
Array start from 0
Array start from 1
For i in range 20 NONE
For i<-1 to length[20] For i<-20 Downto length[1] NIL
*, /, +, -
MULT, DIV, ADD, SUBT
%
MOD
#
//
- capitalise function name, including parameters - code blocks are indented
BIG O
Used to describe the performance or complexity of an algorithm O(1), O(log n), O(n), O(n log n), O(n^2), O(n^3)
RECURSION
-
Programming technique where a function calls itself Must have a condition to stop (base case).
1. FACTORIAL(n) 2. IF n < 2 3. RETURN 1 4. ELSE 5. RETURN FACTORIAL(n-1) * n 1. SUM_DIGITS(n) #343 = 10 2. IF n == 0 3. return 0 4. ELSE 5. return (n/10) + n % 10 1. COUNT_X(string): #'yyyxxyy' = 2 if string == '' 2. '': : return 0 3. 4. 5. temp = string[-1] if temp == 'x' 6. 'x': : return COUNT_X (string[:-1]) + 1 7. return COUNT_X (string[:-1]) + 0 8. 1. def REV_STRING(s): REV_STRING(s): if s == "": 2. return s 3. else : 4. return REV_STRING(s[1:]) + s[0] 5.
DATA STRUCTURES
Data structures are constructions for storing and organising data. Ie. Arrays, Stacks, Queues, Trees.. STACKS LIFO – Last In First Out -
Items added to the top (push – append()) Also removed from the top (pop – pop(-1))
QUEUES FIFO – First In First Out -
Items added to the top (push – append()) Removed from the bottom (pop – pop(0))
LINKED LISTS An ordered set of data elements, each containing a link to its successor (and sometimes its predecessor – doubly-linked list). -
First element is the ‘head’
Last element points to null
Has these attributes -
N.value – the item in the node N.previous – pointer to the previous node N.next – pointer to the next node L.head – first node L.tail – last node
TREES
Tree represents the nodes connected by edges. Root – top most node Parent – any node which has a node under it. Child – the node below a given node Leaf – node with no child Traversing – passing through nodes
-
Good for speed (of navigation of data). Logarithmic time.
BINARY SEARCH TREE -
-
Nodes left child must have value less than parent, and right child have value greater than parent. Attributes; N.value – value of the node o N.parent – pointer to the nodes parent o N.left – pointer to the nodes left child o N.right – pointer to the nodes right child o Used to quickly find data. Insertion Is O(logN)
Insertion ALGORITHM If root If root is is NULL NULL then create then create root node return If root If root exists then compare the data with node with node. .data while until until insertion insertion position is located If data If data is is greater greater than node .data goto right goto right subtree else goto left goto left subtree endwhile insert data
PYTHON 1. def insert(tree, data): 2. if tree.value == NONE: 3. tree=BinTreeNode(data) 4. elif tree.value > data: 5. 6. tree.leftChild: if tree.leftChild: 7. tree.leftChild.insert( return tree.leftChild.insert( data) else: 8. 9. tree.leftChild tree.leftChild = Node(data) 10. 11. else: 12. tree.rightChild: if tree.rightChild: 13. tree.rightChild.insert return tree.rightChild.insert (data) else: 14. 15. tree.rightChild tree.rightChild = Node(data)
end If
Finding a node RECURSIVELY 1. BIN-TREE-FIND(tree,target) 2. IF tree.value = target OR tree = 0: 3. RETURN tree 4. ELSE IF target < tree.value 5. RETURN BIN-TREEFIND(tree.left, target) 6. ELSE 7. RETURN BIN-TREEFIND(tree.right, target) 8. RETURN 0
ITERATIVELY 9. BIN-TREE-FIND(tree,target) 10. R ← tree 11. WHILE r ≠ Ø 12. IF r.value = target 13. RETURN r ( or TRUE) 14. ELSE IF r.value > target 15. r ← r.left 16. ELSE 17. r ← r.right 18. RETURN FALSE
Traversal: 1. Pre-order NLR - output item, then follow left tree, then right tree. 2. Post-order LRN - follow the left child, follow the right child, output node value. 4. In-order LNR - for each node, display the left-hand side, then the node itself, then the right. When displaying the left or right, follow the same instructions.
PRE-ORDER
IN-ORDER
POST-ORDER
1. PREORDER(tree): 2. PRINT tree.value 3. IF tree.left ≠ 0: 4. PREORDER (tree.left) 5. IF tree.right ≠ 0: 6. PREORDER (tree.right)
7. INORDER(tree): 8. IF tree.left ≠ 0: 9. INORDER(tree.left) 10. PRINT tree.value 11. IF tree.right ≠ 0: 12. INORDER(tree.right)
13. POSTORDER(tree): 14. IF tree.left ≠ 0: 15. POSTORDER (tree.left) 16. IF tree.right ≠ 0: 17. POSTORDER (tree.right) 18. PRINT tree.value
GRAPH TRAVERSAL
BFS
1. def bfs(self, start): 2. queue = [start] #start value as first node 3. visited = [] #list of the visited nodes 4. 5. while queue: #while queue has a value 6. cur_node = queue.pop(0) #start working with the first node 7. for neighbour in self.neighbours[cur_nod 8. self.neighbours[cur_node]: e]: #for all neighbours of the node 9. 10. 11. 12. 13.
DFS
if neighbour not in visited: queue.append(neighbour) #append to queue if not visited
visited.append(cur_node) #append current node to visited return visited
Implement exactly the same same but with a stack stack ( stack.pop() )
SORTS INSERTION SORT
-
Pick the first item. Keep swapping it with its left neighbour until the swap would put it to the left of a number smaller than it. Pick the next number along. Repeat.
1. #================== #================================== ================================= ================================== ==================== === 2. # Time Complexity of Solution: 3. # Best O(n); Average Average O(n^2); Worst O(n^2). 4. # 5. # Approach: 6. # Insertion sort sort is good good for collections that are very small small 7. # or nearly sorted. Otherwise Otherwise it's not a good sorting sorting algorithm: algorithm: 8. # it moves data data around too much. Each time time an insertion insertion is made, made, 9. # all elements elements in a greater greater position position are shifted. 10. #============== #============================= ============================= ============================== ============================ ============ 11. 12. def insertionSort(alist): insertionSort(alist): for index in range(1,len(alist)): 13. range(1,len(alist)): 14. 15. currentvalue = alist[index] 16. position = index 17. while position>0 and alist[position-1]>currentvalue: 18. alist[position-1]>currentvalue: 19. alist[position]=alist[position-1] 20. position = position-1 21. 22. alist[position]=currentvalue 23. 24. alist = [54,26,93,17,77,31,44,55,20] [54,26,93,17,77,31,44,55,20] 25. insertionSort(alist) 26. print(alist)
BUBBLE SORT
1. #================== #================================== ================================= ================================== ==================== === 2. # Time Complexity of Solution: 3. # Best O(n^2); Average O(n^2); Worst O(n^2). 4. # 5. # Approach: 6. # Bubblesort is an elementary sorting algorithm. The idea is to 7. # imagine bubbling the smallest elements of a (vertical) array to the 8. # top; then bubble the next smallest; then so on until until the entire 9. # array is sorted. Bubble sort is worse than both insertion sort and 10. # selection sort. It moves elements as many times as insertion sort 11. # (bad) and it takes as long as selection selection sort (bad). (bad). On On the positive 12. # side, bubble sort is easy easy to understand. understand. Also there are highly 13. # improved variants of bubble sort. 14. # 15. # 0] For each element element at index index i from 0 to n, loop: 16. # 1] For each each element at index k, from n to i exclusive, loop: 17. # 2] If the element at k is less than that at k-1, swap them. 18. #================== #================================== ================================= ================================== ==================== === 19. 20. def bubbleSort(alist): bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): 21. range(len(alist)-1,0,-1): for i in range(passnum): 22. if alist[i]>alist[i+1]: 23. alist[i]>alist[i+1]: 24. temp = alist[i] 25. alist[i] = alist[i+1] 26. alist[i+1] = temp 27. 28. alist = [54,26,93,17,77,31,44,55,20] [54,26,93,17,77,31,44,55,20] 29. bubbleSort(alist) 30. print(alist)
MERGE SORT
1. #================== #================================== ================================= ================================== ==================== === 2. # Time Complexity of Solution: 3. # Best = Average Average = Worst = O(nlog(n)). O(nlog(n)). 4. # 5. # Approach: 6. # Merge sort is a divide and conquer algorithm. In the divide and 7. # conquer paradigm, a problem problem is broken into pieces where each piece 8. # still retains all the properties properties of the larger problem -- except except 9. # its size. To solve the original original problem, each piece is solved 10. # individually; then the pieces are merged back together. 11. # 12. # mid = len(aList) / 2 13. # left = mergesort(aLi mergesort(aList[:mid]) st[:mid]) 14. # right = mergesort(aL mergesort(aList[mid:]) ist[mid:]) 15. # 16. # That approach take too much memory for creating sublists. 17. #================== #================================== ================================= ================================== ==================== === 18. 19. def merge_sort(aList): merge_sort(aList): 20. if len(aList) < 2: 21. return aList 22. 23. 24. result = [] 25. mid = int(len(aList)/2) int(len(aList)/2) 26. 27. left = merge_sort(aList[:mid]) merge_sort(aList[:mid]) 28. right = merge_sort(aList[mid:]) merge_sort(aList[mid:]) 29. 30. while (len(left) > 0) and (len(right) > 0): if left[0] > right[0]: 31. 32. result.append(right.pop(0)) 33. else: 34. result.append(left.pop(0)) 35. 36. result.extend(left+right) #extend merges lists return result 37. 38. 39. print(merge_sort([4,2,5,7,2,1,3]))
QUICK SORT
1. #================== #================================== ================================= ================================== ==================== === 2. # Time Complexity of Solution: 3. # Best = Average = O(nlog(n)); Worst = O(n^2). 4. # 5. # Approach: 6. # Quicksort is admirably known known as the algorithm algorithm that that sorts an array 7. # while preparing preparing to sort it. it. For contrast, contrast, recall that merge sort 8. # first partitions partitions an array into smaller pieces, then sorts each piece, 9. # then merge the pieces pieces back. Quicksort actually sorts sorts the array 10. # during the partition phase. 11. # 12. # Quicksort works by selecting an element called a pivot pivot and splitting splitting 13. # the array around around that pivot pivot such that all the elements elements in, say, say, the 14. # left sub-array are less than than pivot and and all the elements in the right 15. # sub-array are greater than pivot. The splitting continues continues until the 16. # array can no no longer be broken into pieces. That's it. Quicksort is 17. # done. 18. #================== #================================== ================================= ================================== ==================== === 19. 20. import random 21. 22. def quicksort( aList ): 23. _quicksort( aList, 0, len( aList ) - 1 ) 24. 25. def _quicksort( aList, first, last ): if first < last: 26. 27. pivot = partition( aList, first, last ) 28. _quicksort( aList, first, pivot - 1 ) 29. _quicksort( aList, pivot + 1, last ) 30. 31. 32. def partition( aList, first, last ) : 33. pivot = first + random.randrange( last - first + 1 ) 34. swap( aList, pivot, last ) 35. for i in range( first, last ): 36. if aList[i] <= aList[last]: 37. swap( aList, i, first ) 38. first += 1 39. 40. swap( aList, first, last ) 41. return first 42. 43. 44. def swap( A, x, y ): 45. A[x],A[y]=A[y],A[x]
SEARCH BINARY SEARCH RECURSIVE
1. def binary(myList, value): 2. 3. mid = int(len(myList)//2) int(len(myList)//2) 4. 5. if(len(myList) == 0): 6. found!" ) print("\nNumber not found!") 7. return False 8. else: 9. if (myList[mid] == value): 10. print("number found") 11. found" ) 12. return True 13. elif (myList[mid] > value): 14. return binary(myList[:mid], value) 15. else: 16. return binary(myList[mid+1:], value) 17. 18. 19. myList = [1,4,5,6,9,10,12,14,16,24,29,30] [1,4,5,6,9,10,12,14,16,24,29,30] 20. binary(myList, 6)
ITERATIVE
1. def binarySearch(alist, item): 2. first = 0 3. last = len(alist)-1 4. found = False 5. 6. while first<=last and not found: 7. midpoint = (first + last)//2 if alist[midpoint] == item: 8. 9. found = True else: 10. if item < alist[midpoint]: 11. 12. last = midpoint-1 13. else: 14. first = midpoint+1 15. return found 16. 17. 18. testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,] 19. print(binarySearch(testlist, (binarySearch(testlist, 3))