Numerical Computation_Lec_2_Part 1.pdf
Numerical Computation_Lec_2_Part 1.pdf
Computation
Lecture 2 (Part1)
3/16
Comparing between two Algorithms
Example:
Input: 8 2 4 9 3 6
Output:
2 3 4 6 8 9
- Input sequence is called an instance of the sorting problem. 5/16
Sorting Problem (cont.)
Efficiency
o Different algorithms devised to solve the same problem often
differ dramatically in their efficiency.
o These differences can be much more significant than differences
due to hardware and software
o In this chapter, we will see two algorithms for sorting problem
6/16
Insertion Sort
Solves the sorting problem
An efficient algorithm for sorting a small
number of elements
Works the way many people sort a hand of playing
cards
Start with an empty left hand and the cards face
down on the table
Then remove one card at a time from the table and
insert it into the correct position in the left hand
To find the correct position for a card, we compare
it with each of the cards already in the hand, from right to left
7/16
Insertion Sort (cont.)
8/16
Example of Insertion Sort
9/16
Analysis of Algorithms
Analysis of Algorithms is the determination of the amount of time,
storage and/or other resources necessary to execute them.
Analyzing algorithms is called Asymptotic Analysis
We shall assume a generic one processor, random-access machine
(RAM) model of computation as our implementation technology and
understand that our algorithms will be implemented as computer
programs.
In the RAM model, instructions are executed one after another
The running time depends on the input: an already sorted sequence is
easier to sort and short sequences are easier to sort than long ones.
10/16
Analysis of Algorithms (cont.)
We can have three cases to analyze an algorithm
2 3 5 4 1 7 6
11/16
Analysis of Algorithms (cont.)
2 3 5 4 1 7 6
12/16
Analysis of Algorithms (cont.)
13/16
Analysis of Algorithms (cont.)
15/16
Analysis of Algorithms (cont.)
Best Case
Worst Case
Average Case
16/16
Lec.3: Design and Analysis
Divide & Conquer
Thank
you!