03 Sorting Algorithms
03 Sorting Algorithms
Contents
• Insertion sort
• Bubble sort
• Selection sort
• Shell sort
• Merge sort
• Quick sort
• Heap sort
• Radix sort
2
Insertion Sort
• If a new element is inserted in the correct place
in a sorted list, the list will remain sorted
• Insertion Sort starts with a single element list
• Adding a second element in the proper place
keeps the list sorted
• Repeat until all elements are inserted into the
list at their correct place
• Sorted list grows in size after adding of each new
element
3
Insertion Sort Example
4
Insertion Sort Algorithm
Begin
Repeat for i = 2 to N step 1
Set new := list[ i ]
Set loc := i - 1
Repeat while (location ≥ 1) and (list[loc] > new)
Set list[loc + 1] := list[loc]
// shift list[loc] one position to the right
Set location := location - 1
End Repeat
Set list[ location + 1 ] := new
End Repeat
End
5
Analysis of Insertion Sort
• In Place sorting – no additional space required for
sorting
• Best Case – when the list is already sorted, only N
comparisons are required; O(N)
• Worst Case – when the list is sorted in reverse order,
N2 exchanges are required along with about half
number of comparisons; O(N2)
• Average Case – Same as Worst Case
• Only In-Place soring method suitable for Linked Lists
6
Bubble Sort
• If pairs of adjacent elements in a list are
compared with each other and none are out of
order, the list is sorted
• If any pair is out of order, those elements can be
swapped to get an ordered list
• Bubble sort makes multiple passes through a list
swapping any adjacent elements that are out of
order
7
Bubble Sort
• After the first pass, the largest element is
positioned at the end of list
• After the second pass, the second largest
element is positioned before the largest element
• Because of this, each successive pass of the
comparison loop can be shortened by one
8
Bubble Sort Example
9
Bubble Sort Algorithm
Begin
Set numberOfPairs := N
Set swappedElements := true
Repeat while (swappedElements is true)
Set numberOfPairs := numberOfPairs - 1
Set swappedElements := false
Repeat for i = 1 to numberOfPairs step 1
If (list[ i ] > list[ i + 1 ]) then
Swap( list[i], list[i + 1] )
Set swappedElements := true
End if
End Repeat
End Repeat
End 10
Improvements over Original Bubble Sort
• Reducing the number of elements to covered in
the inner loop by 1 for each cycle – as the
largest elements are moved to the end
• Checking whether any elements have been
swapped in each cycle – if not, the list is already
sorted and no further loops are necessary
11
Bubble Sort Analysis
• In Place Sort
• Time performance very similar to Insertion
Sort
• Average Case and Worst Case : O(N2)
• Best Case: O(N)
12
Selection Sort
• If we can identify smallest element and move it
to the start of it, and do it repeatedly for all
elements, the list is sorted
• Selection sort passes though the list comparing
elements such they reach their final position in
one exchange
• This sort is similar to Bubble Sort but does more
comparisons and less number of exchanges
13
Selection Sort Algorithm
Begin
Repeat for i = 1 to N Step 1
Set current := i
Repeat for location = i to N step 1
If list [location] < list [current]
// min element identified
Set current := location
End if
End Repeat
If ( i <> current)// if swap required
swap ( list [i], list [current] )
End if
End Repeat
End
14
Selection Sort Analysis
• Worst Case Performance in the same range as
Bubble Sort – O(N2)
• Performs N exchanges and N2 comparisons as
against N2 exchanges and N2 comparisons of
Bubble Sort
• Used in situations when exchanging elements is
more expensive than comparing them, for
example, lists with large records
15
Shell Sort
• Shell Sort treats the list as a combination of
interleaved sublists
• For example, elements in the even locations
could be one list and elements in the odd
locations the other list
• Shell sort begins by sorting many small lists, and
increases their size and decreases their number
as it continues
16
Shell Sort
• One technique uses lists with decreasing powers
of 2; for example, if the list has 64 elements, the
first pass uses 32 sublists of 2 elements each, the
second pass uses 16 sublists of 4 elements each,
and so on
• Other list sizes have also been used – no specific
number works best in all list sizes
• These sublists are sorted using Insertion Sort
17
Shell Sort Example
8 sublists
2 elements / sublist
Increment = 8
4 sublists
4 elements / sublist
Increment = 4
2 sublists
8 elements / sublist
Increment = 2
1 sublist
16 elements / sublist
18
Increment = 1
Shell Sort Algorithm
Begin
Set passes := log2 N
19
Shell Sort Analysis
• Performance of the algorithm changes
dramatically with different values of increment
• With increments that are one less than powers of
2, the worst-case has been shown to be O(N3/2)
• An order of O(N4/3) can be achieved with other
increments
• Still undergoing active research and analysis
• Efficient In-Place sorting method for large lists
20
Merge Sort
• If there are two sorted lists, a combined sorted
list can be created by merging the lists
• Merge Sort starts with breaking down an
unsorted list to single elements
• Single elements are combined to form sorted
pairs – which then are combined to form larger
sorted lists
• It is a Divide and Conquer algorithm – divides a
large list into smaller units and builds the
complete list by combining them
21
Merge Sort Example
22
Merge Sort Example (continued)
23
Merge Sort Example (continued)
24
Merge Sort Algorithm
MergeSort(list, first, last)
Begin
If first < last then
Set middle = ( first + last ) / 2
MergeSort( list, first, middle ) // L-Part
MergeSort( list, middle + 1, last ) // R-Part
MergeLists( list, first, middle, middle + 1, last )
End if
End
// list: the list of elements to be put in order
// first: the index of the 1st element in the part of list to sort
// last: the index of the last element in the part of list to sort
// if first=last, list has only one element
25
MergeList Algorithm (Part 1)
MergeLists( list, start1, end1, start2, end2) // start1: beginning of list A
26
MergeList Algorithm (Part 2)
28
Quicksort
29
Quicksort Algorithm
30
Partitioning Process
• Partitioning is the critical element of Quicksort
algorithm
• During Partitioning, the list is scanned element by
element comparing values to the pivot
• During this process, there are sections of the list as
indicated below
not yet scanned
p i
31
Partitioning Process
3. List A is scanned from left starting from i = 2, and every element A[i] is compared with pivot:
4. Finally, swap (A[1], A[p]). The pivot in this sub-list is now in its correct position
32
Quicksort Example
33
Quicksort Example
34
PivotList Algorithm
PivotList(list, first, last)
Begin
Set PivotValue := list[ first ]
Set pivot := first
Repeat for index = first+1 to last step 1
If (list[ index ] < PivotValue)
Set pivot := pivot + 1
If ( pivot <> index ) // don’t swap if same
Swap( list[pivot], list[index] )
End if // This for loop does
//(last-first) iterations
End if
End Repeat
// move pivot value into correct place
Swap(list[first], list[pivot])
return pivot // Returns position of pivot
End
index : incremented at every step
37
Heap Sort
38
Heap Details
• A heap is also a complete binary tree, so nodes
are filled in along the bottom of the tree from
left to right and a new level is started only when
the previous level has been filled
39
Heap Example
40
Heap Storage
• Heap is stored in an array
• For an element stored at location i (i >0), its
children are stored at locations 2i and 2i+1
• If 2i and 2i+1 are greater than the list size, then
the element at location i is a leaf
• If only 2i+1 is greater than the list size, then the
element at location i has just one child
41
Heapsort
• Heapsort begins by constructing a heap
42
Heapsort Algorithm
Step 1: Construct the heap
Step 2: Start the sorting process
for i = 1 to N do
Swap the root with the last location of the current heap
Reduce heap size (N) by 1
Fix the heap with new size
end for
43
FixHeap Algorithm
FixHeap (list, low, key, high)
v
Begin
Set vacant := low // v
Repeat while 2*vacant ≤ high
Set largerChild := 2*vacant // 2v 2v 2v+1
45
Heap Construction Example
the last internal node
Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
i=8 2i = 16
no change
46
Final Heapsort Algorithm
// Step 1: Constructing the heap from the initial
list
Repeat for i = N/2 to 1 step -1
FixHeap( list, i, list[i], N )
End Repeat
47
Final Heapsort Loop - 1
1
1
1 1
3
3
3
3
6
6
6
48
Final Heapsort Loop - 2
49
Heapsort Analysis
• FixHeap places each node at appropriate level with
respect to its parent node
• For a binary heap, if N is the number of nodes, the
depth D <= log N
• In Worst Case Situation, for N nodes, number of
operations are (N* ( log N)) or O(N log N)
• Best Case is also O (N log N)
• Number of operations for building a heap is O(N)
• Best In-Place sorting algorithm – suitable for arrays
but not Linked Lists
50
Radix Sort
• This sort is unusual because it does not directly
compare any of the elements
• Instead creates a set of buckets and repeatedly
separate the elements into the buckets
• On each pass, different part of the elements are
examined and sorted
51
Radix Sort
• Assuming decimal elements and 10 buckets, we
would put the elements into the bucket
associated with its units digit
• The buckets are actually queues so the elements
are added at the end of the bucket
• At the end of the pass, the buckets are
combined in increasing order
52
Radix Sort
• On the second pass, we separate the elements
based on the “tens” digit, and on the third pass
we separate them based on the “hundreds” digit
53
Radix Sort Example
54
Radix Sort Example (continued)
55
Radix Sort Example (continued)
The unit and tens digits are already in order
56
Algorithm to sort a set of numeric keys
Begin
shift = 1
Repeat for pass = 1 to keysize do
Repeat for entry = 1 to N do
bucketNumber = (list[entry] / shift) mod 10
Append( bucket[bucketNumber], list[entry] )
End Repeat
list = CombineBuckets()
shift = shift * 10
End Repeat
End
keysize: # of digits of the longest key
N: # of elements in list
57
Radix Sort Analysis
58
Radix Sort Analysis
• Firstly, this sort can only be applied to inputs that
are based on known ranges of values (radixes)
• Also, this is very time efficient algorithm but not
space efficient
• If an array is used for the buckets, for B buckets,
N*B extra memory locations are required because
it’s possible for all of the elements may wind up in
one bucket
59
The End