SlideShare a Scribd company logo
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

Selection sort
Selection sortSelection sort
Selection sort
stella D
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Shweta Sahu
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv21
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
Shubham Dwivedi
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
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
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
MdSaiful14
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 

What's hot (20)

Selection sort
Selection sortSelection sort
Selection sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 

Similar to Quick sort

Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
Deepak 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.pptx
DeepakM509554
 
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
Radhika Talaviya
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
sajinis3
 
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.pptx
nikshaikh786
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Soumen Santra
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
Krish_ver2
 
Sorting
SortingSorting
Sorting
Gopi Saiteja
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
Dr. Shaukat Wasi
 

Similar to Quick sort (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

Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
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
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 

Recently uploaded (20)

Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 

Quick sort

  • 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