Basic Sorts
Basic Sorts
Analysis of Algorithms
Searching & Sorting: Part 2
Ibrahim Albluwi
Sorting: A Fundamental Problem
Problem. Given a list of n elements, order them in non-decreasing (or ascending) order.
Common variant. Order the elements in descending order.
Requirement. e meaning of <, >, == for the element type must be defined.
In C++, it is defined for int, double, char, string, etc.,
but not for user defined types (e.g. What does car1 > car2 mean?)
Problem. Given a list of n elements, order them in non-decreasing (or ascending) order.
Common variant. Order the elements in descending order.
Requirement. e meaning of <, >, == for the element type must be defined.
In C++, it is defined for int, double, char, string, etc.,
but not for user defined types (e.g. What does car1 > car2 mean?)
M N Z C B A T U V S
Idea 1. Select the min and place it in its correct position, then the second min, etc.
Idea 2. Go through each element and insert it in its correct position relative to its le.
Idea 3. Bubble Sort!
Selection Sort: Implementation
i
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
if (i != min_index)
place the minimum
in its right position
swap(a[i], a[min_index]);
}
}
Selection Sort: Implementation
i
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
if (i != min_index)
place the minimum
in its right position
swap(a[i], a[min_index]);
}
}
Selection Sort: Implementation
i
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
i j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
i j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
5 3 0 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 18 48 25 31 32 40 12
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 48 25 31 32 40 18
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 48 25 31 32 40 18
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 12 48 25 31 32 40 18
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 12 48 25 31 32 40 18
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 12 48 25 31 32 40 18
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 12 48 25 31 32 40 18
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
if (i != min_index)
swap(a[i], a[min_index]);
}
}
Selection Sort: Tracing
i j
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
min_index
i
0 3 5 12 18 25 31 32 40 48
0 1 2 3 4 5 6 7 8 9
if (i != min_index)
swap(a[i], a[min_index]);
}
}
void selection(int a[], int n) { Analysis
for (int i = 0; i < n-1; i++) {
int min_index = i;
for (int j = i+1; j < n; j++) find the index of the
if (a[j] < a[min_index]) minimum in a[i, n-1]
min_index = j;
Data compares.
Data compares. e algorithm is insensitive to the arrangement of the elements in the array.
n−1
1
∑
= 1 + 2 + 3 + … + (n − 1) = i = 2 n(n − 1) data compares
i=1
void selection(int a[], int n) { Analysis
for (int i = 0; i < n-1; i++) {
int min_index = i;
for (int j = i+1; j < n; j++) find the index of the
if (a[j] < a[min_index]) minimum in a[i, n-1]
min_index = j;
Data compares. e algorithm is insensitive to the arrangement of the elements in the array.
n−1
1
∑
= 1 + 2 + 3 + … + (n − 1) = i = 2 n(n − 1) data compares
i=1
Data Moves.
Worst case. One swap per iteration, a total of n − 1 swaps (= 3(n − 1) data moves).
Best case. No swaps if the array is already sorted.
Data compares. e algorithm is insensitive to the arrangement of the elements in the array.
n−1
1
∑
= 1 + 2 + 3 + … + (n − 1) = i = 2 n(n − 1) data compares
i=1
Data Moves.
Worst case. One swap per iteration, a total of n − 1 swaps (= 3(n − 1) data moves).
Best case. No swaps if the array is already sorted.
Think!
Can you come up with an array
of size 6 that leads to 5 swaps?
void selection(int a[], int n) { Analysis
for (int i = 0; i < n-1; i++) {
int min_index = i;
for (int j = i+1; j < n; j++) find the index of the
if (a[j] < a[min_index]) minimum in a[i, n-1]
min_index = j;
Data compares. e algorithm is insensitive to the arrangement of the elements in the array.
n−1
1
∑
= 1 + 2 + 3 + … + (n − 1) = i = 2 n(n − 1) data compares
i=1
Data Moves.
Worst case. One swap per iteration, a total of n − 1 swaps (= 3(n − 1) data moves).
Best case. No swaps if the array is already sorted.
Total. O(n 2) operations in the best case and the worst case.
Insertion Sort: Implementation
i
0 -1 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
temp = -1
i
0 -1 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
}
Insertion Sort: Implementation
temp = -1
j i
0 -1 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
a[j+1] = temp;
}
}
Insertion Sort: Implementation
temp = -1
j i
0 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
a[j+1] = temp;
}
}
Insertion Sort: Implementation
temp = -1
j i
0 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
a[j+1] = temp;
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 3
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 5
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 6
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 5 6 2 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 5 6 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 5 6 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 5 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 5 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 3 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 2
j i
-1 0 3 3 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 2 3 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 2 3 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 4
j i
-1 0 2 3 5 6 4 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 4
j i
-1 0 2 3 5 6 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 4
j i
-1 0 2 3 5 6 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 4
j i
-1 0 2 3 5 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 4
j i
-1 0 2 3 5 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 9
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 40
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 31
j i
-1 0 2 3 4 5 6 9 40 31
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 31
j i
-1 0 2 3 4 5 6 9 40 40
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp = 31
j i
-1 0 2 3 4 5 6 9 40 40
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
temp =
j i
-1 0 2 3 4 5 6 9 31 40
0 1 2 3 4 5 6 7 8 9
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
Insertion Sort: Implementation
i
-1 0 2 3 4 5 6 9 31 40
0 1 2 3 4 5 6 7 8 9
sorted
int j = i-1;
while (j >= 0 && temp < a[j]) {
shi the elements until
a[j+1] = a[j]; the right position of
j--; element i is found
}
place the element in
a[j+1] = temp; its right position
}
}
void insertion(int a[], int n) { Analysis
for (int i = 1; i < n; i++) { 5 4 3 2 1
Example
1 2 3 5 4 6 7 10 8 9 11 13 12
[Optional Info] Insertion sort performs a number of shis that is equal to the number of
inversions. A sorted array has 0 inversions, a partially sorted array has a number of inversions
1
that is linear in the size of the array and a reversely sorted array has 2 n(n − 1) inversions.
Best Case. Sorted arrays.
Analysis
Data compares. n − 1 (each element is compared to the one to its le) 1 2 3 4 5
Number of shis. 0 (all elements are in their place) 1 2 3 4 5
Data moves. Number of shis + 2(n − 1) 1 2 3 4 5
For moving a[i] to temp and then back to its place.
1 2 3 4 5
Total. O(n)
1 2 3 4 5
i j
8 16 2 4 0 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 2 4 0 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 2 4 0 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 2 4 0 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 2 0 4 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 2 0 4 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 0 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
8 16 0 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
8 0 16 2 4 52 91
0 1 2 3 4 5 6
j-1
i j
8 0 16 2 4 52 91
0 1 2 3 4 5 6
j-1
i j
0 8 16 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 8 16 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 8 16 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 8 16 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 8 16 2 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 8 2 16 4 52 91
0 1 2 3 4 5 6
j-1
i j
0 8 2 16 4 52 91
0 1 2 3 4 5 6
j-1
i j
0 2 8 16 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 2 8 16 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 2 8 16 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 2 8 16 4 52 91
0 1 2 3 4 5 6
i j-1 j
0 2 8 4 16 52 91
0 1 2 3 4 5 6
j-1
i j
0 2 8 4 16 52 91
0 1 2 3 4 5 6
j-1
i j
0 2 4 8 16 52 91
0 1 2 3 4 5 6
i j-1 j
0 2 4 8 16 52 91
0 1 2 3 4 5 6
i j-1 j
0 2 4 8 16 52 91
0 1 2 3 4 5 6
j-1
i j
0 2 4 8 16 52 91
0 1 2 3 4 5 6
i No Swaps!
This means that the
0 2 4 8 16 52 91 remaining elements
are already sorted
0 1 2 3 4 5 6
i No Swaps!
This means that the
0 2 4 8 16 52 91 remaining elements
are already sorted
0 1 2 3 4 5 6
1 1
O(n) O(n) O(n ) 2 O(n 2) n(n − 1) n(n − 1) O(n) O(n)
Insertion
4 4
shis
O(n) O(n 2)
O(n)
Sorted Arrays Reversely Sorted O(n 2)
Arrays
1 1
O(n) O(n) O(n ) 2 O(n 2) n(n − 1) n(n − 1) O(n) O(n)
Insertion
4 4
shis
O(n) O(n 2)
O(n)
Sorted Arrays Reversely Sorted O(n 2)
Arrays
1 1
O(n) O(n) 2
O(n ) O(n 2) n(n − 1) n(n − 1) O(n) O(n)
Insertion
4 4
shis
O(n) O(n 2)
O(n)
Sorted Arrays Reversely Sorted O(n 2)
Arrays
1 1
O(n) O(n) O(n ) 2 O(n 2) n(n − 1) n(n − 1) O(n) O(n)
Insertion
4 4
shis
O(n) O(n 2)
O(n)
Sorted Arrays Reversely Sorted O(n 2)
Arrays
1 1
O(n) O(n) O(n ) 2 O(n 2) n(n − 1) n(n − 1) O(n) O(n)
Insertion
4 4
shis
O(n) O(n 2)
O(n)
Sorted Arrays Reversely Sorted O(n 2)
Arrays
1 1
O(n) O(n) O(n ) 2 O(n 2) n(n − 1) n(n − 1) O(n) O(n)
Insertion
4 4
shis
O(n) O(n 2)
O(n)
Sorted Arrays Reversely Sorted O(n 2)
Arrays
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
m m
How many data compares does selection sort perform if run on such an array of size 2m ?
Exercise # 1
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
m m
How many data compares does selection sort perform if run on such an array of size 2m ?
1
Answer. Selection sort always does 2
n(n − 1) data compares if the array is of size n,
regardless of how the elements are ordered in the array.
1
e size of the array is 2m. erefore, selection sort performs 2
2m(2m − 1)
= m(2m − 1) = 2m 2 − m data compares.
Exercise # 2
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
m m
How many swaps does bubble sort perform if run on such an array of size 2m ?
Exercise # 2
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
m m
How many swaps does bubble sort perform if run on such an array of size 2m ?
Answer.
e 1st pass swaps the right-most 1 with 2m − 2 elements.
e 2nd pass swaps the right-most 2 with 2m − 4 elements.
e 3rd pass swaps the right-most 3 with 2m − 6 elements.
…
e right-most 6 is swapped with 4 elements.
e right-most 7 is swapped with 2 elements.
e right-most 8 is swapped with 0 elements. All the remaining elements
will not need extra swaps for them to get to their positions
(swaps from the previous passes of the algorithm get them to their positions).
Answer.
First half: m-1 compares. Each element is compared to the one to its le.
Second half: e 8 is compared to the 8 to its le (1 compare).
e 7 is compared to the 7, 8, 8 to its le (3 compares).
e 6 is compared to the 6, 7, 7, 8, 8 to its le (5 compares).
…
Finally, the 1 is compared to all the remaining 2m-1 elements.
e total is: 1 + 3 + 5 + ... + 2m-1
= (0+1) + (2+1) + (4+1) + ... + 2m-2+1
= m + 0 + 2 + 4 + ... + 2m-2
= m + 2(0 + 1 + 2 + ... + m-1)
= m + m(m-1) = m2
Adding the compares from the first half, we get a total of m 2 + m − 1 compares.
Exercise # 4
Q. Assume that selection sort knows how to find the minimum in a range of size m
in log2 m comparisons only. What would be the order of growth of the running time
of selection sort if run on an array of size n ?
Q. Assume that selection sort knows how to find the minimum in a range of size m
in log2 m comparisons only. What would be the order of growth of the running time
of selection sort if run on an array of size n ?
Q. Assume that insertion sort uses binary search to find the insertion position in the
sorted portion of the array. Does this affect the worst case running time of the
algorithm?
insertion-sort(a[], n):
A. No.
for every i from 1 to n-1:
B. Affects the actual running insert a[i] in the range 0 to i-1
time but not the using linear search and shifts
asymptotic running time.
Q. Assume that insertion sort uses binary search to find the insertion position in the
sorted portion of the array. Does this affect the worst case running time of the
algorithm?
insertion-sort(a[], n):
A. No.
for every i from 1 to n-1:
B. Affects the actual running insert a[i] in the range 0 to i-1
time but not the using linear search and shifts
asymptotic running time.
Number of data compares becomes: O(lg(1) + lg(2) + lg(3) + … + lg(n − 1)) = O(n log n)
Number of data moves remains O(n 2)