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

Lecture Three-Algorithm and Problem Solving

The lecture focuses on analyzing algorithms to predict resource requirements, particularly running time, and emphasizes the importance of performance prediction, algorithm comparison, code optimization, and handling large data. It discusses how running time varies with input size and the significance of measuring it through basic operations while considering best, worst, and average cases. The analysis also highlights the concept of order of growth in determining algorithm efficiency, particularly in the context of insertion sort.

Uploaded by

f.alkhelaifi9009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture Three-Algorithm and Problem Solving

The lecture focuses on analyzing algorithms to predict resource requirements, particularly running time, and emphasizes the importance of performance prediction, algorithm comparison, code optimization, and handling large data. It discusses how running time varies with input size and the significance of measuring it through basic operations while considering best, worst, and average cases. The analysis also highlights the concept of order of growth in determining algorithm efficiency, particularly in the context of insertion sort.

Uploaded by

f.alkhelaifi9009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Algorithms and Problem Solving

Course Number: ITCC103

Lecture 3: Analyzing Algorithms

Rawand Al-Foqah’a

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 1


Analyzing Algorithms
➢We want to predict the resources that the algorithm requires. Usually, running time.
➢ Why analyze? Why not just code up the algorithm, run the code, and time it?
Instead of just coding and running an algorithm to see how long it takes, we analyze it because:
1.Predict Performance – Analysis helps us understand how fast or slow an algorithm will be, even before
we run it.
2.Compare Algorithms – We can compare different approaches and choose the best one.
3.Optimize Code – It helps us improve efficiency by identifying and fixing performance issues.
4.Handle Large Data – Some algorithms work well with small inputs but become too slow with large
data. Analysis helps us prepare for that.

-Analysis helps us choose the best algorithm before coding, ensuring efficiency and scalability.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 2


How do we analyze an algorithm’s running
time?
The time taken by an algorithm depends on the input.
➢ Sorting 1000 numbers takes longer than sorting 3 numbers.
➢ A given sorting algorithm may even take differing amounts of time on two inputs of the same
size.
➢For example, we’ll see that insertion sort takes less time to sort n elements when they are already
sorted than when they are in reverse sorted order.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 3


Running Time of an Algorithm
➢What is Running Time?
❑Is the time an algorithm takes to complete based on input size (n).

❑We analyze it to understand efficiency and scalability.

➢How Do We Measure It?


❑Count Basic Operations (e.g., loops, comparisons, assignments).

❑Consider Best, Worst, and Average cases to cover all scenarios.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 4


Analysis of Insertion Sort

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 5


Analysis of Insertion Sort

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 6


Analysis of Insertion Sort

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 7


Best Case

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 8


Worst Case

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 9


Worst Case Cont.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 10


Worst-case and Average-case Analysis
➢We usually concentrate on finding the worst-case running time: the longest
running time for any input of size n.
➢Reasons:
❖The worst-case running time gives a guaranteed upper bound on the running time for any input.
❖ For some algorithms, the worst case occurs often. For example, when searching, the worst case
often occurs when the item being searched for is not present, and searches for absent items may
be frequent.
➢Why not analyze the average case? Because it’s often about as bad as the
worst case.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 11


Order Of Growth
➢Another abstraction to ease analysis and ➢For insertion sort, we concluded that the
focus on the important features. worst-case running time is:
➢Look only at the leading term of the formula
𝑎𝑛2 + 𝑏𝑛 + 𝑐 .
for running time: ➢Drop lower-order terms → 𝑎𝑛2 .
❑ Drop lower-order terms. ➢Ignore constant coefficient →𝑛2 .
❑Ignore the constant coefficient in the ➢But we cannot say that the worst-case
leading term. running time T(n) equals 𝑛2 .It grows like
𝑛2 .But it doesn’t equal 𝑛2 .
➢We usually consider one algorithm to be
more efficient than another if its worst- case
running time has a smaller order of growth.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 12


End Of the Lecture

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 13


References
❑Thomas H. CORMEN, et al. Introduction to Algorithms. The MIT Press, Cambridge,
Massachusetts London, England, ISBN 9780262046305, 2022.

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 14

You might also like