SlideShare a Scribd company logo
Presented by:-AREEN GAUR(2020PCP5516)
M .Tech Ist year(CSE,MNIT Jaipur)
Submitted To: Prof. Vijay Laxmi
(CSE dept. MNIT Jaipur)
Points Covered:
 Description of Quick sort
 Algorithm of Quick sort
 Example
 Performance and recurrence relation
 Best case partitioning and time complexity with derivation
 Worst case partitioning and time complexity with derivation
 Balanced partitioning and general equation for best case
 Input for best and worst case
2
Description
 This sorting algorithm uses Divide and Conquer approach , the three-step
divide-and-conquer process for sorting a array A[p….r]:
 DIVIDE-Partition the array A[p…r] into two subarrays A[p…q-1] and
A[q+1..r] such that each element of left subarray is less than or equal to A[q]
and each element of right subarray is greater than or equal to A[q] , for this
partitioning we need to compute the index q.
 Conquer-Sort the two subarrays by recursive calls to quick sort.
 Combine-combine the two sorted subarrays
 Quick sort is inplace and not stable sorting algo.
3
Procedure
Quicksort(A, p, r)
1. if (p < r)
2. q=partition(A, p, r)
3. Quicksort(A, p, q-1)
4. Quicksort(A, q+1, r)///initial call is Quicksort(A,0,n-1) where n is no. of elements in array
partition(A, p, r)
1. x=A[r]
2. i = p-1
3. for j =p to r-1
4. if (A[j]≤x)
5. i = i+1
6. exchange A[i] with A[j]
7. exchange A[i+1] with A[r]
8. return i+1
4
Example
2 8 7 1 3 5 6 4
p , i=0 j=1 2 3 4 5 6 r=7
5
2 8 7 1 3 5 6 4
p , j=0 1 2 3 4 5 6 r=7
2 8 7 1 3 5 6 4
p , i=0 1 j=2 3 4 5 6 r=7
2 8 7 1 3 5 6 4
p , i=0 1 2 j=3 4 5 6 r=7
2 1 7 8 3 5 6 4
0 i=1 2 3 j=4 5 6 r=7
As i=p-1 so i=-1 x=4
2 1 3 8 7 5 6 4
p=0 1 i=2 3 4 5 6 7
2 1 3 4(pivot) 7 5 6 8
p=0 1 i=2 3 4 5 6 7
2 1 3 8 7 5 6 4
p=0 1 i=2 3 4 j=5 6 r=7
2 1 3 8 7 5 6 4
p=0 1 i=2 3 4 5 j=6 7
6
Left sub array right sub array
Performance and Recurrence Relation
1. The running time depends on whether the partition is balanced or unbalanced.
2. If the partition is balanced the algo runs asymptotically fast with time
complexity q(n logn).
3. If the partition is unbalanced , it can run asymptotically slow with time
complexity q(n2).
7
Best-case partitioning
This occurs when array is divided into two equal subarrays, each of size no more
than n/2, since one of size floor(n/2) and one of size ceil(n/2)-1.
The recurrence relation is :
T(n)=2T(n/2)+q(n)
Using master’s theorem we know time complexity will be q(n logn).
Proof: In general aT(n/b)+f(n) where a ≥ 1 and b>1 and f(n) is positive function
On comparing f(n) and nloga/logb
We get f(n) and nloga/logb asymptotically equal and by case 3 of master’s theorem
we know if f(n)=q(n(loga/logb) . (logn)k ) where k ≥ 0 then
T(n)=q(n(loga/logb).(logn)k+1 )
Hence time complexity in best case is q(n logn).
8
Worst Case partitioning
The worst case behaviour for quick sort occurs when the partitioning routine
produces one subproblem with n-1 elements and one with zero elements.
The recurrence relation is :
T(n)=T(n-1)+T(0)+ q(n)
T(n)=T(n-1)+ q(n)///T(1)=1 base case
Proof: By substitution method
T(n)=T(n-2)+n-1+n
T(n)=T(n-3)+n-2+n-1+n , if we do this n-1 times then
T(n)=T(n-(n-1))+(n-(n-2))+(n-(n-3))+………….n-1+n
T(n)=T(1)+2+3+4+……………………+n-1+n//////this is an ap series
9
Worst Case partitioning(continued)
T(n)=(n(n+1))/2 [sum of n natural numbers]
T(n) is approximately equal to q(n2).
Therefore the worst case running time of quicksort is q(n2).
Moreover the q(n2) running time occurs when the input array is already
completely sorted.
10
Balanced Partitioning
The average case running time of quicksort is much closer to the best case.
T(n) = n + T(an) + T((1-a)n)
where 0<a <1
Therefore the running time of quicksort in balanced partitioning will be q(nlogn).
11
Input for best case :- any unsorted array
example:
5 6 7 8 9 89 90 2
12
Input for worst case :- any sorted array
example:
5 5 5 5 5 5 5 5
5 6 7 8 9 10 11 20
THANK YOU
13

More Related Content

What's hot

Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
Mohamed Loey
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
Preetham Devisetty
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
Krish_ver2
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
Dipayan Sarkar
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Quick sort
Quick sortQuick sort
Quick sort
Abdelrahman Saleh
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Abdelrahman Saleh
 
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
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 

What's hot (20)

Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
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...
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 

Similar to Quick sort

CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
DeepakM509554
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
LakshayYadav46
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Merge Sort
Merge SortMerge Sort
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
MuhammadSheraz836877
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
Edhole.com
 
