SlideShare a Scribd company logo
Data Structures and
Algorithms
Prof. Adriano Patrick Cunha
Prof. Adriano Patrick Cunha
Program
Introduction Algorithms.
Designing and Analyzing Algorithms.
Recursive technique.
Lists.
Trees.
Priority Lists.
Prof. Adriano Patrick Cunha
Data Structures and Algorithms
“I will, in fact, claim that the difference between
a bad programmer and a good one is whether
he considers his code or his data structures
more important. Bad programmers worry about
the code. Good programmers worry about data
structures and their relationships.”
Linus Torvalds
Prof. Adriano Patrick Cunha
Data Structures and Algorithms
Problems solve by algorithms and data structures
The Human Genome Project
The Internet enables people all around the world to quickly access and retrieve large amounts of information.
Electronic commerce enables goods and services to be negotiated and exchanged electronically, and it depends on the privacy of
personal information such as credit card numbers, passwords, and bank statements.
Manufacturing and other commercial enterprises often need to allocate scarce resources in the most beneficial way.
We are given a road map on which the distance between each pair of adjacent intersections is marked, and we wish to determine the
shortest route from one intersection to another.
We are given two ordered sequences of symbols, X = (x1 ; x2 ;... ; xm) and Y = (y1 ; y2 ;... ; yn), and we wish to find a longest common
subsequence of X and Y
We are given a mechanical design in terms of a library of parts, where each part may include instances of other parts, and we need to list
the parts in order so that each part appears before any part that uses it.
We are given n points in the plane, and we wish to find the convex hull of these points. The convex hull is the smallest convex polygon
containing the points.
Prof. Adriano Patrick Cunha
Algorithms
Informally, an algorithm is any well-defined computational procedure that takes
some value, or set of values, as input and produces some value, or set of values,
as output. An algorithm is thus a sequence of computational steps that
transform the input into the output.
We can also view an algorithm as a tool for solving a well-specified
computational problem. The statement of the problem specifies in general terms
the desired input/output relationship. The algorithm describes a specific
computational procedure for achieving that input/output relationship.
Prof. Adriano Patrick Cunha
Problem:
Sorting
Input: sequence <a1, a2, ..., an) of numbers
Output: permutation <a'1, a'2, ..., a'n>
$(such that) -> $ a'1 <= a'2 <= ... <= a'n
E.g.
Input: 〈31, 41, 59, 26, 41, 58〉
Output: 〈26, 31, 41, 41, 58, 59〉
Prof. Adriano Patrick Cunha
Solve:
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
It works the way many people sort the cards by playing
cards
1. Letters initially on the table
2. One card at a time
3. Place it in the left hand, in the correct position
a. Find the right position from right to left
E.g. 〈5, 2, 4, 6, 1, 3〉
Prof. Adriano Patrick Cunha
Solve:
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
Is correct ?
Prof. Adriano Patrick Cunha
Solve:
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
Is correct ?
Is the best ?
Prof. Adriano Patrick Cunha
What is Correct?
An algorithm is said to be correct if, for every input instance, it halts with the
correct output. We say that a correct algorithm solves the given computational
problem.
An incorrect algorithm might not halt at all on some input instances, or it might
halt with an incorrect answer.
Contrary to what you might expect, incorrect algorithms can sometimes be
useful, if we can control their error rate.
Prof. Adriano Patrick Cunha
What is Correct?
Loop invariant
Property or statement that holds true for each loop iteration
Helps understand why an algorithm is correct
Prof. Adriano Patrick Cunha
Loop Invariant
Three details must be shown:
Initialization: the invariant is true before the first loop iteration
Maintenance: if the invariant is true before an iteration of the loop, it will
remain true before the next loop iteration
Termination: when the loop ends, the invariant gives us a useful property
that helps to show that the algorithm is correct
Prof. Adriano Patrick Cunha
What is being the best?
Insertion Sort
Number of statements executed c1
n2
Computer A: 1 billion (109
) instructions per second.
Great programmer: 2n2
instructions.
Intercalation Sort(Merge-Sort)
Number of statements executed c2
nlgn
Computer B: 10 millions (107
) instructions per second.
Regular programmer: 50nlgn instructions.
Time to sort 1 million (106
) elements of a set?
Prof. Adriano Patrick Cunha
Problem of the traveling salesman
A traveling salesman has to visit a certain number of cities and each move between two cities involves
a certain cost. What will be the most economic return, visiting each of the cities only once and
returning the one from where you left? The optimal solution for this type of problem is to find a
Hamilton circuit of minimum length.
Hamilton (or Hamiltonian) Circuit It is a path that begins and ends at the same vertex running through
all the vertices once (except the last which is also the first).
Prof. Adriano Patrick Cunha
Problem of the traveling salesman
4 15
10
9
16
20
8
14
127
A
B
CD
E
A -> E -> C -> D -> B -> A <56km>
B -> C -> E -> A -> D -> B <49km>
C -> E -> A -> D -> B -> C <49km>
D -> A -> E -> C -> B -> D <49km>
E -> A -> D -> C -> B -> E <44km>
N Alternativas (~n!) Tempo
5 120 0,00012 s
10 362880 3,62880 s
12 479001600 8 min
15 1307674368000 15 days
20 2432902008176640000 77.147 years
50 3,04 E+0064 ∞
100 9,33 E+0157 ∞
Prof. Adriano Patrick Cunha
What is being the best?
Running Time
- Depends on input (e.g. already sorted)
- Depends on input size (6 elements vs 6x10^9 elements)
- parametrize in input size
- Want upper bounds
- guarantee to user
Prof. Adriano Patrick Cunha
Analysis of Algorithms
Theoretical study of computer-program performance and resources usage.
What's more important than performance?
Why study algorithms and performance?
Determines something is feasible or infeasible
Performance enables the usability, security
Speed is fun!!
Prof. Adriano Patrick Cunha
Kinds of Analysis
- Woist-case(usually)
- T(n) = max time on any input of size n
- Average-case (sometimes)
- T(n) = expected time over all inputs of size n
- Need assumption of statistical distribution
- Best-case (bogus)
- Cheat
Prof. Adriano Patrick Cunha
Asympototic analysis
Ignore machine dependent constants
Look at GROWTH T(n) as n-> infinity
O Notation
- Drop low order terms
- Ignore leading constants
- E.g. 3n3
+ 90n2
- 5n + 6046 => O(n3
)
- As n -> infinity, O(n2
) algorithm always beats a O(n3
) algorithm.
Prof. Adriano Patrick Cunha
Insertion Sort - Asympototic analysis
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
//Insert A[ j ] into the sorted sequence A[ 1 .. j -1]
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
cost times
c1
c2
0
c3
c4
c5
c6
c7
n
n - 1
0
n - 1
∑n
j=2
Tj
∑n
j=2
(Tj
-1)
∑n
j=2
(Tj
-1)
n - 1
T(n) = c1n + c2(n-1) + c3(n-1) + c4∑n
j=2
Tj
+ c5∑n
j=2
(Tj
-1) + c6∑n
j=2
(Tj
-1) + c7(n-1)
Prof. Adriano Patrick Cunha
Insertion Sort - Asympototic analysis
Woist-case (Sequence in reverse order)
tj
= j, para j = 2, 3, ..., n
∑n
j=2
Tj
= ∑n
j=2
j = (∑n
k=1
k) - 1 = n(n + 1)/2 - 1
∑n
j=2
(Tj
-1) = ∑n
j=2
( j-1) = ∑n-1
k=1
k = n(n - 1)/2
T(n) = c1n + c2(n-1) + c3(n-1) + c4[n(n + 1)/2 - 1] + c5[n(n - 1)/2] + c6[n(n - 1)/2] + c7(n-1)
Logo, O(n2
)
Prof. Adriano Patrick Cunha
Recursive Technique
Continua ...

