QuickSort Idea
Merge Sort Idea
QuickSort Sample Execution: [10,2,3,4,5,1]
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]
QuickSort Python Code
**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
MergeSort Python Code
**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)
Heap Indexing
Heap Sort Idea
Heap Sort Hipify function Python Code
**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)
Heap Sort Python Code
**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
QuickSelect Idea
Quick Select Python Code
**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)
Heap Select Python Code
**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**]
Self-Organizing lists strategies
Complexity Classes
Was macht die Funktion Heapify or Durchsickern?
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.