Sorting
SortingSorting
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
Mahbubur Rahman
 
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
RUHULAMINHAZARIKA
 

Similar to Quick sort (20)

CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Sorting
SortingSorting
Sorting
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
 
Sorting
SortingSorting
Sorting
 
Merge sort
Merge sortMerge sort
Merge sort
 
Lec10
Lec10Lec10
Lec10
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 
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
 

Recently uploaded

14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
Nettur Technical Training Foundation
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
AkolbilaEmmanuel1
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 

Recently uploaded (20)

14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 

Quick sort

  • 1. Presented by:-AREEN GAUR(2020PCP5516) M .Tech Ist year(CSE,MNIT Jaipur) Submitted To: Prof. Vijay Laxmi (CSE dept. MNIT Jaipur)
  • 2. Points Covered:  Description of Quick sort  Algorithm of Quick sort  Example  Performance and recurrence relation  Best case partitioning and time complexity with derivation  Worst case partitioning and time complexity with derivation  Balanced partitioning and general equation for best case  Input for best and worst case 2
  • 3. Description  This sorting algorithm uses Divide and Conquer approach , the three-step divide-and-conquer process for sorting a array A[p….r]:  DIVIDE-Partition the array A[p…r] into two subarrays A[p…q-1] and A[q+1..r] such that each element of left subarray is less than or equal to A[q] and each element of right subarray is greater than or equal to A[q] , for this partitioning we need to compute the index q.  Conquer-Sort the two subarrays by recursive calls to quick sort.  Combine-combine the two sorted subarrays  Quick sort is inplace and not stable sorting algo. 3
  • 4. Procedure Quicksort(A, p, r) 1. if (p < r) 2. q=partition(A, p, r) 3. Quicksort(A, p, q-1) 4. Quicksort(A, q+1, r)///initial call is Quicksort(A,0,n-1) where n is no. of elements in array partition(A, p, r) 1. x=A[r] 2. i = p-1 3. for j =p to r-1 4. if (A[j]≤x) 5. i = i+1 6. exchange A[i] with A[j] 7. exchange A[i+1] with A[r] 8. return i+1 4
  • 5. Example 2 8 7 1 3 5 6 4 p , i=0 j=1 2 3 4 5 6 r=7 5 2 8 7 1 3 5 6 4 p , j=0 1 2 3 4 5 6 r=7 2 8 7 1 3 5 6 4 p , i=0 1 j=2 3 4 5 6 r=7 2 8 7 1 3 5 6 4 p , i=0 1 2 j=3 4 5 6 r=7 2 1 7 8 3 5 6 4 0 i=1 2 3 j=4 5 6 r=7 As i=p-1 so i=-1 x=4
  • 6. 2 1 3 8 7 5 6 4 p=0 1 i=2 3 4 5 6 7 2 1 3 4(pivot) 7 5 6 8 p=0 1 i=2 3 4 5 6 7 2 1 3 8 7 5 6 4 p=0 1 i=2 3 4 j=5 6 r=7 2 1 3 8 7 5 6 4 p=0 1 i=2 3 4 5 j=6 7 6 Left sub array right sub array
  • 7. Performance and Recurrence Relation 1. The running time depends on whether the partition is balanced or unbalanced. 2. If the partition is balanced the algo runs asymptotically fast with time complexity q(n logn). 3. If the partition is unbalanced , it can run asymptotically slow with time complexity q(n2). 7
  • 8. Best-case partitioning This occurs when array is divided into two equal subarrays, each of size no more than n/2, since one of size floor(n/2) and one of size ceil(n/2)-1. The recurrence relation is : T(n)=2T(n/2)+q(n) Using master’s theorem we know time complexity will be q(n logn). Proof: In general aT(n/b)+f(n) where a ≥ 1 and b>1 and f(n) is positive function On comparing f(n) and nloga/logb We get f(n) and nloga/logb asymptotically equal and by case 3 of master’s theorem we know if f(n)=q(n(loga/logb) . (logn)k ) where k ≥ 0 then T(n)=q(n(loga/logb).(logn)k+1 ) Hence time complexity in best case is q(n logn). 8
  • 9. Worst Case partitioning The worst case behaviour for quick sort occurs when the partitioning routine produces one subproblem with n-1 elements and one with zero elements. The recurrence relation is : T(n)=T(n-1)+T(0)+ q(n) T(n)=T(n-1)+ q(n)///T(1)=1 base case Proof: By substitution method T(n)=T(n-2)+n-1+n T(n)=T(n-3)+n-2+n-1+n , if we do this n-1 times then T(n)=T(n-(n-1))+(n-(n-2))+(n-(n-3))+………….n-1+n T(n)=T(1)+2+3+4+……………………+n-1+n//////this is an ap series 9
  • 10. Worst Case partitioning(continued) T(n)=(n(n+1))/2 [sum of n natural numbers] T(n) is approximately equal to q(n2). Therefore the worst case running time of quicksort is q(n2). Moreover the q(n2) running time occurs when the input array is already completely sorted. 10
  • 11. Balanced Partitioning The average case running time of quicksort is much closer to the best case. T(n) = n + T(an) + T((1-a)n) where 0<a <1 Therefore the running time of quicksort in balanced partitioning will be q(nlogn). 11
  • 12. Input for best case :- any unsorted array example: 5 6 7 8 9 89 90 2 12 Input for worst case :- any sorted array example: 5 5 5 5 5 5 5 5 5 6 7 8 9 10 11 20