0% found this document useful (0 votes)
7 views

Lecture03 - Analysis of Sorting Algorithms - Part1

The document provides an analysis of various sorting algorithms, including Selection Sort, Insertion Sort, and Bubble Sort. It includes a series of review questions focused on analyzing code snippets to determine their time complexities. Each question presents a different code structure, challenging the reader to identify the correct time complexity from multiple-choice options.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture03 - Analysis of Sorting Algorithms - Part1

The document provides an analysis of various sorting algorithms, including Selection Sort, Insertion Sort, and Bubble Sort. It includes a series of review questions focused on analyzing code snippets to determine their time complexities. Each question presents a different code structure, challenging the reader to identify the correct time complexity from multiple-choice options.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 172

Analysis

Sorting Algorithms I

Mohammed Alqmase
Outline

➢ Overview
➢ Selection Sort Algorithm
➢ Insertion Sort Algorithm
➢ Bubble Sort Algorithm
Review Questions
Q1
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
// constant time operation
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q2
Analyze the following code and determine its time complexity:
for (int i = 1; i < n; i*= 2) {
// constant time operation
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q3
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
// constant time operation
}

for (int j = 0; j < n; j++) {


// constant time operation
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q4
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
// constant time operation
}

for (int j = 0; j < n; j*=2) {


// constant time operation
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q5
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q6
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q7
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q8
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j*= 2) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q9
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j*= 2) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒍𝒐𝒈𝒏 )
D) O( 𝒏 𝒍𝒐𝒈𝒏 )
Q10
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
// constant time operation
}
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒏𝟑 )
D) O( 𝒏𝟒 )
Q11
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n*n; j++) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒏𝟑 )
D) O( 𝒏𝟒 )
Q12
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n+n; j++) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒏𝟑 )
D) O( 𝒏𝟒 )
Q13
Analyze the following code and determine its time complexity:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n+n; j++) {
// constant time operation
}
}

A) O( 𝒏 )
B) O( 𝒏² )
C) O( 𝒏𝟑 )
D) O( 𝒏𝟒 )
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
}
}

for ( i=0; i<n; i++) {


for ( j=0; j<i; j++ ) {
//comparison
}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 n
1 n
for ( i=0; i<n; i++) { 2 n
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n

}
𝑛 + 𝑛 + 𝑛 + ⋯ + 𝑛 = 𝑛2
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1

}
}
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1
𝑛−1
}
෍𝑖 = 0 + 1 + 2 + ⋯ + (𝑛 − 1)
} 𝑖=0
0 + 1 + 2 + ⋯ + (𝑛 − 1)
𝒊
for ( i=0; i<n; i++ ) {

for ( j=0; j<n; j++) { 0 1 2 … n-1

//comparison
𝒋
}
} i No. of Comparison
0 0
1 1
for ( i=0; i<n; i++) { 2 2
.
for ( j=0; j<i; j++ ) { .
.
//comparison n-1 n-1
𝑛−1
} 𝑛(𝑛 − 1)
෍𝑖 =
2
} 𝑖=0
𝒊
for ( i=0; i<n; i++) {

for ( j=0; j<i; j++ ) { 0 1 2 … n-1

//comparison 1
}
𝒋
i No. of No. of
Comparison 1 Comparison 2
for ( j=i; j<n; j++ ) { 0 0 n
1 1 n-1
//comparison 2
2 2 n-2
}
.
} .
.
𝑛−1
n-3 n-3 2
𝑛(𝑛 − 1) n-2 n-2 1
෍𝑖 =
2
𝑖=0 n-1 n-1 0
𝒊
for ( i=0; i<n; i++) {

for ( j=0; j<i; j++ ) { 0 1 2 … n-1

//comparison 1
}
𝒋
i No. of No. of
Comparison 1 Comparison 2
for ( j=i; j<n; j++ ) { 0 0 n
1 1 n-1
//comparison 2
2 2 n-2
}
.
} .
.
//comparison 1 //comparison 2 n-3 n-3 2
𝑛−1 𝑛−1
𝑛(𝑛 − 1) n-2 n-2 1
෍𝑖 = 𝑛(𝑛 − 1)
෍𝑖 = n-1 n-1 0
𝑖=0
2 2
𝑖=0
Selection Sort Algorithm
Selection Sort Algorithm

How Does it Work?


𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

𝒋
𝒊

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

𝒋
𝒊

𝟏 𝟑 𝟓 𝟔 𝟕 𝟖

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟔 𝟕 𝟖

𝒋
𝟏 𝟑 𝟓 𝟔 𝟕 𝟖
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[j] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[j] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[j] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟖 𝟓 𝟕 𝟔 𝟑

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
𝒊
𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡

