SlideShare a Scribd company logo
1 of 9
QuickSort
• Quick sort works by partitioning the array to be sorted.
• each partition is internally sorted recursively.
• As a first Step, this algorithm chooses one elements of an array as a pivot or a key element.
• the array is then partitioned on either side of the pivot.
• elements are moved so that those greater then the pivot are shifted to its right where as
the others are shifted to its left.
• two pointers low and up are initialized to the lower and upper boundes of the sub array.
• up pointer will be decremented and low pointer will be incremented as per following
condition.
1. Increase low pointer until t[low] > pivot.
2. Decrease up pointer until t[up] <= pivot.
3. If low < up then interchange t[low] with t[up].
4. If up <= low then interchange t[up] with t[i].
Algorithm
Produce pivot(T[i,...,j];var |)
{Permutes the elements in array T[i,...,j] and returns a value i such that, at the end, i<=1<=j,
T[k]<=p for all i<=k<i, t[i]=p, and t[k]>p for all i<k<=j, where p is the initial value t[i]}
P<--T[i]
K<--I; I<--J+1
Repeat k<--k+1 until t[k]>p
Repeat I<--I-1 untill T[i]<=p
While k < I do
Swap T[k] and T[i]
Repeat k<--k+1Untill T[k]>p
Repeat I<--I-1 Untill T[i]<=p
Swap T[i] and T[i]
Procedure quicksort(T[i,...,j])
{Sorts subarray T[i,...,j] into non decreasing order}
If j -- i is sufficiently small then insert (T[i,...,j])
else
Pivot(T[i,...,j],i)
Quicksort(T[i,..., I --1])
Quicksort(T[i+1,...,j])
Analysis
1. Worst case
• Running time of quick sort depends on whether the partitioning is balanced or unbalanced.
• And this in turn depends on which elements elements is chosen as key or pivot elements.
• The worst case behavior for quick sort occurs when the partitioning routine produces one
sub problem with n--1 elements and one with 0 elements.
• In this case recurrence will be,
T(n)=t(n-1)+t(0)+o(n)
T(n)=t(n-1)+o(n)
T(n)=o(n2)
2. Best case
• Occures when partition produces sub problems each of size n/2.
• Recurrence equation:
T(n)=2t(n/2)+o(n)
I=2, B=2, K=1, so I=Bk
T(n)=o(nlogn)
3. Average case
• Average case running time is much closer to the best case.
• If suppose the partitioning algorithm produces a 9--to--1 proportional split the recurrence
will be
T(n)=t(9n/10)+t(n/10)+o(n)
Solving it,
T(n)=o(nlogn)
• The running time of quick sort is therefor o(nlogn) whenever the split has constant
proportionality.
20 10 80 100307506040
Pivot,i j
Here, pivot = T[i], k = 1, i=j+1
Repeat k = k+1, Untill T[k] > pivot
Repeat i = i-1, Untill T[k] <= pivot
20 10 80 100307506040
Pivot K i
Is k < I ? , Yes then Swap T[k] <--> T[i]
Example
20 10 30 100807506040
Pivot K i
Repeat i = i-1, Untill T[k] <= Pivot
Repeat k = k+1, Untill T[k] > Pivot
20 10 30 100807506040
Pivot K i
Is k < I ? , Yes then Swap T[k] <--> T[i]
20 10 30 100806050740
Pivot K i
Repeat k = k+1, Untill T[k] > pivot
Repeat i = i-1, Untill T[k] <= pivot
20 10 30 100806050740
Pivot Ki
Is k < I ? , No then Swap Pivot <--> T[i]
20 10 30 100806050407
Pivot
• Now we have two sub list one having elements less or equal pivot and second
having elements greater than pivot.
20 10 30 1008060507
Pivot,i j Pivot,i
40
j
• Sorted Array
10 20 30 100806050407
Thank You

More Related Content

What's hot

What's hot (20)

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Red black tree
Red black treeRed black tree
Red black tree
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Sorting
SortingSorting
Sorting
 
Quick sort
Quick sortQuick sort
Quick sort
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Array data structure
Array data structureArray data structure
Array data structure
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Shell sort
Shell sortShell sort
Shell sort
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 

