Higher Sort and Selection Algorithms Flashcards

1
Q

QuickSort Idea

A
  • divide and conquer
    • a three element list can be divided into left right and mid
    • we split the list into three element lists sorted around a pivot element
    • we merge all the three element lists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Merge Sort Idea

A
  • divide and conquer
    • take a pivot index
    • split the lists recursively until you end up with one element lists
    • then merge the results by comparing the elements
  • the merge function does the comparison and composition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

QuickSort Sample Execution: [10,2,3,4,5,1]

A

QuickSort: [10,2,3,4,5,1]
Split: [10,2,3,4,5,1] => [2,3,1],[4],[10,5]
QuickSort: [2,3,1]
Split: [2,3,1] => [2,1],[3],[]
QuickSort: [2,1]
Split: [2,1] => [],[1],[2]
Merge: [],[1],[2] => [1,2]
Merge: [1,2],[3],[] => [1,2,3]
QuickSort: [10,5]
Split: [10,5] => [],[5],[10]
Merge: [],[5],[10] => [5,10]
Merge: [1,2,3],[4],[5,10] => [1,2,3,4,5,10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

QuickSort Python Code

A
**def** **QuickSort**(list): **if** (len(list) \<= **1**): **return** list less, greater, equal = Partition2(list) less = QuickSort(less) greater = QuickSort(greater) result = less + equal + greater **return** result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

MergeSort Python Code

A
**def** **Merge**(list1, list2): i = **0** j = **0** result = [] **while**( i \< len(list1) **and** j \< len(list2)): **if** (list1[i] \<= list2[j]): result.append(list1[i]) i = i + **1****else**: result.append(list2[j]) j = j +**1****while** (i \< len(list1)): result.append(list1[i]) i = i + **1****while**(j \< len(list2)): result.append(list2[j]) j = j +**1****return** result **def** **MergeSort**(list): **if** (len(list) \<= **1**): **return** list; pivot = len(list) // **2** left = list[:pivot] right = list[pivot:] left = MergeSort(left) right = MergeSort(right) **return** Merge(left, right)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Heap Indexing

A
  • leftIndex: (i * 2) + 1 / (i * 2)
  • rightIndex: (i * 2) + 2 / (i * 2) + 1
  • lastParent: len(list) / 2 - 1 / len(list) / 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Heap Sort Idea

A
  • We apply the tournament principle
  • Two elements play against eachother
  • We hipify each element by comparing it with the two child nodes starting from the last parent
  • When we have hipified all nodes we have a MaxHeap which means that every parent is greater than it’s children
  • We know that our first element is the greatest
  • We iterate through the heap swap the first element with the last element and hipify in order to end up with a sorted list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Heap Sort Hipify function Python Code

A
**def** **MaxHipify**(list,i): leftIndex = (i\***2**) + **1** rightIndex = (i\***2**) + **2** chosenIndex = i **if** (leftIndex \< len(list) **and** list[leftIndex] \> list[i]): Swap(list, leftIndex, i) chosenIndex = leftIndex **if** (rightIndex \< len(list) **and** list[rightIndex] \> list[i]): Swap(list, rightIndex, i) chosenIndex = rightIndex **if** (chosenIndex != i): MaxHipify(list, chosenIndex)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Heap Sort Python Code

A
**def** **HeapSort**(list): lastParentIndex = (len(list) // **2**) - **1****while**(lastParentIndex \>=**0**): MaxHipify(list, lastParentIndex) lastParentIndex = lastParentIndex -**1**i = len(list) -**1**result = [] # as we have a max heap the first element is the greatest**while**i \>=**0**: result.insert(**0**, list[**0**]) # swap the first element with the last Swap(list,**0**, i) list = SubList(list, len(list) -**1**) # hipify in order to position greatest at top MaxHipify(list,**0**) i = i -**1****return** result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

QuickSelect Idea

A
  • we resort to the idea of QuickSort by recursively partitioning around a pivot element until we have isolated the first k-Elements consisting of the left and the right part
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Quick Select Python Code

A
**def** **QuickSelect**(list, k): **if** (k \> len(list)): **return** None leftList, rightList = Partition(list) **if** (len(leftList) == k): **return** leftList[k-**1**] **else**: **if** (len(leftList) \< k): # less than k elements in the left list we have to search in the right list**return** QuickSelect(rightList, k - len(leftList)) **else**: **return** QuickSelect(leftList, k)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Heap Select Python Code

A
**def** **BuildHeap**(list): index = (len(list) // **2**) - **1****while**(index \>=**0**): Heapify(list, index) index = index -**1****def** **HeapSelect**(list,k): **if** (k \> len(list)): **return** None BuildHeap(list) **for** index **in** range(**1**,k): Swap(list, **0**, len(list)-**1**) list = SubList(list, len(list)-**1**) Heapify(list, **0**) **return** list[**0**]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Self-Organizing lists strategies

A
  • MF-Strategy: Move to Front
    • Element is moved to the top immediatly after access
  • T-Strategy
    • Element is moved one position to the front after it has been accessed
  • FC-Strategy
    • Elements are sorted according to their frequency count
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Complexity Classes

A
  • LOGSPACE - the class of all problems which have a logarithmic memory complexity
  • P - the class of all problems which have a time complexity underlying polynomial function T(n) in {O(n) | n >= 2}
  • NP - the class of all problems which don’t necessarily have a polynomial time complexity function but a polynominal time complexity function which would be required to verify the calculation
  • PSPACE - the class of all problems which have a polynomial memory space function
  • EXPTIME - the class of all problems which have an exponential time complexity function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Was macht die Funktion Heapify or Durchsickern?

A

Wir haben einen Parent A und zwei Kinder B und C. Das Ziel ist es A an die Stelle des Maximums von B und C zu bekommen, falls A kleiner als B oder C.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A