LN_CSC310 04 Sorting Algorithms I
LN_CSC310 04 Sorting Algorithms I
Lecture Notes
Dr Fatimah Adamu-Fika
Department of Cyber Security
Faculty of Computing
Air Force Institute of Technology
Kaduna, Nigeria
April 2023
© Since 2020. All rights reserved. No part of this document may be reproduced or
transmitted in any form or by any means, electronic or mechanical, including
photocopy, recording, or any information storage and retrieval system, without
permission in writing from the author.
CONTENT
Overview ................................................................................................................................ 1
Insertion Sort......................................................................................................................... 14
Summary .............................................................................................................................. 21
Exercises ............................................................................................................................... 22
Textbooks.......................................................................................................................... 24
Websites ........................................................................................................................... 24
There are two categories of sorting based on whether the objects that need to be
sorted can fit into the available memory all at once or not.
• Internal sorting: When all elements that need to be sorted are placed in
memory at the same time, then the sorting done is internal.
• External sorting: When all elements that need to be sorted cannot fit in memory
all at once, then the sorting is done in phases. The sorted data are put back
together. Such sorting is called external sorting. External sorting is used for a
massive amount of data, and some sort of external storage is used to hold the
large data and the sorted output files.
4. A stable sorting algorithm maintains the relative order of elements with equal
values. This means equal elements appear in the same order in the sorted
output as they do in the input.
Sorting Problem
The sorting problem requires the sorting of the items of a given list. Such a list can be
a list of numbers, character strings and records, similar to those maintained by AFIT
about you. In the case of sorting records, we need to choose a piece of information
to guide the sorting. For example, we chose to sort your last semester's results by your
matriculation number. Such chosen piece of information is called a key. Often, we
talk of sorting a list of keys even when we deal with items that are not records. The
sorted list must be in monotonic order, i.e., each element must not be smaller or larger
than the previous element as required by the ordering criterium. The sorted list must
retain all the original elements, i.e., the sorted list must be a permutation of the given
unsorted elements. In your semester results, the keys (your matriculation numbers) are
The sorting problem is a problem of interest in computing for several reasons. We may
need to sort a list because it might be a required output for a task such as ranking
internet search results. Sorting can make it easier to answer questions about a list or to
retrieve information from a list, for example, sorted students’ records. Sorting can also
be an intermediary step in solving larger problems, for example, some greedy
algorithms require a sorted input.
Unless otherwise stated, the sorting algorithms we will be looking at will be solving the
defined sorting problem. We will be studying six common sorting algorithms:
1. The three elementary sorting algorithms: Selection sort; Bubble sort and
Insertion sort
2. Merge sort
3. Quicksort
4. Heapsort
This unit will focus on the three elementary sorting algorithms while the other three will
be discussed in the next unit.
SELECTION SORT
Selection sort uses a brute-force approach to sort a given list. Selection sort is a sorting
algorithm that selects the smallest element from an unsorted list in each iteration and
places that element at the beginning of the unsorted sublist.
At the start of our sort, before we begin our iterations our list looks like this:
5 3 2 4 1
The current element at index min is highlighted in red. The element at the index that
is being compared with the element at min is shown with a red border. Elements that
are in their final positions are shown in green.
Note: min stores the index of the smallest element and NOT its value!
1. Set the index of the first element of the list as a variable that holds the smallest
value, let’s call this variable min.
mi n
5 3 2 4 1
0
<
mi n
5 3 2 4 1
0
mi n
5 3 2 4 1
1
reset mi n
< <
mi n mi n
5 3 2 4 1 5 3 2 4 1
1 2
mi n mi n
5 3 2 4 1 5 3 2 4 1
2 2
reset mi n mi n is unchanged
<
mi n
5 3 2 4 1
2
mi n
5 3 2 4 1
4
reset mi n
3. At the end of each pass (iteration), the element at index min is placed at the
front of the remaining unsorted elements. And the element that was previously
there will be placed at index min.
swap
mi n
5 3 2 4 1
4
1 3 2 4 5
Remember min stores the index of the smallest element and NOT its value!
After the 1st pass (before i increases when i = 0), the list: [1, 2, 3, 4, 5]
1 3 2 4 5
After the 2nd pass (before i increases when i = 1), the list: [1, 2, 3, 4, 5]
1 2 3 4 5
After the 3rd pass (before i increases when i = 2) , the list: [1, 2, 3, 4, 5]
1 2 3 4 5
After the (n–1)th pass (before i increases when i = n – 2), the list: [1, 2, 3, 4, 5]
1 2 3 4 5
After the nth pass (before i increases when i = n – 1), the list: [1, 2, 3, 4, 5]
1 2 3 4 5
Generally, the algorithm maintains two sublists in the original list, (1) a sublist list
of elements in their final positions and (2) a sublist of the remaining elements in
the list waiting to be sorted. On each pass of the algorithm, it searches for the
smallest element in the unsorted sublist and places it at the front of that sublist.
For simplicity and explanation purposes, we assume the list is an array, with the
first index at 0 and the last index at n – 1. Let i be the current pass, the list scans
through the remaining n – i unsorted elements, and the smallest element A[min]
in the subarray A[i … n – 1] will be swapped with A[i] (i.e., the first element in
that subarray).
From the example above we can see that after n – 1 passes the array will be fully
sorted. The last pass (nth pass) is redundant, as the element at index n – 1 will be at its
final position when every other element preceding it is put in its right position. This pass
Algorithm SelectionSort(A)
Input: an array, A[0 … n – 1], with n order-able items.
Output: The array, A, sorted in non-decreasing order.
START
1. for( i = 0; i < n - 1; i++ ) //for i = 0 to n – 2
2. min = i
3. for( j = i + 1; j < n; j++ ) //for j = i + 1 to n – 1
4. if( A[j] < A[min]
5. min = j
6. Swap( A, i, min ) //swap A[min] with A[i]
7. return A
END
Correctness Proof
Let A´ denote the output of SelectionSort(A).
To prove that SelectionSort is correct, we need to prove that it terminates and that:
where n = A.length.
a. We precisely state a loop invariant for the inner for loop in lines 3—5 and prove
that this loop invariant holds.
b. Using the termination condition of the loop invariant proved in part (a), we will
precisely state a loop invariant for the outer for loop in lines 1—6 that will allow
us to prove the inequality.
Theorem:
A´ consists of the elements in A but is sorted in non-decreasing order.
Proof:
Inner Loop invariant (for loop of lines 3–5)
At the start of each iteration of the inner for loop, A[min] is smaller than or equal
to the elements in the subarray A[i … j – 1], i.e., A[min] is the smallest item in
that subarray.
Maintenance: From the proof of the inner loop, after the execution of the inner
loop, A[min] will be the smallest element of the subarray A[i … n – 1]. And at
the beginning of the outer loop, the subarray A[0 … i – 1] contains i elements,
in sorted order, that are smaller than the elements of A[i … n – 1]. After the
execution of the outer loop, A[i] becomes the smallest element in the subarray
A[i … n – 1], and the subarray A[0 … i] will consists of elements, in sorted order,
that are smaller than the elements of A[i + 1 … n – 1]. This implies that at the
end of the loop:
Termination: The loop terminates when i = A.length – 1. At that point, array A[0
… n – 1] will contain all the elements sorted in non-decreasing order. Because,
from the maintenance phase of the loop invariant we know that at the end of
an iteration of the outer loop, the subarray A[0 … i] consists of sorted elements.
Substituting n – 1 for i, we have the subarray A[0 … n – 1] which consists of the
original elements, but in sorted order. This is the entire array, so the entire array
is sorted. Hence, the loop invariant holds.
Efficiency Analysis
The selection sort has a straightforward analysis. The input size is given by the number
of elements in the array (list), n; the basic operation is the key comparison A[j] < A[min]
(on line 4). The number of times it is executed depends only on the input size and is
given by the following sum:
!"( !"#
' 1 = (𝑛 − 1) − (𝑖 + 1) + 1
$%&'#
=𝑛−1−𝑖−1+1=𝑛−1−𝑖
!"(
∴ 𝐶(𝑛) = '(𝑛 − 1 − 𝑖)
&%)
!"( (!"()'#
'1= ' 1 = (𝑛 − 1)
&%) &%#
!"( !"(
(𝑛 − 2) ⋅ [(𝑛 − 2) + 1] (𝑛 − 2)(𝑛 − 1)
'𝑖 = '𝑖 = =
2 2
&%) &%#
!"(
(𝑛 − 2)(𝑛 − 1)
∴ '(𝑛 − 1 − 𝑖) = [𝑛(𝑛 − 1)] − [𝑛 − 1] − 1 2
2
&%)
(𝑛 − 2)(𝑛 − 1)
= [(𝑛 − 1)(𝑛 − 1)] − 1 2
2
[2(𝑛 − 1)(𝑛 − 1)] − [(𝑛 − 2)(𝑛 − 1)]
=
2
(𝑛 − 1)[2(𝑛 − 2) − (𝑛 − 2)]
=
2
(𝑛 − 1)𝑛 1 1
= 1= 𝑛( − 𝑛2
2 2 2
Thus, selection sort is 𝑂(𝑛( ) on all inputs. Note, however, that the number of swaps is n
–1, exactly one swap for each iteration of i. Thus, the complexity of swaps is 𝑂(𝑛).
Space complexity is 𝑂(1) because the sorting happens in place and only an extra
variable is used.
The insertion sort is an unstable algorithm, it does not preserve the relative ordering of
the equal elements in the original list when sorting.
BUBBLE SORT
The bubble sort is another brute force approach to sorting order-able lists. The bubble
sort compares adjacent elements of the list and swaps them if they are out of order.
By repeatedly doing so, we end up ‘bubbling up’ the largest element to its final
position in the list.
5 3 2 4 1
The elements that are being compared are highlighted in red and elements in their
final positions are shown in green.
1. Starting from the first index, compare the first two elements. If the first element
is greater than the second element, exchange them.
>
5 3 2 4 1
3 5 2 4 1
swap
2. Next, compare the second and third elements and swap them if the second
element is larger.
>
3 5 2 4 1
3 2 5 4 1
swap
This process goes on till the end of the unsorted list when the last and second
to the last elements in the list are compared.
> >
3 2 5 4 1 3 2 4 5 1
3 2 4 5 1 3 2 4 1 5
swap swap
3 2 4 1 5
After the 2nd pass (before i increases when i = 1), the list: [2, 3, 1, 4, 5]
2 3 1 4 5
After the 3rd pass (before i increases when i = 2) , the list: [2, 1, 3, 4, 5]
2 1 3 4 5
After the (n–1)th pass (before i increases when i = n – 2), the list: [1, 2, 3, 4, 5]
1 2 3 4 5
After the nth pass (before i increases when i = n – 1), the list: [1, 2, 3, 4, 5]
1 2 3 4 5
Bubble sort works by progressively maintaining two sublists until the entire list is sorted:
(1) consists of the largest elements already sorted, at the back of the list and (2)
consists of the remaining elements that need sorting, at the front of the list.
Again, we assume the list as an array with the first index at 0 and the last index at n –
1. On the ith pass, the list scans from the start of the array through the remaining n – i
unsorted elements and finds the largest element among them. Whatever element is
found to be the largest element is pushed to its final position at the end of the unsorted
subarray.
?
A[0], …, A[j] ↔A[j + 1], …, A[n – 1 – i] | A[n – i] ≤ A[n – i + 1] ≤ … ≤ A[n – 1]
Algorithm BubbleSort(A)
Input: an array, A[0 … n – 1], with n order-able items.
Output: The array, A, sorted in non-decreasing order.
START
1. for( i = 0; i < n - 1; i++ ) //for i = 0 to n – 2
2. for( j = 0; j < n – 1 - i; j++ ) //for j = 0 to n – 2 – i
3. if( A[j + 1] < A[j] )
4. Swap( A, j, j + 1 ) //swap A[j+1] with A[j]
5. return A
END
Correctness Proof
Let A´ denote the output of BubbleSort(A).
To prove that BubbleSort is correct, we need to prove that it terminates and that:
where n = A.length.
a. We precisely state a loop invariant for the inner for loop in lines 2—4 and prove
that this loop invariant holds.
b. Using the termination condition of the loop invariant proved in part (a), we will
precisely state a loop invariant for the outer for loop in lines 1—4 that will allow
us to prove the inequality.
Theorem:
A´ consists of the elements in A but is sorted in non-decreasing order.
Proof:
Inner Loop invariant (for loop of lines 2–4)
At the start of each iteration of the inner loop, (1) the subarray A[0 … j] consists
of the elements originally in A[0 … j] before entering the inner loop but possibly
in a different order and (2) the last element A[j] of the subarray is the largest in
it.
Maintenance: From the proof of the inner loop, after the execution of the inner
loop, A[n – 1 – i] will be the largest element of the subarray A[0 … n – 1 – i].
And at the beginning of the outer loop, the subarray A[n – i … n – 1] contains
elements, in sorted order, that are larger than the elements of A[0 … n – 1 – i].
After the execution of the outer loop, the subarray A[n – 1 – i … n – 1] will
consists of elements, in sorted order, that are larger than the elements of A[0 …
n – i – 2]. This implies that at the end of the loop:
Termination: The loop terminates when i = A.length – 1. At that point, array A[0
… n – 1] will contain all the elements sorted in non-decreasing order. Because,
from the maintenance phase of the loop invariant we know that at the end of
an iteration of the outer loop, the subarray A[n – 1 – i … n – 1] consists of sorted
elements. Substituting n – 1 for i (n – 1 – (n – 1) = n – 1 – n + 1 = 0), we have
that the subarray A[0 … n – 1] consists of the original elements, but in sorted
order. This is the entire array, so the entire array is sorted.
Note, the steps above in proving the loop invariants constitute an induction proof,
which shows that when the program leaves the loop:
Efficiency Analysis
The bubble sort, like the selection sort, has a straightforward analysis. The input size is
given by the number of elements in the array (list), n; the basic operation is the key
comparison A[j+1] < A[j] (on line 3). The number of times it is executed depends only
on the input size and is given by the following sum:
!"( !"("&
=𝑛−1−𝑖
!"(
∴ 𝐶(𝑛) = '(𝑛 − 1 − 𝑖)
&%)
!"( (!"()'#
'1= ' 1 = (𝑛 − 1)
&%) &%#
!"( !"(
(𝑛 − 2) ⋅ [(𝑛 − 2) + 1] (𝑛 − 2)(𝑛 − 1)
'𝑖 = '𝑖 = =
2 2
&%) &%#
!"(
(𝑛 − 2)(𝑛 − 1)
∴ '(𝑛 − 1 − 𝑖) = [𝑛(𝑛 − 1)] − [𝑛 − 1] − 1 2
2
&%)
(𝑛 − 2)(𝑛 − 1)
= [(𝑛 − 1)(𝑛 − 1)] − 1 2
2
[2(𝑛 − 1)(𝑛 − 1)] − [(𝑛 − 2)(𝑛 − 1)]
=
2
(𝑛 − 1)[2(𝑛 − 2) − (𝑛 − 2)]
=
2
(𝑛 − 1)(2𝑛 − 2 − 𝑛 + 2)
=
2
(𝑛 − 1)𝑛 1 1
= 1= 𝑛( − 𝑛2
2 2 2
Thus, bubble sort belongs to the complexity class 𝑂(𝑛( ). However, the number of key
swaps depends on the input. In the worst case of a non-increasing sorted array, it is
the same as the number of key comparisons. Thus, the complexity of swaps is 𝑂(𝑛( ).
Space complexity is 𝑂(1) because the sorting happens in place and only an extra
variable is used.
The bubble sort is a stable algorithm, it maintains the original ordering of the identical
elements when sorting.
As is often the case when using a brute-force strategy, the first version of an algorithm
obtained can often be improved upon with a modest amount of effort. Considering
our bubble sort algorithm above, we can improve on it by exploiting the following
observation: if a pass through the list makes no swaps, then the list has been sorted
and we can stop the algorithm. Though this improved version runs faster on some
inputs, it is still in Θ(𝑛( ) in the worst and average cases. Also, even among elementary
sorting methods, bubble sort is an inferior choice.
INSERTION SORT
Insertion sort is a sorting algorithm that places an unsorted element at its suitable place
in each iteration. Insertion sort applies the decrease and conquer approach to sort a
list. Specifically, it uses the decrease-by-one technique, it assumes a smaller problem
of the list has already been solved, leveraging this solution to the smaller problem, for
the next unsorted element, it finds its appropriate place among the sorted elements,
and it inserts it there.
Let’s use our earlier problem instance [5, 3, 2, 4, 1] to explain how insertion sort works.
5 3 2 4 1
Elements in their final positions (in the sorted sublist) are highlighted in green. The
element currently stored in key is highlighted in red. The element that is currently being
compared to key is shown with a red-bordered. The hole (empty slot) created by
shifting an element to the right is shown as greyed out. A red bar indicates the division
between the sublist being sorted (to the left) and the remaining elements that have
not been considered yet.
key key
5 3 2 4 1 5 3 2 4 1
3
copy
2. Compare key with the first element. If the first element is greater than key, then
key is placed in front of the first element otherwise key is placed behind it. Now
the first two elements are sorted.
key key
5 3 2 4 1 5 5 2 4 1
3 3
>
move
key
3 5 2 4 1
3
insert
At end of the 1st pass (before i increases when i = 1), the list: [3, 5 | 2, 4, 1]
key
3 5 2 4 1
3
3. Now, the next unsorted element (the third) is taken as the new key, and we
compare it to the elements to the left of it, starting with the one immediately
preceding it. If we find an element smaller than it, we place it just behind that
element. Otherwise, if there is no element smaller than it, we then place it at
the beginning of the array.
key
3 5 2 4 1
2
copy
>
move
key key
3 5 2 4 1 3 5 5 4 1
2 2
>
move
key
2 3 5 4 1
2
insert
At the end of 2nd pass (before i increases when i = 2), the list: [2, 3, 5 | 4, 1]
key
2 3 5 4 1
2
key
2 3 5 4 1
4
copy
key key
2 3 5 4 1 2 3 5 5 1
4 4
>
move
key key
2 3 5 5 1 2 3 4 5 1
4 4
> insert
At the end of 3rd pass (before i increases when i = 3), the list: [2, 3, 4, 5 | 1]
key
2 3 4 5 1
4
key key
2 3 4 5 1 2 3 4 5 5
1 1
>
move
key key
2 3 4 5 5 2 3 4 4 5
1 1
>
move
key key
2 3 4 4 5 2 3 3 4 5
1 1
>
move
key key
2 3 3 4 5 2 2 3 4 5
1 1
>
move
key
1 2 3 4 5
1
insert
At the end of the 4th pass, i.e., the (n – 1)th pass (before i increases when i = 4
i.e., i = n – 1), the list: [1, 2, 3, 4, 5 |]
key
1 2 3 4 5
1
It is clear to see that insertion sort is based on a recursive idea, however, it is more
efficient to implement this iteratively. Starting with A[1] and ending with A[n – 1], A[i] is
inserted in its appropriate place among the first i elements of the array that have
already been sorted. However, unlike selection sort, these are generally not their final
positions.
Algorithm InsertionSort(A)
Input: an array, A[0 … n – 1], with n order-able items.
Output: The array, A, sorted in non-decreasing order.
START
1. for( i = 1; i < n; i++ ) //for i = 1 to n – 1
2. key = A[i]
3. j = i – 1
4. while( j >= 0 && A[j] > key)
5. A[j + 1] = A[j]
6. j = j – 1
7. A[j + 1] = key
8. return A
END
Correctness Proof
Let A´ denote the output of InsertionSort(A).
To prove that InsertionSort is correct, we need to prove that it terminates and that:
where n = A.length.
a. We precisely state a loop invariant for the inner while loop in lines 4—6 and
prove that it holds.
b. Using the termination condition of the loop invariant proved in part (a), we will
precisely state a loop invariant for the outer for loop in lines 1—7 that will allow
us to prove the inequality.
Theorem:
A´ consists of the elements in A but is sorted in non-decreasing order.
Proof:
Inner Loop invariant (while loop of lines 4–6)
At the start of each iteration of the inner loop, each element of the subarray
A[j … i] is greater than or equal to key, A[j … i] ≥ key.
Maintenance: From the proof of the inner loop, after the execution of the inner
loop, A[0 … j] is sorted and is at most equal to key, A[0 … j] ≤ key and A[j + 2
… i] is sorted and is at least equal to key, A[j + 2 … i] ≥ key. The inner loop does
not destroy data in A, because, in the first iteration of the outer loop, A[i] is
copied into key. On line 7, key is copied into A[j + 1]. Hence, we get A[0 … i],
which is a sorted permutation of the original i + 1 elements of A. Because key
is restored to A, after the execution of the outer loop, we maintain the invariant
A[0 … i – 1] contains the first i elements of the original array.
Termination: The loop terminates when i = A.length. At that point, array A[0 …
n – 1] will contain all the elements sorted in non-decreasing order. Because,
from the maintenance phase of the loop invariant we know that at the end of
an iteration of the outer loop, the subarray A[0 … i – 1] consists of sorted
elements. Substituting n for i, we have that the subarray A[0 … n – 1] consists
of the original elements, but in sorted order. This is the entire array, so the entire
array is sorted.
Note, the steps above in proving the loop invariants constitute an induction proof,
which shows that when the program leaves the loop:
Efficiency Analysis
The input size is given by the number of elements in the array (list), n; the basic
operation is on line 4. We go with key comparison A[j] > key instead of j ≥ 0. The reason
why we did this is simple, j ≥ 0 will execute faster than A[j] > key in an actual
implementation because the latter will require indexing a memory location to
The number of times the basic operation is executed depends on the nature of the
input because the number of key comparisons, A[j] > key, depends on the nature of
the input. We do not know exactly how many times this will be, so let's assume line 4 is
executed t number of times. Since we are interested in finding the big-oh of the basic
operation, we will derive an upper bound for t. So, for fixed i, i.e., for one iteration of
the outer loop, let ti be the maximum number of times line 4 can be executed. If the
array is in decreasing sorted order, it results in the worst case. We must compare each
element A[i ] with each element in the entire sorted subarray A[0 … i – 1], and so ti = i
for i = 1, 2, 3, …, n – 1.
Thus, the cost for running the basic operation is given by the following sum:
!"#
𝐶(𝑛) = ' 𝑖
&%#
(𝑛 – 1) ⋅ 7(𝑛 − 1) + 18 1
= = 𝑛(𝑛 − 1)
2 2
Thus, insertion sort belongs to the complexity class 𝑂(𝑛( ). However, the number of
moving keys depends on the input. In the worst case of a non-increasing sorted array,
it is the same as the number of key comparisons. Thus, the complexity of moving
elements is Θ(𝑛( ). The best case occurs when the array is already sorted. In the best
case, the comparison A[j] > key is executed only once in each iteration of the outer
loop. Thus, ti = 1 for i = 1, 2, 3, …, n – 1. Therefore, for sorted arrays, the number of
key comparisons is
!"#
𝐶(𝑛) = ' 1 = 𝑛 − 1
&%#
Thus, in the best case, insertion sorts belong to the complexity class Θ(𝑛). The average
case occurs when the array elements are in jumbled order (neither non-decreasing
nor non-increasing). The time complexity in the average case is the same as in the
worst case, Θ(𝑛( ). Thus, the time complexities for insertion sort are:
Space complexity is 𝑂(1) because the sorting happens in place and only an extra
variable is used.
Insertion sort is a stable algorithm, it maintains the original ordering of the identical
elements when sorting.
SUMMARY
• The three elementary algorithms presented here are mainly used for the
internal sorting of small data and are comparison-based. In comparison-based
sorting algorithms, we compare elements to determine the order of elements
in the final sorted output.
o Selection sort compares elements to place the smallest elements at the
front of the list.
o Bubble sort compares elements to push the largest elements to the end
of the list.
o Insertion sort compares elements to decide the position of an element
in a partially sorted subarray.
• In addition to the comparison operations, other types of operations are also
performed in these sorting algorithms. But the number of times these operations
execute would always be less than that of the comparison operations. That is
why comparison operation is the determining factor of the time complexity.
o Bubble sort and selection sorts perform swap operations.
o Insertion sort performs shift operations.
• The table below summarises the properties and complexities of these
algorithms.
UNIT GOALS
At this point (after completing this unit) you should be able to:
EXERCISES
a. How does the algorithm sort the elements, and what is the name of the
algorithm?
b. How many loop invariants does the algorithm maintain? State the loop
invariant(s).
c. Give this algorithm's best-case and worst-case running times in Θ–
notation.
4. Given an optimised bubble sort algorithm that can detect when a list a
completely sorted before (n-1)th pass and stop the sorting process in such
instance. In how many iterations would the algorithm sort the following lists?
a. [3, 4, 5, 2, 1]
b. [1, 2, 3, 4, 5]
c. How many iterations would it take to sort the lists in (a)–(b) using selection
sorting?
5. You have a small array on a flash drive to sort. However, the “memory-write” is
a costly operation.
a. Select an appropriate elementary sort algorithm to sort this small list.
b. What operations does this algorithm use to move numbers from the
unsorted sublist to the sorted list section?
c. How many movements would be needed to completely sort a list in the
worst-case and the best-case scenarios?
6. Given the following list: [7, 1, 6, 2, 4, 8, 5, 3]. Apply the sorting algorithms
indicated in (a) and (b) to the list and do as the questions instructed.
a. Show the state of the list after each swap during a selection sort. To the
right side of the state you provide, indicate all the candidates
considered for the relevant minimum value for the pass in question.
b. Show the state of the list after each “insertion” during an insertion sort.
To the right side of the state you provide, indicate how many shifts were
made to accomplish the insertion in question. You do not need to show
the movements, you only need to mention the total number of
movements that occur during the considered pass.
7. When is an insertion sort better than a selection sort?
8. When is a selection sort better than an insertion sort?
9. A new sorting algorithm, called “pair-sort”, is used to sort pairs of positive
integers by the magnitude of the product they form. For example, the pair (3,5)
form the product, 15 (i.e., 3 x 5).
“pair-sort” is applied to the following list of pairs: [(2,9), (7,3) (1,10) (3,6) (2,4),
(2,5)]. And the following list of pairs is the output produced: [(2,4), (2,5), (1,10),
(3,6), (2,9), (7,3)].
Is “pair-sort” a stable sort? Justify your answer.
Textbooks
[1] A. Levitin, Introduction to the Design and Analysis of Algorithms, Boston: Pearson,
2012.
Websites