Similar to QuickSort Algorithm Explained in 40 Characters

Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisRadhika Talaviya
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxnikshaikh786
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 

Similar to QuickSort Algorithm Explained in 40 Characters (20)

Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
Quicksort
QuicksortQuicksort
Quicksort
 
Lec35
Lec35Lec35
Lec35
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Sorting
SortingSorting
Sorting
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 

Recently uploaded

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
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
 

Recently uploaded (20)

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
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
 

QuickSort Algorithm Explained in 40 Characters

  • 1. QuickSort • Quick sort works by partitioning the array to be sorted. • each partition is internally sorted recursively. • As a first Step, this algorithm chooses one elements of an array as a pivot or a key element. • the array is then partitioned on either side of the pivot. • elements are moved so that those greater then the pivot are shifted to its right where as the others are shifted to its left. • two pointers low and up are initialized to the lower and upper boundes of the sub array. • up pointer will be decremented and low pointer will be incremented as per following condition. 1. Increase low pointer until t[low] > pivot. 2. Decrease up pointer until t[up] <= pivot. 3. If low < up then interchange t[low] with t[up]. 4. If up <= low then interchange t[up] with t[i].
  • 2. Algorithm Produce pivot(T[i,...,j];var |) {Permutes the elements in array T[i,...,j] and returns a value i such that, at the end, i<=1<=j, T[k]<=p for all i<=k<i, t[i]=p, and t[k]>p for all i<k<=j, where p is the initial value t[i]} P<--T[i] K<--I; I<--J+1 Repeat k<--k+1 until t[k]>p Repeat I<--I-1 untill T[i]<=p While k < I do Swap T[k] and T[i] Repeat k<--k+1Untill T[k]>p Repeat I<--I-1 Untill T[i]<=p Swap T[i] and T[i] Procedure quicksort(T[i,...,j]) {Sorts subarray T[i,...,j] into non decreasing order} If j -- i is sufficiently small then insert (T[i,...,j]) else Pivot(T[i,...,j],i) Quicksort(T[i,..., I --1]) Quicksort(T[i+1,...,j])
  • 3. Analysis 1. Worst case • Running time of quick sort depends on whether the partitioning is balanced or unbalanced. • And this in turn depends on which elements elements is chosen as key or pivot elements. • The worst case behavior for quick sort occurs when the partitioning routine produces one sub problem with n--1 elements and one with 0 elements. • In this case recurrence will be, T(n)=t(n-1)+t(0)+o(n) T(n)=t(n-1)+o(n) T(n)=o(n2) 2. Best case • Occures when partition produces sub problems each of size n/2. • Recurrence equation: T(n)=2t(n/2)+o(n) I=2, B=2, K=1, so I=Bk T(n)=o(nlogn)
  • 4. 3. Average case • Average case running time is much closer to the best case. • If suppose the partitioning algorithm produces a 9--to--1 proportional split the recurrence will be T(n)=t(9n/10)+t(n/10)+o(n) Solving it, T(n)=o(nlogn) • The running time of quick sort is therefor o(nlogn) whenever the split has constant proportionality.
  • 5. 20 10 80 100307506040 Pivot,i j Here, pivot = T[i], k = 1, i=j+1 Repeat k = k+1, Untill T[k] > pivot Repeat i = i-1, Untill T[k] <= pivot 20 10 80 100307506040 Pivot K i Is k < I ? , Yes then Swap T[k] <--> T[i] Example
  • 6. 20 10 30 100807506040 Pivot K i Repeat i = i-1, Untill T[k] <= Pivot Repeat k = k+1, Untill T[k] > Pivot 20 10 30 100807506040 Pivot K i Is k < I ? , Yes then Swap T[k] <--> T[i] 20 10 30 100806050740 Pivot K i
  • 7. Repeat k = k+1, Untill T[k] > pivot Repeat i = i-1, Untill T[k] <= pivot 20 10 30 100806050740 Pivot Ki Is k < I ? , No then Swap Pivot <--> T[i] 20 10 30 100806050407 Pivot
  • 8. • Now we have two sub list one having elements less or equal pivot and second having elements greater than pivot. 20 10 30 1008060507 Pivot,i j Pivot,i 40 j • Sorted Array 10 20 30 100806050407