Sorting LinkedLists
QuickSort Review What is needed to do quick sort efficiently?
QuickSort Review What is needed to do quick sort efficiently? partitionFunction must be O(n)
QuickSort Review What is needed to partition in O(n)? Partition Array between low and high: pivotValue = value at low i = low+1 //left maker j = high //right marker while i <= j while i points to value <= pivotValue and i <= j increase i while j points to value >= pivotValue and i <= j decrease j if i < j swap values at i and j swap values at low and j return new location of pivot
QuickSort Review What is needed to partition in O(n)? O(n) : set i, j, pivot Partition Array between low and high: pivotValue = value at low i = low+1 //left maker j = high //right marker while i <= j while i points to value <= pivotValue and i <= j increase i while j points to value >= pivotValue and i <= j decrease j if i < j swap values at i and j swap values at low and j return new location of pivot
QuickSort Review What is needed to partition in O(n)? O(n) : O(1) : set i, j, pivot O(1) : move left or right one element compare elements swap elements Partition Array between low and high: pivotValue = value at low i = low+1 //left maker j = high //right marker while i <= j while i points to value <= pivotValue and i <= j increase i while j points to value >= pivotValue and i <= j decrease j if i < j swap values at i and j swap values at low and j return new location of pivot
QuickSort Could use existing algorithm with DLL
QuickSort Could use existing algorithm with DLL Could adapt for Singly Linked List
QuickSort Use pivot pointer and two new lists for small and large items
QuickSort Grab first node as pivot
QuickSort Iterate through list, removing each node and adding to small or large
QuickSort Iterate through list, removing each node and adding to small or large
QuickSort Iterate through list, removing each node and adding to small or large
QuickSort Iterate through list, removing each node and adding to small or large
QuickSort Recursively sort small and large
QuickSort Recursively sort small and large
QuickSort Reassemble into one list
QuickSort Reassemble into one list
QuickSort Reassemble into one list
QuickSort Reassemble into one list
QuickSort Log(n) levels of recursion (hopefully) O(n) work to partition O(n) work to reassemble – or O(1) with tail pointer
MergeSort MergeSort with array Disadvantages?
MergeSort MergeSort with array Need O(n) extra storage Lots of copy overhead In place trades LOTS of copying for less storage
MergeSort What is needed to do efficiently?
MergeSort What is needed to do efficiently? Slice in half in O(n) Merge in O(n)
Slice Slice list in half: Produce second linked list with second half
Slice Slice list in half: Walk to last node before halfway point
Slice Slice list in half: Attach otherlist to second half of list
Slice Slice list in half: Break list, update tail and length of mylist
Merge Merge : Combine elements from two sorted lists
Merge Merge : Combine elements from two sorted lists One by one move smallest value to Merged list
Merge Merge : Combine elements from two sorted lists One by one move smallest value to Merged list
Merge Merge : Combine elements from two sorted lists One by one move smallest value to Merged list
Merge Merge : Combine elements from two sorted lists One by one move smallest value to Merged list
Merge Steal nodes back into original list
Merge And remove from Merged
Merge And remove from Merged
Final Sort With split/merge, sort part is easy