SlideShare a Scribd company logo
Design & Analysis
of
Algorithms
DECREASE&CONQUER
TRANSFORM & CONQUER
TRANSFORM & CONQUER
Transforming
Stage
Conquering
Stage
Problem instance is
simplified to be more
amenable
Transformed
problem is solved
Types of problem modification
INSTANCE
SIMPLIFICATION
Problem instance is
transformed to be
more simple to solve
Presorting
Types of problem modification
Representation
Change
Data structure is
transformed so that
it is more efficient to
solve.
Horner’s Rule
Types of problem modification
Problem
Reduction
Problem is
transformed to
different problem
whose algorithm is
already available
Finding LCM
Presorting to find Mode
Algorithm PresortMode(A[0...n-1])
// assumes that A is sorted
i ← 0
modefrequency ← 0
while i < n do
runlength ← 1
runvalue ← A[i]
while i+runlength < n and A[i+runlength] = runvalue do
runlength++
if runlength > modefequency then
modefrequency ← runlength
modevalue ← runvalue
i += runlength
return modevalue
Algorithm PresortMode(A[0...n-1])
// assumes that A is sorted
i ← 0
modefrequency ← 0
while i < n do
runlength ← 1
runvalue ← A[i]
while i+runlength < n and A[i+runlength] = runvalue do
runlength++
if runlength > modefequency then
modefrequency ← runlength
modevalue ← runvalue
i += runlength
return modevalue
Using GCD to find LCM
lcm(m,n) = (m*n)/ gcd(m,n)
Decrease & CONQUER
Problem of size
N
Conquering
Stage
Solve for N-1
• Problem of Size N
• Solve for N-1
• Repeat above and problem will be
reduced eventually to what we can
easily solve
Decrement types
• Decrease by constant
e.g. insertion sort
• Decrease by constant factor
e.g. the fake coin problem
• Decrease by variable
e.g. Euclid’s Algorithm
Insertion Sort
Algorithm InsertionSort(A[0...n-1])
for i ← to n-1 do // move the unsorted portion
v ← A[i] // value to sort
j ← i-1 // end of the sorted array
while j ≥ 0 and A[j] > v do // scan the sorted part of the array for
the insertion point
A[j+1] ← A[j] // shift the sort array to make room for the insertion
j--
A[j+1] ← v // insert
Algorithm InsertionSort(A[0...n-1])
for i ← to n-1 do
v ← A[i]
j ← i-1
while j ≥ 0 and A[j] > v do
A[j+1] ← A[j]
j--
A[j+1] ← v
Complexity:
>>O(n^2) in worst case
>>O(n) for presorted array
BACKTRACKING APPROACH
BACK
TRACK
START
Yes
No
SearchAgain
SOLUTION
More about backtracking
• Just like Brute Force
• It is used when a problem has many candidates as solution.
• Used very commonly by hacker
• Used in electrical engineering
• Artificial intelligence
• Solving puzzles & paths
• Accuracy is guaranteed
Sum of Subsets
Algorithm SumOfSubset(s,k,y){
X[k] = 1;
If(s+w[k] = m)
Write (x[1:n]);
Else if((s+w[k] + w[k+1]) <= m)
SumOfSubset(s+w[k], k+1, y-w[k]);
If ((s+ y-w[k]>=m) &&(s =w[k+1] <=m)) {
X[k] =0;
SumOfSubset(s,k+1,y-w[k]);
}
}
Design & Analysis of Algorithms Techniques
Design & Analysis of Algorithms Techniques
Design & Analysis of Algorithms Techniques

More Related Content

What's hot

Selection sort
Selection sortSelection sort
Selection sort
Abdelrahman Saleh
 
LYAPUNOV STABILITY PROBLEM SOLUTION
LYAPUNOV STABILITY PROBLEM SOLUTIONLYAPUNOV STABILITY PROBLEM SOLUTION
LYAPUNOV STABILITY PROBLEM SOLUTION
rohit kumar
 