More Related Content

PDF
Analysis and Design of Algorithms notes
PPTX
Daa unit 1
PPTX
Notion of an algorithm
PPTX
Randomized Algorithm- Advanced Algorithm
PDF
Reoptimization techniques for solving hard problems
PPSX
Ds03 algorithms jyoti lakhani
PDF
Quantum Business in Japanese Market
Analysis and Design of Algorithms notes
Daa unit 1
Notion of an algorithm
Randomized Algorithm- Advanced Algorithm
Reoptimization techniques for solving hard problems
Ds03 algorithms jyoti lakhani
Quantum Business in Japanese Market

What's hot (20)

PPT
test pre
PDF
ADA complete notes
PPTX
Dynamic Programming - Part 1
PDF
Design and analysis of computer algorithms
PDF
Design & Analysis Of Algorithm
PPTX
Introduction to dynamic programming
PPT
Greedy Algoritham
PPTX
Dynamic Programming - Part II
PDF
Daa notes 1
PPT
Greedymethod
PPT
5.1 greedyyy 02
DOC
algorithm Unit 2
PPT
Divide and conquer algorithm
PDF
Lecture 2 role of algorithms in computing
PPTX
Algorithms : Introduction and Analysis
PPTX
Dynamic Programming
PDF
12 Greeddy Method
PPT
Greedy algorithm
PPTX
Daa:Dynamic Programing
PDF
Design & Analysis of Algorithms Lecture Notes
test pre
ADA complete notes
Dynamic Programming - Part 1
Design and analysis of computer algorithms
Design & Analysis Of Algorithm
Introduction to dynamic programming
Greedy Algoritham
Dynamic Programming - Part II
Daa notes 1
Greedymethod
5.1 greedyyy 02
algorithm Unit 2
Divide and conquer algorithm
Lecture 2 role of algorithms in computing
Algorithms : Introduction and Analysis
Dynamic Programming
12 Greeddy Method
Greedy algorithm
Daa:Dynamic Programing
Design & Analysis of Algorithms Lecture Notes
Ad

