Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting LinkedLists.

Similar presentations


Presentation on theme: "Sorting LinkedLists."— Presentation transcript:

1 Sorting LinkedLists

2 QuickSort Review What is needed to do quick sort efficiently?

3 QuickSort Review What is needed to do quick sort efficiently?
partitionFunction must be O(n)

4 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

5 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

6 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

7 QuickSort Could use existing algorithm with DLL

8 QuickSort Could use existing algorithm with DLL
Could adapt for Singly Linked List

9 QuickSort Use pivot pointer and two new lists for small and large items

10 QuickSort Grab first node as pivot

11 QuickSort Iterate through list, removing each node and adding to small or large

12 QuickSort Iterate through list, removing each node and adding to small or large

13 QuickSort Iterate through list, removing each node and adding to small or large

14 QuickSort Iterate through list, removing each node and adding to small or large

15 QuickSort Recursively sort small and large

16 QuickSort Recursively sort small and large

17 QuickSort Reassemble into one list

18 QuickSort Reassemble into one list

19 QuickSort Reassemble into one list

20 QuickSort Reassemble into one list

21 QuickSort Log(n) levels of recursion (hopefully)
O(n) work to partition O(n) work to reassemble – or O(1) with tail pointer

22 MergeSort MergeSort with array Disadvantages?

23 MergeSort MergeSort with array Need O(n) extra storage
Lots of copy overhead In place trades LOTS of copying for less storage

24 MergeSort What is needed to do efficiently?

25 MergeSort What is needed to do efficiently?
Slice in half in O(n) Merge in O(n)

26 Slice Slice list in half: Produce second linked list with second half

27 Slice Slice list in half: Walk to last node before halfway point

28 Slice Slice list in half: Attach otherlist to second half of list

29 Slice Slice list in half: Break list, update tail and length of mylist

30 Merge Merge : Combine elements from two sorted lists

31 Merge Merge : Combine elements from two sorted lists
One by one move smallest value to Merged list

32 Merge Merge : Combine elements from two sorted lists
One by one move smallest value to Merged list

33 Merge Merge : Combine elements from two sorted lists
One by one move smallest value to Merged list

34 Merge Merge : Combine elements from two sorted lists
One by one move smallest value to Merged list

35 Merge Steal nodes back into original list

36 Merge And remove from Merged

37 Merge And remove from Merged

38 Final Sort With split/merge, sort part is easy


Download ppt "Sorting LinkedLists."

Similar presentations


Ads by Google