Merge and Quick Sort 1 Divide and Conquer applied to sorting recursive and iterative selection sort cost considerations 2 Merge Sort split, sort, and merge a recursive sort function an iterative version 3 Quick Sort partition and sort cost considerations timing Python code MCS 275 Lecture 16 Programming Tools and File Management Jan Verschelde, 15 February 2017 Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 1 / 28
28
Embed
Merge and Quick Sort - University of Illinois at Chicagojan/mcs275/quicksort.pdf · Merge and Quick Sort 1 Divide and Conquer applied to sorting recursive and iterative selection
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Merge and Quick Sort1 Divide and Conquer
applied to sortingrecursive and iterative selection sortcost considerations
2 Merge Sortsplit, sort, and mergea recursive sort functionan iterative version
3 Quick Sortpartition and sortcost considerationstiming Python code
MCS 275 Lecture 16Programming Tools and File Management
Jan Verschelde, 15 February 2017
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 1 / 28
Merge and Quick Sort
1 Divide and Conquerapplied to sortingrecursive and iterative selection sortcost considerations
2 Merge Sortsplit, sort, and mergea recursive sort functionan iterative version
3 Quick Sortpartition and sortcost considerationstiming Python code
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 2 / 28
Divide and Conquerapplied to sorting
To solve a problem, divide it into smaller problems.
Solve the smaller problems and use the solutions of the smallerproblems to solve the original problem.
Recursion is natural, base case is trivial problem,apply mathematical induction for general case.
Sorting is a classical, important problem.
Consider various issues:data in memory or on fileprocedural or functional solutionrecursive or iterative algorithm
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 3 / 28
Merge and Quick Sort
1 Divide and Conquerapplied to sortingrecursive and iterative selection sortcost considerations
2 Merge Sortsplit, sort, and mergea recursive sort functionan iterative version
3 Quick Sortpartition and sortcost considerationstiming Python code
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 4 / 28
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 5 / 28
Sorting Recursively and Iteratively
Selection Sort done recursively:base case: length of list ≤ 1let m be the minimum of the list,denote by rest the list minus m,return [m] + sorted(rest)
Selection Sort done iteratively:let S be the sorted list, S = []
as long as the unsorted list is not empty:1 select the minimum from the unsorted list2 append the minimum to the sorted list S3 remove the minimum from the unsorted list
Procedural version does not return a sorted list,but sorts the list given on input.
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 6 / 28
the function recursive_select
def recursive_select(data):"""Returns a list with the datasorted in increasing order."""if len(data) <= 1:
The iterative loop splits, for as long as length of sublists is less than thelength of the original list.
Assume the length of the list to sort is power of 2.
Programming Tools (MCS 275) merge and quick sort L-16 15 February 2017 17 / 28
an iterative merge sort
def iterative_merge(data):"""Returns a list with the datasorted in increasing order.Assumes len(data) is a power of 2."""ind = 1hop = 2while hop <= len(data):