Lyapunov stability
Lyapunov stability Lyapunov stability
Lyapunov stability
Srinath Thamban
 
Queues
QueuesQueues
Queues
Sadaf Ismail
 
Derivatives
DerivativesDerivatives
Derivativessmj123
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
Edhole.com
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
Smit Parikh
 
Nfa
NfaNfa
n-squared_sorts
n-squared_sortsn-squared_sorts
n-squared_sorts
Mohamed Elsayed
 
Discreate time system and z transform
Discreate time system and z transformDiscreate time system and z transform
Discreate time system and z transform
VIKAS KUMAR MANJHI
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Afaq Mansoor Khan
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
maamir farooq
 
The Ring programming language version 1.3 book - Part 15 of 88
The Ring programming language version 1.3 book - Part 15 of 88The Ring programming language version 1.3 book - Part 15 of 88
The Ring programming language version 1.3 book - Part 15 of 88
Mahmoud Samir Fayed
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
zzzubair
 
Applications of Z transform
Applications of Z transformApplications of Z transform
Applications of Z transform
AakankshaR
 

What's hot (16)

Selection sort
Selection sortSelection sort
Selection sort
 
LYAPUNOV STABILITY PROBLEM SOLUTION
LYAPUNOV STABILITY PROBLEM SOLUTIONLYAPUNOV STABILITY PROBLEM SOLUTION
LYAPUNOV STABILITY PROBLEM SOLUTION
 
Lyapunov stability
Lyapunov stability Lyapunov stability
Lyapunov stability
 
Queues
QueuesQueues
Queues
 
Derivatives
DerivativesDerivatives
Derivatives
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
Nfa
NfaNfa
Nfa
 
n-squared_sorts
n-squared_sortsn-squared_sorts
n-squared_sorts
 
Discreate time system and z transform
Discreate time system and z transformDiscreate time system and z transform
Discreate time system and z transform
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
 
The Ring programming language version 1.3 book - Part 15 of 88
The Ring programming language version 1.3 book - Part 15 of 88The Ring programming language version 1.3 book - Part 15 of 88
The Ring programming language version 1.3 book - Part 15 of 88
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
Applications of Z transform
Applications of Z transformApplications of Z transform
Applications of Z transform
 

Similar to Design & Analysis of Algorithms Techniques

09d transform & conquer spring2015
09d transform & conquer spring201509d transform & conquer spring2015
09d transform & conquer spring2015
Hira Gul
 
Lecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfLecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdf
VishalPusadkar1
 
Derivative Free Optimization
Derivative Free OptimizationDerivative Free Optimization
Derivative Free Optimization
Olivier Teytaud
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
sajinis3
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptx
MonirHossain707319
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
Puja Koch
 
Lecture 9-online
Lecture 9-onlineLecture 9-online
Lecture 9-online
lifebreath
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
DavidMaina47
 
Linear Regression.pptx
Linear Regression.pptxLinear Regression.pptx
Linear Regression.pptx
nathansel1
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
MuhammadSheraz836877
 
Analysis of Algorithms - 1
Analysis of Algorithms - 1Analysis of Algorithms - 1
Analysis of Algorithms - 1
AtakanAral
 
Z transfrm ppt
Z transfrm pptZ transfrm ppt
Z transfrm ppt
SWATI MISHRA
 
Coursera 2week
Coursera  2weekCoursera  2week
Coursera 2week
csl9496
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
ssuser3a8f33
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
Deepak John
 

Similar to Design & Analysis of Algorithms Techniques (20)

09d transform & conquer spring2015
09d transform & conquer spring201509d transform & conquer spring2015
09d transform & conquer spring2015
 
Lecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfLecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdf
 
Derivative Free Optimization
Derivative Free OptimizationDerivative Free Optimization
Derivative Free Optimization
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptx
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
 