Viewers also liked (20)

PDF
Data Structures for Robotic Learning
PPTX
Algorithm & data structures lec1
PDF
Business Services - November 2015
PDF
Brendan Murray Resume[1]
PPTX
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
PPT
Mari Asun Landa
PPTX
Wonder p owerpoint1
PDF
Accesibilidad de un material
PPTX
Kuca Moraes - O Blog Primordial - 8020mkt
PPT
Mari Asun Landa
PPT
Mari asun landa
PPS
L_m-frdn
PDF
North Shore and Hibiscus Coast Things to Do and Places To Go - October 2015
PDF
Organizing for our Collective Success Presented by the National Young Farmers...
PPTX
StartUpSaturday_Deque
PPTX
Relatório de Twitter - @deppaulocorrea (11/2009)
PPT
PPTX
Lectura debate market share y posicionamiento
PPTX
Powerpoint tema 1 (1)
PPTX
Storyboard
Data Structures for Robotic Learning
Algorithm & data structures lec1
Business Services - November 2015
Brendan Murray Resume[1]
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
Mari Asun Landa
Wonder p owerpoint1
Accesibilidad de un material
Kuca Moraes - O Blog Primordial - 8020mkt
Mari Asun Landa
Mari asun landa
L_m-frdn
North Shore and Hibiscus Coast Things to Do and Places To Go - October 2015
Organizing for our Collective Success Presented by the National Young Farmers...
StartUpSaturday_Deque
Relatório de Twitter - @deppaulocorrea (11/2009)
Lectura debate market share y posicionamiento
Powerpoint tema 1 (1)
Storyboard
Ad

Similar to Data structures and algorithms (20)

PDF
Data Structures (BE)
PPTX
L1_Start_of_Learning_of_Algorithms_Basics.pptx
PPTX
L1_DatabAlgorithm Basics with Design & Analysis.pptx
PDF
Algorithm Design and Analysis
PDF
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
PDF
01-Slides.pdf
PPT
Design and analysis of algorithm in Computer Science
PPTX
Unit 1.pptx
PPT
Algorithm in Computer, Sorting and Notations
PPT
Algorithm
PPT
Algorithm
PPTX
Segment_1_New computer algorithm for cse.pptx
PDF
Algorithms
PPT
Systems in the small - Introduction to Algorithms
PDF
Data Structure - Lecture 1 - Introduction.pdf
PPT
Lecture01 algorithm analysis
PPTX
Algo_lecture1-3.pptx
PDF
Introduction to Algorithms Complexity Analysis
PPTX
Binary to hexadecimal algorithmic old.pptx
Data Structures (BE)
L1_Start_of_Learning_of_Algorithms_Basics.pptx
L1_DatabAlgorithm Basics with Design & Analysis.pptx
Algorithm Design and Analysis
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
01-Slides.pdf
Design and analysis of algorithm in Computer Science
Unit 1.pptx
Algorithm in Computer, Sorting and Notations
Algorithm
Algorithm
Segment_1_New computer algorithm for cse.pptx
Algorithms
Systems in the small - Introduction to Algorithms
Data Structure - Lecture 1 - Introduction.pdf
Lecture01 algorithm analysis
Algo_lecture1-3.pptx
Introduction to Algorithms Complexity Analysis
Binary to hexadecimal algorithmic old.pptx

