SlideShare a Scribd company logo
1 of 19
QUICK SORT
DIVIDE AND CONQUER
1
DIVIDE AND CONQUER
 An algorithm is only called divide and conquer if it
contains two or more recursive calls
 The divide and conquer strategy used to
 make a more efficient search algorithm
 can also be applied to sorting
2
DIVIDE AND CONQUER (CONT.)
 Divide : Divide the problem P into P1 , P2,……Pk sub
problems until P is smaller one
 Conquer : the sub-problems by solving them recursively
 Combine : the solutions of sub problems to create a
solution to the original problem
3
4
A problem size n
Sub problem 1 of
size n/2
Sub problem 2 of
size n/2
A solution to
sub problem 1
A solution to
sub problem 2
A solution to the original
problem
DIVIDE AND CONQUER (CONT.)
DIVIDE AND CONQUER - EXAMPLE
 Sorting: merge sort and quicksort
 Binary tree traversals
 Binary search
 Multiplication of large integers
 Matrix multiplication: Strassen’s algorithm
 Closest-pair and convex-hull algorithms
5
MERITS OF DIVIDE AND CONQUER
6
 Time spent on executing the problem using divide and
conquer is smaller than others.
 Provides efficient algorithm.
 Suited for parallel computation in which each sub problem
can be solved simultaneously by its own processor.
Quick sort
 The main idea is to partition the array into two regions:
small items are moved to the left side of the array
large items are moved to the right side
 After partitioning, repeat the sort on the left and right sides
each region is a sub-problem, a smaller version of the
original problem
7
Quick sort
8
 Main question: how do we decide which items are “small”
and which are “large”?
 A common technique: use the first item in the region as a
pivot
everything less than the pivot ends up in the left region
items greater than or equal to the pivot go in the right
region
Quick sort
1. Divide: partition A[p..r] into two sub arrays A[p..q-1] and
A[q+1..r] such that each element of A[p..q-1] is ≤ A[q],
and each element of A[q+1..r] is ≥ A[q]. Compute q as part
of this partitioning.
2. Conquer: sort the sub arrays A[p..q-1] and A[q+1..r] by
recursive calls to QUICKSORT.
3. Combine: the partitioning and recursive sorting leave us
with a sorted A[p..r]
9
Quick sort algorithm
QUICKSORT(A[p,…r])
 Input: A[p,….r]
 Output: the sub array A[1…r] stored in non decreasing order
If p<r
 q= PARTITION(A,p,r)
 QUICKSORT(A[p,q-1])
 QUICKSORT(A[q+1, r])
10
v
v
S1 S2
S
Quick sort algorithm (Cont.)
11
PARTITION(A[p…r])
Initialization
 P=A[p]
 i=p
 j=r+1
Quick sort algorithm (Cont.)
12
 Repeat
Repeat i=i+1 until A[i]≥P
Repeat j=j-1 until A[j]≤P
Swap(A[i],A[j])
 Until i ≥ j
 Swap(A[p],a[j]);
 Return j
Quick sort algorithm (Cont.)
13
Void swap( int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
COMPLEXITY OF QUICK SORT
14
 Space complexity
 Time complexity
 Choosing pivot element
15
Space complexity:
 Space for quick sort:
Each and every recursive call require stack space
for an array that is q.
 Space for partition:
Space for an array A=‘n’ locations
Space for control variable= 3 location (i,j,pivot)
COMPLEXITY OF QUICK SORT (Cont.)
16
Time complexity
 Best case : n log2n
 Worst case : n2
 Average case : 1.38 n log2n
COMPLEXITY OF QUICK SORT (Cont.)
17
Choosing pivot element
 Selection of proper pivot, improve the efficiency of
an algorithm.
 Median of three partition method uses pivot as the
median of left most, right most, and middle element
of the array.
COMPLEXITY OF QUICK SORT (Cont.)
BENEFITS OF QUICK SORT
18
 Sorting time is very less when comparing with all
other sorts.
 Idea of partitioning can be useful in many
applications.
Divide and conquer - Quick sort

More Related Content

What's hot

Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structureMeherul1234
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithmRezwan Siam
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmLearning Courses Online
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back trackingAbinaya B
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 

What's hot (20)

Quick Sort
Quick SortQuick Sort
Quick Sort
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Quick sort
Quick sortQuick sort
Quick sort
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 

Viewers also liked

Viewers also liked (7)

Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Safe laparoscopy
Safe laparoscopySafe laparoscopy
Safe laparoscopy
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 

Similar to Divide and conquer - Quick sort

module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptajiths82
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.pptAliAhmad38278
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxFazlullah28
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxsgrishma559
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 

Similar to Divide and conquer - Quick sort (20)

module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Merge sort
Merge sortMerge sort
Merge sort
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.ppt
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
ECE 565 FInal Project
ECE 565 FInal ProjectECE 565 FInal Project
ECE 565 FInal Project
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 

More from Madhu Bala

Internet of Things (IoT)
Internet of Things (IoT)Internet of Things (IoT)
Internet of Things (IoT)Madhu Bala
 
Operating system
Operating systemOperating system
Operating systemMadhu Bala
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)Madhu Bala
 