𝟏 𝟑 𝟓 𝟕 𝟔 𝟖

Selection-Sort(arr, n): 𝒋
for ( i = 0; i<n-1; i++ )
smallest = i;
for ( j=i+1; j<n; j++ )
if ( arr[j] < arr[smallest] )
smallest = j;
swap arr[i] and arr[smallest]
Selection Sort Algorithm

Analysis
Analysis: In the best case:
𝑓 𝑛 = 4𝑚 + 9𝑛 − 4
Selection-Sort(arr, n):
𝑛 𝑛−1
𝑓 𝑛 =4 + 9𝑛 − 4
1 n 2n-2 2
for ( i = 0; i<n-1; i++ ) 𝑛2 − 𝑛
𝑓 𝑛 =4 + 9𝑛 − 4
2

n-1 smallest = i; = 𝑂(𝑛2 )

2n-2 m+1 2m
for ( j=i+1; j<n; j++ )

m if ( arr[j] < arr[smallest] )

In the worst case m smallest = j; 0 In the best case

3n-3 swap arr[i] and arr[smallest]


Analysis: In the worst case:
𝑓 𝑛 = 5𝑚 + 9𝑛 − 4
Selection-Sort(arr, n):
𝑛 𝑛−1
𝑓 𝑛 =5 + 9𝑛 − 4
1 n 2n-2 2
for ( i = 0; i<n-1; i++ ) 𝑛2 − 𝑛
𝑓 𝑛 =5 + 9𝑛 − 4
2

n-1 smallest = i; = 𝑂(𝑛2 )

2n-2 m+1 2m
for ( j=i+1; j<n; j++ )

m if ( arr[j] < arr[smallest] )

In the worst case m smallest = j; 0 In the best case

3n-3 swap arr[i] and arr[smallest]


Insertion Sort Algorithm
Insertion Sort Algorithm

How Does it Work?


𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟖

𝟔 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟕

𝟓 𝟔 𝟖 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟕

𝟓 𝟔 𝟖 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟕

𝟓 𝟔 𝟖 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟕 𝟖 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟕 𝟖 𝟏 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟏

𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟏 𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟏 𝟓 𝟔 𝟕 𝟖 𝟑

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟑

𝟏 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟏 𝟑 𝟓 𝟔 𝟕 𝟖

𝒋 𝒊
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟏 𝟑 𝟓 𝟔 𝟕 𝟖
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟖

𝟔 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟓

𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟖 𝟕 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟕

𝟓 𝟔 𝟖 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟕

𝟓 𝟔 𝟖 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 = 𝟕

𝟓 𝟔 𝟖 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 =

𝟓 𝟔 𝟕 𝟖 𝟏 𝟑

𝒋 𝒊
InsertionSort(arr, n)
for ( i=1; i<n; i++)
current = arr[i];

j=i-1;
while ( j>=0 && arr[j] > current )
arr[j+1] = arr[j];
j=j-1;

arr[j+1] = current;
Insertion Sort Algorithm

Analysis
Analysis of Insertion Sort Algorithm
InsertionSort(arr, n)

for ( i=1; i<n; i++)

current = arr[i];

j=i-1;

while ( j>=0 && arr[j] > current )

arr[j+1] = arr[j];

j=j-1;

arr[j+1] = current;
InsertionSort(arr, n)
1 n 2n-2
for ( i=1; i<n; i++)

current = arr[i]; n-1

j=i-1; 2n-2 Worst case Best case

while ( j>=0 && arr[j] > current ) 3m 3n-3

arr[j+1] = arr[j]; 2m 0

j=j-1; 2m 0

arr[j+1] = current; 2n -2
InsertionSort(arr, n)
Analysis: In the best case:
1 n 2n-2 𝑓(𝑛) = 11𝑛 − 9
for ( i=1; i<n; i++) = 𝑂(𝑛)

current = arr[i]; n-1

j=i-1; 2n-2 Worst case Best case

while ( j>=0 && arr[j] > current ) 3m 3n-3

arr[j+1] = arr[j]; 2m 0

j=j-1; 2m 0

arr[j+1] = current; 2n -2
Analysis: In the worst case:
𝑓 𝑛 = 7𝑚 + 11𝑛 − 9
InsertionSort(arr, n) 𝑛 𝑛−1
𝑓 𝑛 =7 + 11𝑛 − 9
1 n 2n-2 2
for ( i=1; i<n; i++)
𝑛2 − 𝑛
𝑓 𝑛 =7 + 11𝑛 − 9
2
current = arr[i]; n-1
= 𝑂(𝑛2 )

j=i-1; 2n-2 Worst case Best case