More from Adriano Patrick Cunha (8)

PDF
Desenvolvimento web e mobile ifce
PDF
Recuperacao Falhas em Sistemas Workflow
PDF
ETL DW-RealTime
PDF
Congresso TI - Qualidade de Código.
PDF
Concurrencyproblem
PDF
Article K-OPT in JSSP
PDF
PDF
Natuur mobile
Desenvolvimento web e mobile ifce
Recuperacao Falhas em Sistemas Workflow
ETL DW-RealTime
Congresso TI - Qualidade de Código.
Concurrencyproblem
Article K-OPT in JSSP
Natuur mobile

Recently uploaded (20)

PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administraation Chapter 3
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Transform Your Business with a Software ERP System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Reimagine Home Health with the Power of Agentic AI​
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
history of c programming in notes for students .pptx
System and Network Administraation Chapter 3
Operating system designcfffgfgggggggvggggggggg
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
CHAPTER 2 - PM Management and IT Context
Wondershare Filmora 15 Crack With Activation Key [2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Navsoft: AI-Powered Business Solutions & Custom Software Development
Transform Your Business with a Software ERP System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Migrate SBCGlobal Email to Yahoo Easily
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Data structures and algorithms

  • 1. Data Structures and Algorithms Prof. Adriano Patrick Cunha
  • 2. Prof. Adriano Patrick Cunha Program Introduction Algorithms. Designing and Analyzing Algorithms. Recursive technique. Lists. Trees. Priority Lists.
  • 3. Prof. Adriano Patrick Cunha Data Structures and Algorithms “I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” Linus Torvalds
  • 4. Prof. Adriano Patrick Cunha Data Structures and Algorithms Problems solve by algorithms and data structures The Human Genome Project The Internet enables people all around the world to quickly access and retrieve large amounts of information. Electronic commerce enables goods and services to be negotiated and exchanged electronically, and it depends on the privacy of personal information such as credit card numbers, passwords, and bank statements. Manufacturing and other commercial enterprises often need to allocate scarce resources in the most beneficial way. We are given a road map on which the distance between each pair of adjacent intersections is marked, and we wish to determine the shortest route from one intersection to another. We are given two ordered sequences of symbols, X = (x1 ; x2 ;... ; xm) and Y = (y1 ; y2 ;... ; yn), and we wish to find a longest common subsequence of X and Y We are given a mechanical design in terms of a library of parts, where each part may include instances of other parts, and we need to list the parts in order so that each part appears before any part that uses it. We are given n points in the plane, and we wish to find the convex hull of these points. The convex hull is the smallest convex polygon containing the points.
  • 5. Prof. Adriano Patrick Cunha Algorithms Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output. We can also view an algorithm as a tool for solving a well-specified computational problem. The statement of the problem specifies in general terms the desired input/output relationship. The algorithm describes a specific computational procedure for achieving that input/output relationship.
  • 6. Prof. Adriano Patrick Cunha Problem: Sorting Input: sequence <a1, a2, ..., an) of numbers Output: permutation <a'1, a'2, ..., a'n> $(such that) -> $ a'1 <= a'2 <= ... <= a'n E.g. Input: 〈31, 41, 59, 26, 41, 58〉 Output: 〈26, 31, 41, 41, 58, 59〉
  • 7. Prof. Adriano Patrick Cunha Solve: Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; It works the way many people sort the cards by playing cards 1. Letters initially on the table 2. One card at a time 3. Place it in the left hand, in the correct position a. Find the right position from right to left E.g. 〈5, 2, 4, 6, 1, 3〉
  • 8. Prof. Adriano Patrick Cunha Solve: Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; Is correct ?
  • 9. Prof. Adriano Patrick Cunha Solve: Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; Is correct ? Is the best ?
  • 10. Prof. Adriano Patrick Cunha What is Correct? An algorithm is said to be correct if, for every input instance, it halts with the correct output. We say that a correct algorithm solves the given computational problem. An incorrect algorithm might not halt at all on some input instances, or it might halt with an incorrect answer. Contrary to what you might expect, incorrect algorithms can sometimes be useful, if we can control their error rate.
  • 11. Prof. Adriano Patrick Cunha What is Correct? Loop invariant Property or statement that holds true for each loop iteration Helps understand why an algorithm is correct
  • 12. Prof. Adriano Patrick Cunha Loop Invariant Three details must be shown: Initialization: the invariant is true before the first loop iteration Maintenance: if the invariant is true before an iteration of the loop, it will remain true before the next loop iteration Termination: when the loop ends, the invariant gives us a useful property that helps to show that the algorithm is correct
  • 13. Prof. Adriano Patrick Cunha What is being the best? Insertion Sort Number of statements executed c1 n2 Computer A: 1 billion (109 ) instructions per second. Great programmer: 2n2 instructions. Intercalation Sort(Merge-Sort) Number of statements executed c2 nlgn Computer B: 10 millions (107 ) instructions per second. Regular programmer: 50nlgn instructions. Time to sort 1 million (106 ) elements of a set?
  • 14. Prof. Adriano Patrick Cunha Problem of the traveling salesman A traveling salesman has to visit a certain number of cities and each move between two cities involves a certain cost. What will be the most economic return, visiting each of the cities only once and returning the one from where you left? The optimal solution for this type of problem is to find a Hamilton circuit of minimum length. Hamilton (or Hamiltonian) Circuit It is a path that begins and ends at the same vertex running through all the vertices once (except the last which is also the first).
  • 15. Prof. Adriano Patrick Cunha Problem of the traveling salesman 4 15 10 9 16 20 8 14 127 A B CD E A -> E -> C -> D -> B -> A <56km> B -> C -> E -> A -> D -> B <49km> C -> E -> A -> D -> B -> C <49km> D -> A -> E -> C -> B -> D <49km> E -> A -> D -> C -> B -> E <44km> N Alternativas (~n!) Tempo 5 120 0,00012 s 10 362880 3,62880 s 12 479001600 8 min 15 1307674368000 15 days 20 2432902008176640000 77.147 years 50 3,04 E+0064 ∞ 100 9,33 E+0157 ∞
  • 16. Prof. Adriano Patrick Cunha What is being the best? Running Time - Depends on input (e.g. already sorted) - Depends on input size (6 elements vs 6x10^9 elements) - parametrize in input size - Want upper bounds - guarantee to user
  • 17. Prof. Adriano Patrick Cunha Analysis of Algorithms Theoretical study of computer-program performance and resources usage. What's more important than performance? Why study algorithms and performance? Determines something is feasible or infeasible Performance enables the usability, security Speed is fun!!
  • 18. Prof. Adriano Patrick Cunha Kinds of Analysis - Woist-case(usually) - T(n) = max time on any input of size n - Average-case (sometimes) - T(n) = expected time over all inputs of size n - Need assumption of statistical distribution - Best-case (bogus) - Cheat
  • 19. Prof. Adriano Patrick Cunha Asympototic analysis Ignore machine dependent constants Look at GROWTH T(n) as n-> infinity O Notation - Drop low order terms - Ignore leading constants - E.g. 3n3 + 90n2 - 5n + 6046 => O(n3 ) - As n -> infinity, O(n2 ) algorithm always beats a O(n3 ) algorithm.
  • 20. Prof. Adriano Patrick Cunha Insertion Sort - Asympototic analysis Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; //Insert A[ j ] into the sorted sequence A[ 1 .. j -1] i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; cost times c1 c2 0 c3 c4 c5 c6 c7 n n - 1 0 n - 1 ∑n j=2 Tj ∑n j=2 (Tj -1) ∑n j=2 (Tj -1) n - 1 T(n) = c1n + c2(n-1) + c3(n-1) + c4∑n j=2 Tj + c5∑n j=2 (Tj -1) + c6∑n j=2 (Tj -1) + c7(n-1)
  • 21. Prof. Adriano Patrick Cunha Insertion Sort - Asympototic analysis Woist-case (Sequence in reverse order) tj = j, para j = 2, 3, ..., n ∑n j=2 Tj = ∑n j=2 j = (∑n k=1 k) - 1 = n(n + 1)/2 - 1 ∑n j=2 (Tj -1) = ∑n j=2 ( j-1) = ∑n-1 k=1 k = n(n - 1)/2 T(n) = c1n + c2(n-1) + c3(n-1) + c4[n(n + 1)/2 - 1] + c5[n(n - 1)/2] + c6[n(n - 1)/2] + c7(n-1) Logo, O(n2 )
  • 22. Prof. Adriano Patrick Cunha Recursive Technique Continua ...