sortPythonic
Table of Contents
Lab 8: Sorting
Sort the given list using selection sort and insertion sort.
[TOC]
Select vs Insert vs Merge vs Quick
In
selectsort
, the position of the update is pre-determined, starting from the end of the list. We then go select the maximum value among the unsorted elements of the list, and swap it with the element in the pre-determined location.In
insertsort
, given a key, a copy of a pre-determined element in the list, we insert it at the appropriate location after comparing it with the unsorted elements of the list.In
mergesort
, a divide-and-conquer partitioning algorithm (which more often requires extra memory), the input array is divided in two halves, calls itself recursively for the two halves and then merges the two sorted halves. Themerge()
function is used for merging two halves.In
quicksort
, also a divide-and-conquer partitioning algorithm (lends itself to be efficiently implemented in-place without extra memory), the choice of the pivot element determines how the elements get partitioned, and calls itself recursively for the two partitions.
Solution Key
or
Ajeeth selectsort
lambda
version:
Related Material
http://bit.ly/quickSortVideoCD - a video explaining QuickSort incrementally in a CyberDojo session.
http://bit.ly/quickSortVideo
What exactly does this accomplish?
MergeSort description
A general purpose merge
function which can merge sorted arrays in ascending order of their elements. The merge is accomplished by popping the element from either A
or B
, and then adding the remnants of A
and B
.
Review the first test in the http://bit.ly/mergeSortCD session.
If you attempt to merge two arrays and avoid duplicates, then try http://j.mp/unionListCC
series
starts off with as many lists as there are elements in both the lists.zip_longest
is used to handle odd counts in the merging of lists, since it will automatically use a suitable[]
element to balance things out, whenever required. Thewhile
loop continues until there is only one list in theseries
.
Last updated