Algorithm Analysis
Submitted By:-Ms.Mehak Kapoor
PG DEPT COMPUTER SCIENCE &IT
1
What is an algorithm?
2
Some algorithms are harder than others
3
Algorithm 1: Maximum element
4
Algorithm 1: Maximum element
procedure max (a1, a2, …, an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai
{max is the largest element}
5
Algorithm 1: Maximum element
procedure max (a1, a2, …, an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai
6
max := a1
for i := 2 to n
if max < ai then max := ai
4 | 1 | 7 | 0 | 5 | 2 | 9 | 3 | 6 | 8 |
a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 |
|
max |
|
i |
2
3
4
5
6
7
8
9
10
4
7
9
Maximum element running time
7
Properties of algorithms
8
Searching algorithms
9
Algorithm 2: Linear search
procedure linear_search (x: integer; a1, a2, …, an: integers)
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
{location is the subscript of the term that equals x, or it is 0 if x is not found}
10
Algorithm 2: Linear search, take 1
11
procedure linear_search (x: integer; a1, a2, …, an: integers)
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
4 | 1 | 7 | 0 | 5 | 2 | 9 | 3 | 6 | 8 |
a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 |
|
i |
2
3
4
5
6
7
8
1
x
3
location
8
Algorithm 2: Linear search, take 2
12
procedure linear_search (x: integer; a1, a2, …, an: integers)
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
4 | 1 | 7 | 0 | 5 | 2 | 9 | 3 | 6 | 8 |
a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 |
|
i |
2
3
4
5
6
7
8
9
10
1
x
11
location
0
11
Linear search running time
13
Algorithm 3: Binary search
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
i := 1 { i is left endpoint of search interval }
j := n { j is right endpoint of search interval }
while i < j
begin
m := ⎣(i+j)/2⎦ { m is the point in the middle }
if x > am then i := m+1
else j := m
end
if x = ai then location := i
else location := 0
{location is the subscript of the term that equals x, or it is 0 if x is not found}
14
Algorithm 3: Binary search, take 1
15
2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 |
i := 1
j := n
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
while i < j
begin
m := ⎣(i+j)/2⎦
if x > am then i := m+1
else j := m
end
if x = ai then location := i
else location := 0
i := 1
j := n
while i < j
begin
m := ⎣(i+j)/2⎦
if x > am then i := m+1
else j := m
end
if x = ai then location := i
I
x
14
m
j
location
7
Algorithm 3: Binary search, take 2
16
2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 |
i
j
m
i := 1
j := n
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
while i < j
begin
m := ⎣(i+j)/2⎦
if x > am then i := m+1
else j := m
end
if x = ai then location := i
else location := 0
i := 1
j := n
while i < j
begin
m := ⎣(i+j)/2⎦
if x > am then i := m+1
else j := m
end
if x = ai then location := I
else location := 0
1
x
15
10
5
6
8
8
7
location
0
8
Algorithm 3: Binary search
17
Binary search running time
18
Sorting algorithms
19
Algorithm 4: Bubble sort
procedure bubble_sort (a1, a2, …, an)
for i := 1 to n-1
for j := 1 to n-i
if aj > aj+1
then interchange aj and aj+1
{ a1, …, an are in increasing order }
20
Algorithm 4: Bubble sort
21
a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 |
i
j
| | | | | | | | | |
for i := 1 to n-1
for j := 1 to n-i
if aj > aj+1
then interchange aj and aj+1
Bubble sort running time
for i := 1 to n-1
for j := 1 to n-i
if aj > aj+1
then interchange aj and aj+1
22
Algorithm 5: Insertion sort
procedure insertion_sort (a1, a2, …, an)
for j := 2 to n
begin
i := 1
while aj > ai
i := i +1
m := aj
for k := 0 to j-i-1
aj-k := aj-k-1
ai := m
end { a1, a2, …, an are sorted }
23
take successive elements in the list
find where that element should be in the sorted portion of the list
move all elements in the sorted portion of the list that are greater than the current element up by one
put the current element into it’s proper place in the sorted portion of the list
Insertion sort running time
for j := 2 to n begin
i := 1
while aj > ai
i := i +1
m := aj
for k := 0 to j-i-1
aj-k := aj-k-1
ai := m
end { a1, a2, …, an are sorted }
24
Comparison of running times
25
THANK YOU!
26