GPRS Technology
GPRS TechnologyGPRS Technology
GPRS TechnologyMadhu Bala
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - IntroductionMadhu Bala
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Smoothing Filters in Spatial Domain
Smoothing Filters in Spatial DomainSmoothing Filters in Spatial Domain
Smoothing Filters in Spatial DomainMadhu Bala
 

More from Madhu Bala (10)

Internet of Things (IoT)
Internet of Things (IoT)Internet of Things (IoT)
Internet of Things (IoT)
 
Digital logic
Digital logicDigital logic
Digital logic
 
Operating system
Operating systemOperating system
Operating system
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
GPRS Technology
GPRS TechnologyGPRS Technology
GPRS Technology
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
 
4G technology
4G technology4G technology
4G technology
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Smoothing Filters in Spatial Domain
Smoothing Filters in Spatial DomainSmoothing Filters in Spatial Domain
Smoothing Filters in Spatial Domain
 

Recently uploaded

Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentBharaniDharan195623
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectssuserb6619e
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 

Recently uploaded (20)

Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managament
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 

Divide and conquer - Quick sort

  • 2. DIVIDE AND CONQUER  An algorithm is only called divide and conquer if it contains two or more recursive calls  The divide and conquer strategy used to  make a more efficient search algorithm  can also be applied to sorting 2
  • 3. DIVIDE AND CONQUER (CONT.)  Divide : Divide the problem P into P1 , P2,……Pk sub problems until P is smaller one  Conquer : the sub-problems by solving them recursively  Combine : the solutions of sub problems to create a solution to the original problem 3
  • 4. 4 A problem size n Sub problem 1 of size n/2 Sub problem 2 of size n/2 A solution to sub problem 1 A solution to sub problem 2 A solution to the original problem DIVIDE AND CONQUER (CONT.)
  • 5. DIVIDE AND CONQUER - EXAMPLE  Sorting: merge sort and quicksort  Binary tree traversals  Binary search  Multiplication of large integers  Matrix multiplication: Strassen’s algorithm  Closest-pair and convex-hull algorithms 5
  • 6. MERITS OF DIVIDE AND CONQUER 6  Time spent on executing the problem using divide and conquer is smaller than others.  Provides efficient algorithm.  Suited for parallel computation in which each sub problem can be solved simultaneously by its own processor.
  • 7. Quick sort  The main idea is to partition the array into two regions: small items are moved to the left side of the array large items are moved to the right side  After partitioning, repeat the sort on the left and right sides each region is a sub-problem, a smaller version of the original problem 7
  • 8. Quick sort 8  Main question: how do we decide which items are “small” and which are “large”?  A common technique: use the first item in the region as a pivot everything less than the pivot ends up in the left region items greater than or equal to the pivot go in the right region
  • 9. Quick sort 1. Divide: partition A[p..r] into two sub arrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is ≤ A[q], and each element of A[q+1..r] is ≥ A[q]. Compute q as part of this partitioning. 2. Conquer: sort the sub arrays A[p..q-1] and A[q+1..r] by recursive calls to QUICKSORT. 3. Combine: the partitioning and recursive sorting leave us with a sorted A[p..r] 9
  • 10. Quick sort algorithm QUICKSORT(A[p,…r])  Input: A[p,….r]  Output: the sub array A[1…r] stored in non decreasing order If p<r  q= PARTITION(A,p,r)  QUICKSORT(A[p,q-1])  QUICKSORT(A[q+1, r]) 10 v v S1 S2 S
  • 11. Quick sort algorithm (Cont.) 11 PARTITION(A[p…r]) Initialization  P=A[p]  i=p  j=r+1
  • 12. Quick sort algorithm (Cont.) 12  Repeat Repeat i=i+1 until A[i]≥P Repeat j=j-1 until A[j]≤P Swap(A[i],A[j])  Until i ≥ j  Swap(A[p],a[j]);  Return j
  • 13. Quick sort algorithm (Cont.) 13 Void swap( int *a, int *b) { int t; t=*a; *a=*b; *b=t; }
  • 14. COMPLEXITY OF QUICK SORT 14  Space complexity  Time complexity  Choosing pivot element
  • 15. 15 Space complexity:  Space for quick sort: Each and every recursive call require stack space for an array that is q.  Space for partition: Space for an array A=‘n’ locations Space for control variable= 3 location (i,j,pivot) COMPLEXITY OF QUICK SORT (Cont.)
  • 16. 16 Time complexity  Best case : n log2n  Worst case : n2  Average case : 1.38 n log2n COMPLEXITY OF QUICK SORT (Cont.)
  • 17. 17 Choosing pivot element  Selection of proper pivot, improve the efficiency of an algorithm.  Median of three partition method uses pivot as the median of left most, right most, and middle element of the array. COMPLEXITY OF QUICK SORT (Cont.)
  • 18. BENEFITS OF QUICK SORT 18  Sorting time is very less when comparing with all other sorts.  Idea of partitioning can be useful in many applications.