DSAL-211-Lecture 5 - Sorting Algorithms
DSAL-211-Lecture 5 - Sorting Algorithms
Algorithms
By
W.G. Gondwe
Ag. ICT Manager/Lecturer, Computer Networks
CS&IT Department
Malawi University of Science & Technology
Overview
• Understanding the Sorting Problem
• Sorting Terminology
• Sorting Algorithms
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quick Sort
• Merge Sort (recursive)
The Sorting Problem
• Sorting: putting elements (of a list/document/chart/graph) in a
certain desired order
• One of the most important and ubiquitous computational
problems
• Widely used in applications/operation systems
• Used to pre-process input for other algorithms (e.g. binary search)
• Sorting criteria can be numeric or lexicographical
• Example sorting algorithms: bubble, selection, insertion,
quick, heap, comb, merge, bucket, shell
Sorting Terminology/Definitions
• Swap – switching positions of a pair of elements
• In-place sorting - elements of a list are sorted in their place and
minimal auxiliary space is required (input directly overwritten by
output)
• Internal/External sorting – Internal sorting utilizes available memory,
external sorting requires storage of items on secondary storage
• Stability – a sorting algorithm is stable if the relative positions of
elements with the same value (sorting key) is maintained i.e. any two
items are not swapped if they are equal
• Comparison-based sorting – examines elements only by using a
comparison operator e.g. <, >, ≥, ≤ (compare with search engine
sorting)
Bubble Sort
• A simple, in-place, comparison-based algorithm
• It is an iterative sorting algorithm that compares a pair of
elements at a time
• A pair out of order is swapped, otherwise the algorithm
proceeds to compare the next pair
• The algorithm continues to pass through the list until no swaps
are required
• Effectively, each out-of-order element “bubbles” to its rightful
position on each pass
Bubble Sort cont..
• Simulation: sort the list (1,5,8,3,4,7) in ascending order
1st iteration: (1,5,8,3,4,7) : 1 < 5 (no swap)
2nd iteration: (1,5,8,3,4,7) : 5 < 8 (no swap)
3rd iteration: (1,5,8,3,4,7) -> (1,5,3,8,4,7) : 8 > 3 (swap(8,3))
4th iteration: (1,5,3,8,4,7) -> (1,5,3,4,8,7) : 4 < 8 (swap(8,4))
5th iteration: (1,5,3,4,8,7) -> (1,5,3,4,7,8) : 7 < 8 (swap(8,7))
End of 1st pass
etc…
i.e. on first pass, largest item in the list bubbles to the end of the list
(ascending order)
sub swap(A[i],A[k])
Source: https://en.wikipedia.org/wiki/Bubble_sort
Bubble Sort cont…
• Performance/Complexity
• Worst-case: O(n2)
• Avg. case: O(n2)
• Best case: O(n)
swap(A[i],A[hi])
return i // return new pivot index
end procedure
Quicksort cont…
Complexity:
• Best-case: O(n log n)
• Average case: O(n log n)
• Worst-case: O(n2)
• On average, divide step complexity is log n and conquer (partitioning) complexity
is n (linear), giving a log-linear complexity
• Best case complexity can degenerate to O(n) for some partitioning schemes.
• Choice of pivot: can be first/last element, median or random element
• Choice of partitioning scheme has a bearing on overall complexity
Merge Sort
• Another divide-and-conquer sorting algorithm
• Recursively partitions a list into two (divide) and merges them
(conquer)
• Merge: combine two lists such that the resultant list is sorted
• The merging step is the most critical (conquering)
Merge sort cont..
Illustration: