DSAL-211-Lecture 6 - Sorting Algorithms
DSAL-211-Lecture 6 - 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) in a certain desired order
• Sorting orders can be numeric or lexicographical
• One of the most important and ubiquitous computational problem
• Widely used in applications/operation systems
• Used to pre-process input for other algorithms (e.g. binary search)
• Example sorting algorithms: bubble, selection, insertion, quick, heap,
comb, merge, bucket, shell
Sorting Terminology/Definitions
• Swap – switching element positions
• In-place sorting - elements of a list are sorted in their place and minimal
extra space is required as the algorithm executes
• 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 pass: (1,5,8,3,4,7) : 1 < 5 (no swap)
2nd pass: (1,5,8,3,4,7) : 5 < 8 (no swap)
3rd pass: (1,5,8,3,4,7) -> (1,5,3,8,4,7) : 8 > 3 (swap(8,3))
4th pass: (1,5,3,8,4,7) -> (1,5,3,4,8,7) : 4 < 8 (swap(8,4))
5th pass: (1,5,3,4,8,7) -> (1,5,3,4,7,8) : 7 < 8 (swap(8,7))
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])
temp := A[i]
Bubble Sort cont… A[i] := A[j]
A[j] := temp
end sub
Algorithm:
procedure bubbleSort(list: A) 1
n := size(A) 2
for i:=0 to n-1 3
for j:=1 to n-1 4
if(A[j-1] > A[j]) then 5
swap(A[j-1], A[j]) 6
end if 7
end for 8
end for 9
end procedure 10
Bubble Sort cont…
• Performance/Complexity
• Worst-case: O(n2)
• Avg. case: O(n2)
• Best case: O(n)
Algorithm (partitioning):
procedure partition(A, lo, hi)
pivot := A[hi]
i := lo – 1
for j := lo to hi – 1
if(A[j] < pivot) then
i := i + 1
swap(A[i],A[j])
end if
end for
if A[hi] < A[i + 1] then
swap(A[i + 1],A[hi])
end if
return i + 1 // 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
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: