Uploaded by pauloqtal

Summary Algorithms

advertisement
Summary Algorithms
Purpose of pseudocode:
The purpose of pseudocode is to provide a high level, easy readable
description of an algorithm before it is implemented into a programming
language.
Variable in Pseudocode:
Variables are used in pseudocode to store and manipulate data, represent
memory and to have a better readability and clarity.
Explain diff worst case, best case and average case:
Worst case refers to the maximum amounts of iterations that an algorithms
could have.
Average case refers to the most common amounts of iterations that an
algorithm can have.
Best case refers to the minimum amount of iterations that the algorithm can
have.
Heap sort: deletion, insertion
Deletion:
Swap the root for the last node, then eliminate the last node (root)
Compare the new root with its children and swap with the child of
greater value (for max heap) or with the one of lower value (for min
heap).
repeat until the max or min heap properties are satisfied.
Quicksort: min 12, elements, show visualization 7
multiple choice:
stragegy's of quicksort to chose pivots
First element: choose the first element as pivot, this can lead to poor
performance.
Last element: choose last element as pivot, this can lead to poor
performance as well
Randomize Quicksort: this involves randomizing the pivot and this way
eliminating the possibility of consistently encountering the worst
possible scenario.
Merge sort do what kind of seleciton, comparation
sort.
Divide and conquer, which operation is not involved in
D&C. Small problems, look for solution, subdivision,
etc.
What's the purpose of... array sort etc.
Full binary tree, incomplete complete
Diff between perfect and full binary tree
QuickSort Algorithms
language-python
def QuickSort(arr):
if len(arr) <= 1:
return arr
pivot = len(arr) - 1
left, right = Partition(arr, pivot)
return QuickSort(left) + [arr[pivot]] + QuickSort(right)
def Partition(Arr, pivot):
left = []
right = []
for i in range(len(Arr)):
if i == pivot:
continue
elif Arr[i] < Arr[pivot]:
left.append(Arr[i])
else:
right.append(Arr[i])
return left, right
A = [13, 25, 78, 1, 0, 2, 7, 9, 54, 99, 69, 3]
sorted_A = QuickSort(A)
print(sorted_A)
HomeWorks
HW 1
Write the pseudocode for Insertion sort
Sort(A)
for i = 2 to len(A): (first index is 1)
current = A[i]
j = i - 1
while j >= 0 and A[j] > current:
A[j + i] = A[j]
j -= 1
A[j + 1] = current
return A
HW 2
Download