Lecture 9-online
Lecture 9-onlineLecture 9-online
Lecture 9-online
 
Cs1311lecture23wdl
Cs1311lecture23wdlCs1311lecture23wdl
Cs1311lecture23wdl
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
 
Linear Regression.pptx
Linear Regression.pptxLinear Regression.pptx
Linear Regression.pptx
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Analysis of Algorithms - 1
Analysis of Algorithms - 1Analysis of Algorithms - 1
Analysis of Algorithms - 1
 
Z transfrm ppt
Z transfrm pptZ transfrm ppt
Z transfrm ppt
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Coursera 2week
Coursera  2weekCoursera  2week
Coursera 2week
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 

Recently uploaded

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 

Recently uploaded (20)

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 

Design & Analysis of Algorithms Techniques

  • 1.
  • 3. TRANSFORM & CONQUER Transforming Stage Conquering Stage Problem instance is simplified to be more amenable Transformed problem is solved
  • 4. Types of problem modification INSTANCE SIMPLIFICATION Problem instance is transformed to be more simple to solve Presorting
  • 5. Types of problem modification Representation Change Data structure is transformed so that it is more efficient to solve. Horner’s Rule
  • 6. Types of problem modification Problem Reduction Problem is transformed to different problem whose algorithm is already available Finding LCM
  • 7. Presorting to find Mode Algorithm PresortMode(A[0...n-1]) // assumes that A is sorted i ← 0 modefrequency ← 0 while i < n do runlength ← 1 runvalue ← A[i] while i+runlength < n and A[i+runlength] = runvalue do runlength++ if runlength > modefequency then modefrequency ← runlength modevalue ← runvalue i += runlength return modevalue
  • 8. Algorithm PresortMode(A[0...n-1]) // assumes that A is sorted i ← 0 modefrequency ← 0 while i < n do runlength ← 1 runvalue ← A[i] while i+runlength < n and A[i+runlength] = runvalue do runlength++ if runlength > modefequency then modefrequency ← runlength modevalue ← runvalue i += runlength return modevalue
  • 9. Using GCD to find LCM lcm(m,n) = (m*n)/ gcd(m,n)
  • 10. Decrease & CONQUER Problem of size N Conquering Stage Solve for N-1 • Problem of Size N • Solve for N-1 • Repeat above and problem will be reduced eventually to what we can easily solve
  • 11. Decrement types • Decrease by constant e.g. insertion sort • Decrease by constant factor e.g. the fake coin problem • Decrease by variable e.g. Euclid’s Algorithm
  • 12. Insertion Sort Algorithm InsertionSort(A[0...n-1]) for i ← to n-1 do // move the unsorted portion v ← A[i] // value to sort j ← i-1 // end of the sorted array while j ≥ 0 and A[j] > v do // scan the sorted part of the array for the insertion point A[j+1] ← A[j] // shift the sort array to make room for the insertion j-- A[j+1] ← v // insert
  • 13. Algorithm InsertionSort(A[0...n-1]) for i ← to n-1 do v ← A[i] j ← i-1 while j ≥ 0 and A[j] > v do A[j+1] ← A[j] j-- A[j+1] ← v Complexity: >>O(n^2) in worst case >>O(n) for presorted array
  • 15. More about backtracking • Just like Brute Force • It is used when a problem has many candidates as solution. • Used very commonly by hacker • Used in electrical engineering • Artificial intelligence • Solving puzzles & paths • Accuracy is guaranteed
  • 16. Sum of Subsets Algorithm SumOfSubset(s,k,y){ X[k] = 1; If(s+w[k] = m) Write (x[1:n]); Else if((s+w[k] + w[k+1]) <= m) SumOfSubset(s+w[k], k+1, y-w[k]); If ((s+ y-w[k]>=m) &&(s =w[k+1] <=m)) { X[k] =0; SumOfSubset(s,k+1,y-w[k]); } }