while ( j>=0 && arr[j] > current ) 3m 3n-3

arr[j+1] = arr[j]; 2m 0

j=j-1; 2m 0

arr[j+1] = current; 2n -2
Bubble Sort Algorithm
Bubble Sort Algorithm

How Does it Work?


𝒊

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋−𝟏 𝒋
𝒊

𝟔 𝟖 𝟓 𝟕 𝟏 𝟑

𝒋−𝟏 𝒋
𝒊

𝟔 𝟖 𝟓 𝟏 𝟕 𝟑

𝒋−𝟏 𝒋
𝒊

𝟔 𝟖 𝟏 𝟓 𝟕 𝟑

𝒋−𝟏 𝒋
𝒊

𝟔 𝟏 𝟖 𝟓 𝟕 𝟑

𝒋−𝟏 𝒋
𝒊

𝟏 𝟔 𝟖 𝟓 𝟕 𝟑

𝒋−𝟏 𝒋
𝒊

𝟏 𝟔 𝟖 𝟓 𝟑 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟔 𝟖 𝟑 𝟓 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟔 𝟑 𝟖 𝟓 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟑 𝟔 𝟖 𝟓 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟑 𝟔 𝟖 𝟓 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟑 𝟔 𝟓 𝟖 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟑 𝟓 𝟔 𝟖 𝟕

𝒋−𝟏 𝒋
𝒊

𝟏 𝟑 𝟓 𝟔 𝟕 𝟖

𝒋−𝟏 𝒋
𝒊

𝟏 𝟑 𝟓 𝟔 𝟕 𝟖

𝒋−𝟏 𝒋
𝟏 𝟑 𝟓 𝟔 𝟕 𝟖
𝟏 𝟑 𝟓 𝟔 𝟕 𝟖
𝒊

𝟑 𝟓 𝟔 𝟕 𝟏 𝟖

𝒋−𝟏 𝒋

bubbleSort( 𝒂𝒓𝒓 , 𝒏 ):
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )
swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1];
𝒊

𝟑 𝟓 𝟔 𝟕 𝟏 𝟖

𝒋−𝟏 𝒋

bubbleSort( 𝒂𝒓𝒓 , 𝒏 ):
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )
swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1];
𝒊

𝟑 𝟓 𝟔 𝟕 𝟏 𝟖

𝒋−𝟏 𝒋

bubbleSort( 𝒂𝒓𝒓 , 𝒏 ):
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )
swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1];
𝒊

𝟑 𝟓 𝟔 𝟕 𝟏 𝟖

𝒋−𝟏 𝒋

bubbleSort( 𝒂𝒓𝒓 , 𝒏 ):
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )
swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1];
𝒊

𝟑 𝟓 𝟔 𝟕 𝟏 𝟖

𝒋−𝟏 𝒋

bubbleSort( 𝒂𝒓𝒓 , 𝒏 ):
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )
swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1];
𝒊

𝟑 𝟓 𝟔 𝟕 𝟏 𝟖

𝒋−𝟏 𝒋

bubbleSort( 𝒂𝒓𝒓 , 𝒏 ):
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )
swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1];
Bubble Sort Algorithm

Analysis
Analysis: In the worst case:
𝑓 𝑛 = 8𝑚 + 5𝑛 − 2
bubbleSort( 𝒂𝒓𝒓 , 𝒏 ): 𝑓 𝑛 =8
𝑛 𝑛−1
+ 5𝑛 − 2
2

1 n 2n-2 𝑛2 − 𝑛
𝑓 𝑛 =8 + 5𝑛 − 2
2
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
= 𝑂(𝑛2 )

2n-2 m+1 2m
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )

m m
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )

In the worst case 3m swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1]; 0 In the best case
Analysis: In the best case:
𝑓 𝑛 = 5𝑚 + 5𝑛 − 2
bubbleSort( 𝒂𝒓𝒓 , 𝒏 ): 𝑓 𝑛 =5
𝑛 𝑛−1
+ 5𝑛 − 2
2

1 n 2n-2 𝑛2 − 𝑛
𝑓 𝑛 =5 + 5𝑛 − 2
2
for ( 𝑖=0; 𝑖<𝑛-1; i++ )
= 𝑂(𝑛2 )

2n-2 m+1 2m
for ( 𝑗=𝑛-1; 𝑗>𝑖; 𝑗-- )

m m
if ( 𝑎𝑟𝑟[𝑗] < 𝑎𝑟𝑟[𝑗-1] )

In the worst case 3m swap 𝑎𝑟𝑟[𝑗] and 𝑎𝑟𝑟[𝑗-1]; 0 In the best case

You might also like