Escolar Documentos
Profissional Documentos
Cultura Documentos
BIG O
RECURSION
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
2. if string == '':
3. return 0
4.
5. temp = string[-1]
6. if temp == 'x':
7. return COUNT_X (string[:-1]) + 1
8. return COUNT_X (string[:-1]) + 0
1. def REV_STRING(s):
2. if s == "":
3. return s
4. else:
5. return REV_STRING(s[1:]) + s[0]
DATA STRUCTURES
Data structures are constructions for storing and organising data. Ie. Arrays, Stacks, Queues, Trees..
STACKS
QUEUES
LINKED LISTS
An ordered set of data elements, each containing a link to its successor (and sometimes its
predecessor doubly-linked list).
TREES
- Nodes left child must have value less than parent, and right child have value greater than
parent.
- Attributes;
o 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
- Used to quickly find data.
- Insertion Is O(logN)
Insertion
ALGORITHM PYTHON
1. def insert(tree, data):
If root is NULL
then create root node 2. if tree.value == NONE:
return 3. tree=BinTreeNode(data)
4.
If root exists then 5. elif tree.value > data:
compare the data with node.data 6. if tree.leftChild:
7. return tree.leftChild.insert(
while until insertion position is
data)
located
8. else:
If data is greater than node.data 9. tree.leftChild = Node(data)
goto right subtree 10.
else 11. else:
goto left subtree 12. if tree.rightChild:
13. return tree.rightChild.insert
endwhile (data)
14. else:
insert data
15. tree.rightChild = Node(data)
end If
Finding a node
RECURSIVELY ITERATIVELY
1. BIN-TREE-FIND(tree,target) 9. BIN-TREE-FIND(tree,target)
2. IF tree.value = target OR tree = 0: 10. R tree
3. RETURN tree 11. WHILE r
4. ELSE IF target < tree.value 12. IF r.value = target
5. RETURN BIN-TREE- 13. RETURN r (or TRUE)
FIND(tree.left, target) 14. ELSE IF r.value > target
6. ELSE 15. r r.left
7. RETURN BIN-TREE- 16. ELSE
FIND(tree.right, target) 17. r r.right
8. RETURN 0 18. RETURN FALSE
Traversal:
GRAPH TRAVERSAL
INSERTION SORT
1. #=======================================================================
2. # Time Complexity of Solution:
3. # Best O(n); Average O(n^2); Worst O(n^2).
4. #
5. # Approach:
6. # Insertion sort is good for collections that are very small
7. # or nearly sorted. Otherwise it's not a good sorting algorithm:
8. # it moves data around too much. Each time an insertion is made,
9. # all elements in a greater position are shifted.
10. #=======================================================================
11.
12. def insertionSort(alist):
13. for index in range(1,len(alist)):
14.
15. currentvalue = alist[index]
16. position = index
17.
18. while position>0 and 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]
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 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 sort (bad). On the positive
12. # side, bubble sort is easy to understand. Also there are highly
13. # improved variants of bubble sort.
14. #
15. # 0] For each element at index i from 0 to n, loop:
16. # 1] For 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):
21. for passnum in range(len(alist)-1,0,-1):
22. for i in range(passnum):
23. if 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]
29. bubbleSort(alist)
30. print(alist)
MERGE SORT
1. #=======================================================================
2. # Time Complexity of Solution:
3. # Best = Average = Worst = O(nlog(n)).
4. #
5. # Approach:
6. # Merge sort is a divide and conquer algorithm. In the divide and
7. # conquer paradigm, a problem is broken into pieces where each piece
8. # still retains all the properties of the larger problem -- except
9. # its size. To solve the original problem, each piece is solved
10. # individually; then the pieces are merged back together.
11. #
12. # mid = len(aList) / 2
13. # left = mergesort(aList[:mid])
14. # right = mergesort(aList[mid:])
15. #
16. # That approach take too much memory for creating sublists.
17. #=======================================================================
18.
19. def merge_sort(aList):
20.
21. if len(aList) < 2:
22. return aList
23.
24. result = []
25. mid = int(len(aList)/2)
26.
27. left = merge_sort(aList[:mid])
28. right = merge_sort(aList[mid:])
29.
30. while (len(left) > 0) and (len(right) > 0):
31. if left[0] > right[0]:
32. result.append(right.pop(0))
33. else:
34. result.append(left.pop(0))
35.
36. result.extend(left+right) #extend merges lists
37. return result
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 as the algorithm that sorts an array
7. # while preparing to sort it. For contrast, recall that merge sort
8. # first partitions an array into smaller pieces, then sorts each piece,
9. # then merge the pieces back. Quicksort actually sorts the array
10. # during the partition phase.
11. #
12. # Quicksort works by selecting an element called a pivot and splitting
13. # the array around that pivot such that all the elements in, say, the
14. # left sub-array are less than pivot and all the elements in the right
15. # sub-array are greater than pivot. The splitting continues until the
16. # array can 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 ):
26. if first < last:
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