CH 5 Sorting
CH 5 Sorting
Sorting
Data Structure and Algorithms
1
Overview
Unit 5: Sorting Algorithms (5 hrs)
1.Internal/external Sort, Stable/Unstable Sort
2.Insertion and selection Sort
3.Bubble and Exchange Sort
4.Quick Sort and Merge Sort
5.Radix Sort
6.Shell Sort
7.Heap Sort as priority queue
Unsorted List
Sorted lists
(Main Memory)
Source: https://www.happycoders.eu/algorithms/insertion-sort/#Example_Sorting_Playing_Cards
Sourse: https://en.wikipedia.org/wiki/Insertion_sort
K=1 31 41 59 26 41 58 No shift
Shift = 3
K=2 31 41 59 26 41 58
Move = 1
Shift = 1
K=3 26 31 41 59 41 58
Move = 1
Shift = 1
K=4 26 31 41 41 59 58
Move = 1
K=5 26 31 41 41 58 59 Which is sorted
Source: https://www.happycoders.eu/algorithms/selection-sort/
• Algorithm:
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat steps 2, 3 and 4 until list is sorted
An example of bubble sort. Starting from the beginning of the list, compare every
adjacent pair, swap their position if they are not in the right order (the latter one is
smaller than the former one). After each iteration, one less element (the last one) is
needed to be compared until there are no more elements left to be compared.
7, 5, 2, 4, 3, 9 5, 2, 4, 3, 7, 9 2, 4, 3, 5, 7, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9
5, 7, 2, 4, 3, 9 2, 5, 4, 3, 7, 9 2, 4, 3, 5, 7, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9
5, 2, 7, 4, 3, 9 2, 4, 5, 3, 7, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9 5th Pass
5, 2, 4, 7, 3, 9 2, 4, 3, 5, 7, 9 2, 3, 4, 5, 7, 9 4th Pass
5, 2, 4, 3, 7, 9 2, 4, 3, 5, 7, 9 3rd Pass
5, 2, 4, 3, 7, 9 2nd Pass
1st Pass
7, 5, 2, 4, 3, 9 2, 7, 5, 4, 3, 9 2, 3, 7, 5, 4, 9 2, 3, 4, 7, 5, 9 2, 3, 4, 5, 7, 9
5, 7, 2, 4, 3, 9 2, 5, 7, 4, 3, 9 2, 3, 5, 7, 4, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9
2, 7, 5, 4, 3, 9 2, 4, 7, 5, 3, 9 2, 3, 4, 7, 5, 9 2, 3, 4, 5, 7, 9 5th Pass
2, 7, 5, 4, 3, 9 2, 3, 7, 5, 4, 9 2, 3, 4, 7, 5, 9 4th Pass
2, 7, 5, 4, 3, 9 2, 3, 7, 5, 4, 9 3rd Pass
2, 7, 5, 4, 3, 9 2nd Pass
1st Pass
https://gfycat.com/pleasantcloseeyelashpitviper
• The list can now be divided at the split point and the quick sort can be
invoked recursively on the two halves.
Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 49
Quick Sort
Algorithm:
1. Consider the first element of the array as pivot.
2.Define two variables left and right. Set left and right
to first and last elements of the array respectively.
3.Increment left until array[left] > pivot then stop.
4.Decrement right until array[right] < pivot then stop.
5.If left < right then exchange array[left] and
array[right].
6.Repeat steps 3,4 & 5 until left > right.
7.Exchange the pivot element with array[right] element.
An example of merge sort. First divide the list into the smallest
unit (1 element), then compare each element with the adjacent
list to sort and merge the two adjacent lists. Finally all the
elements are sorted and merged.
• Sort input array using counting sort (or any stable sort)
according to the ith digit.
• Each pass over n d-digit numbers will take O(n+b) time, and there are d
passes total.
• When d is a constant and b isn't much larger than n (in other words,
b=O(n)), then radix sort takes linear time.
https://stackoverflow.com/questions/4833423/shell-sort-java-example
https://commons.wikimedia.org/wiki/File:Heap_sort_example.gif
Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 71
Efficiency of Sort Algorithms
73