CSE373L1
CSE373L1
Algorithms
CSE 373
L1.1
Lecture 1
Dr. Sifat Momen
What is an Algorithm?
▪An algorithm is a finite set of precise
instructions for performing a
computation or for solving a problem.
▪ It must produce correct result
▪It must finish in some finite time
▪ You can represent an algorithm using pseudocode, flowchart,
or even actual code
L1.
2
Algorithm
L1.
7
Algorithm challenge 1
L1.
8
Algorithm Challenge 2
You are given all numbers between 1,2,…,n except one. Your task is to find the missing
number.
Input
The second line contains n−1 numbers. Each number is distinct and
between 1 and n (inclusive).
Output
Example
Input:
5
2315
L1.
Output: 9
4
Algorithm Challenge 3
Given a sorted array and an integer value (say sum), design an algorithm that will determine if
there exists a pair of integer whose sum is equal to sum.
L1.
10
What is this course about?
L1.
12
Why designing and
analysis of algorithm is
important?
Example:
Imagine two friends, Alice and Bob are given the
task of writing an algorithm that can sort 10 million
numbers
Alice writes an algorithm that takes 2N2
instructions and implements using computer that
executes 10 billion instructions per second.
Bob writes an algorithm that takes 50NlgN
instructions and implements using computer that
executes only 10 million instructions per second. L1.
L1.
14
The Problem of Sorting
Input: sequence 〈 a1, a2, …, an 〉 of
numbers.
Output: permutation 〈 a'1, a'2, …, a'n 〉
such
that a'1 ≤ a'2 ≤ … ≤ a'n .
Example
: Input: 8 2 4 9 3
6
Output: 2 3 4 6 8
9 L1.
15
Sorting Algorithms
L1.
17
Insertion sort
L1.
18
Example of insertion sort
8 2 4 9 3 6
L1.
19
Example of insertion sort
8 2 4 9 3 6
L1.
20
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
L1.
21
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
L1.
22
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
L1.
23
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
L1.
24
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
L1.
25
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
L1.
26
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
L1.
27
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
L1.
28
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 don
e L1.
29
C++ Implementation of
Insertion Sort
int main(){
int arr[] = {10, 6, 3, 2, 1, 8};
int l = sizeof(arr)/sizeof(*arr);
print(arr, l);
insertionSort(arr, l);
print(arr, l);
}
L1.
30
C++ Implementation of
Insertion Sort
void insertionSort(int A[], int length){
int key, i;
for(int j = 1; j < length; j++){
key = A[j];
i = j - 1;
while(i > -1 && A[i] > key){
A[i+1] = A[i];
i = i - 1;
}
A[i+1] = key;
}
L1.
} 31
Print Function
L1.
32
Output
L1.
33
Running time
37
Random Access Machine
model
• Single Processor
• Instructions are executed one after another, with no
concurrent operations
• The following instructions take a constant amount of
time
• Arithmetic: add, subtract, multiply, divide, remainder,
floor, ceiling etc…
• Data movement: load, store, copy
• Control: conditional/unconditional branch, subroutine call,
return
• Data type: Integer and Float
38
Insertion Sort: Running
▪ Time
Here tj = no. of times the condition of while loop is tested for the current value of j.
In the worst case (when input is reverse-sorted), in each iteration of the for loop, all the j-1
elements need to be right shifted and the key will be inserted in the front of them, i.e., tj=j.
Using this in the above equation, we get: T(n) = An 2+Bn+C, where A, B, C are constants. 39
What is T(n) in the best case (when the input numbers are already sorted)?
Insertion Sort: Running
Time (Best case)
▪ The best case is when the input is already in the sorted
manner.
▪ Thus tj = 1
L1.
41
Insertion Sort: Running
Time (Worst case)
▪
L1.
42
Asymptotic Analysis
fA(n)=30n+8
Running time→
fB(n)=n2+1
Increasing n
→
Growth of Functions
Complexity Graphs
log(n)
Complexity Graphs
n log(n)
log(n)
Complexity Graphs
n10 n3
n2
n log(n)
Complexity Graphs (log
scale)
3n
nn
n20
2n
n10
1.1n
Asymptotic Notations
Examples:
T(n) = 3n2+10nlgn+8 is O(n2), O(n2lgn), O(n3), O(n4), …
T’(n) = 52n2+3n2lgn+8 is O(n2lgn), O(n3), O(n4), …
Asymptotic Notations
▪ Ω - notation
Θ-notation
Θ(g(n)) is the set of functions with the same
order of growth as g(n)
* f(n) is both O(g(n)) & Ω(g(n)) ↔ f(n) is Θ(g(n))
Examples:
T(n) = 3n2+10nlgn+8 is Θ(n2)
T’(n) = 52n2+3n2lgn+8 is Θ(n2lgn)
Big-O Visualization
Some Examples