SlideShare a Scribd company logo
1 of 32
This Presentation:
• Describes the Quicksort algorithm,
• Shows its Java source code,
• Explains how to derive its time complexity,
• Tests whether the performance of the Java
implementation matches the expected runtime
behavior,
• Introduces various algorithm optimizations
(combination with Insertion Sort and Dual-Pivot
Quicksort)
• Measures and compares their speed.
• Quicksort-Algorithm
• Source Code
• Time Complexity
ANIMATED VISUALIZATION OF THE QUICKSORT ALGORITHM. THE
HORIZONTAL LINES ARE PIVOT VALUES.
Class Sorting algorithm
Worst-case performance O(n
2
)
Best-case performance O(n log n) (simple partition)
or O(n) (three-way partition and
equal keys)
Average performance O(n log n)
Worst-case space complexity O(n) auxiliary (naive)
O(log n) auxiliary (Hoare 1962)
Quicksort Algorithm
Quicksort works according to the "divide and
conquer" principle:
The subarrays to the left and right of the pivot
element are still unsorted after partitioning
We now have four sections: Section A turned into A1
and A2; B turned into B1 and B2.
The two partitions A2a and A2b that emerged
from A2 in this step are again of length one
QUICKSORT
PARTITIONING
• In the example from above this works as follows:
• The first element from the left, which is larger than pivot
element 6, is 7.
• The first element from the right, which is smaller than the 6,
is the 4.
• The 7 and the 4 are swapped.
We continue searching and find the 8 from the left
(the 1 is already on the correct side) and the 5
from the right (the 9 is also already on the correct
side). We swap 8 and 5:
Now the left and right search positions meet at 2.
The swapping ends here
To put the pivot element at the beginning of the
right partition, we swap the 8 with the 6:
THE PIVOT
ELEMENT
• Advantage of the "last element"
pivot strategy
• Disadvantage of the "last element"
pivot strategy
• Alternative pivot strategies
The partitioning is complete: The 6 is in the
correct position, the numbers to the left are
smaller, and the numbers to the right are larger.
So, we have reached the state that was shown in
the previous section after the first partitioning:
QUICKSORT JAVA SOURCE CODE
QUICKSORT TIME COMPLEXITY
Best-Case Time Complexity
The best-case time complexity of Quicksort is O(n log n)
AVERAGE-CASE TIME COMPLEXITY
Worst-case Time Complexity
The worst-case time complexity of Quicksort is O(n²)
JAVA QUICKSORT RUNTIME
• It sorts arrays of sizes 1,024, 2,048, 4,096, etc. Up to a maximum of 536,870,912 (= 229),
but aborts if a single sorting process takes 20 seconds or longer.
• It applies the sorting algorithm to unsorted input data and input data sorted in ascending
and descending order.
• It first runs two warmup phases to allow the hotspot to optimize the code.
• The process is repeated until the process is killed.
RUNTIME
MEASUREMENT OF THE
QUICKSORT
ALGORITHM VARIANTS
• The simple algorithm is the fastest.
• For all algorithm variants, the pivot
strategy right is fastest, closely
followed by middle, then median with a
slightly larger distance (the overhead is
higher than the gain here). RANDOM
is slowest (generating random
numbers is expensive).
• For all pivot strategies, variant 1 is the
fastest, variant 3 the second fastest,
and variant 2 is the slowest.
RUNTIME MEASUREMENTS FOR DIFFERENT
PIVOT STRATEGIES AND ARRAY SIZES
The data shows:
For both unsorted and sorted input data, doubling the array size requires slightly more than
twice the time. This corresponds to the expected quasilinear runtime – O(n log n).
OVERVIEW OF ALL MEASUREMENT RESULTS
Here you can find the measurement results again as a diagram (We have omitted input
data sorted in descending order for clarity):
QUICKSORT OPTIMIZED: COMBINATION WITH INSERTION SORT
The source code changes compared to the standard quicksort are very straightforward and are limited
to the quicksort() method. Here is the method from the standard algorithm once again:
And here is the optimized version. The variables insertionSort and quicksort are instances of the
respective sorting algorithm. Only the code block commented with "Threshold for insertion sort
reached?" has been added in the middle of the method:
QUICKSORT/INSERTION SORT
PERFORMANCE
The CompareImprovedQuickSort program measures
the time needed to sort about 5.5 million elements at
different thresholds for switching to Insertion Sort.
HERE ARE THE MEASUREMENTS IN GRAPHICAL
REPRESENTATION:
DUAL-PIVOT QUICKSORT
The following diagram shows an example of partitioning with two pivot elements at the "thirds" positions: Dual-
Pivot Quicksort (with additional optimizations) is used in the JDK by the method Arrays.sort())
DUAL-PIVOT QUICKSORT SOURCE CODE
Compared to the regular algorithm, the quicksort() method calls itself
recursively not for two but three partitions:
Then again, two search pointers run over the array from left and right and compare and swap the elements to be
eventually divided into three partitions. How exactly they do this can be read reasonably well from the source code.
DUAL-PIVOT QUICKSORT COMBINED WITH INSERTION SORT
Just like the regular Quicksort, Dual-Pivot
Quicksort can be combined with Insertion
Sort.
Dual-Pivot Quicksort Performance
COMPARING ALL QUICKSORT OPTIMIZATIONS
STABILITY OF QUICKSORT
Because of the way elements within the partitioning are
divided into subsections, elements with the same key
can change their original order.
Here is a simple example:
The array [7, 8, 7, 2, 6] should be partitioned with the
pivot strategy "right element". (We marked the second 7
as 7' to distinguish it from the first one).
The first element from the left that is greater than
6 is the first 7.
The first 7 is no longer ahead, but
behind the second 7
OPTIMIZATION
Two other important optimizations, also widely used in practice, are:
1. Make sure at most O(log n) space is used.
2. When the number of elements is less than the threshold k, simply stop; then after
the whole array has been processed, perform insertion sort on it
CONCLUSION
Quicksort is an efficient, unstable sorting algorithm with time complexity of o(n log n) in the
best and average case and o(n²) in the worst case.
THANKS
FOR
YOUR ATTENTION !
Prepared by: Jasur Ahmadov

More Related Content

What's hot

Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderAshin Guha Majumder
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapSrinivasan Raghvan
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Parallel sorting Algorithms
Parallel  sorting AlgorithmsParallel  sorting Algorithms
Parallel sorting AlgorithmsGARIMA SHAKYA
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesPriyanka Rana
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsAbimbola Idowu
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge SortGelo Maribbay
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method Shantanu Mishra
 
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault ToleranceParallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault ToleranceUniversity of Technology - Iraq
 
Bitonic Sort in Shared SIMD Array Processor
Bitonic Sort in Shared SIMD Array ProcessorBitonic Sort in Shared SIMD Array Processor
Bitonic Sort in Shared SIMD Array ProcessorAsanka Dilruk
 
Svd filtered temporal usage clustering
Svd filtered temporal usage clusteringSvd filtered temporal usage clustering
Svd filtered temporal usage clusteringLiang Xie, PhD
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingTiểu Hổ
 

What's hot (20)

Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmap
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Parallel quicksort cz. 1
Parallel quicksort cz. 1Parallel quicksort cz. 1
Parallel quicksort cz. 1
 
Parallel sorting Algorithms
Parallel  sorting AlgorithmsParallel  sorting Algorithms
Parallel sorting Algorithms
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
 
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault ToleranceParallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
 
Bitonic Sort in Shared SIMD Array Processor
Bitonic Sort in Shared SIMD Array ProcessorBitonic Sort in Shared SIMD Array Processor
Bitonic Sort in Shared SIMD Array Processor
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Svd filtered temporal usage clustering
Svd filtered temporal usage clusteringSvd filtered temporal usage clustering
Svd filtered temporal usage clustering
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 

Similar to Complexity of algorithms

Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
A unique sorting algorithm with linear time & space complexity
A unique sorting algorithm with linear time & space complexityA unique sorting algorithm with linear time & space complexity
A unique sorting algorithm with linear time & space complexityeSAT Journals
 
An OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
An OpenCL Method of Parallel Sorting Algorithms for GPU ArchitectureAn OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
An OpenCL Method of Parallel Sorting Algorithms for GPU ArchitectureWaqas Tariq
 
Quicksort algorithm and implantation process
Quicksort algorithm and implantation processQuicksort algorithm and implantation process
Quicksort algorithm and implantation processsamiulhasan0186
 
Implementation of low power divider techniques using
Implementation of low power divider techniques usingImplementation of low power divider techniques using
Implementation of low power divider techniques usingeSAT Publishing House
 
Implementation of low power divider techniques using radix
Implementation of low power divider techniques using radixImplementation of low power divider techniques using radix
Implementation of low power divider techniques using radixeSAT Journals
 
The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)theijes
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxskilljiolms
 
Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...IRJET Journal
 
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...Filip Krikava
 
SQUARE ROOT SORTING ALGORITHM
SQUARE ROOT SORTING ALGORITHMSQUARE ROOT SORTING ALGORITHM
SQUARE ROOT SORTING ALGORITHMMirOmranudinAbhar
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure Eman magdy
 
Restoring and Non-Restoring division algo for CSE
Restoring and Non-Restoring division algo for CSERestoring and Non-Restoring division algo for CSE
Restoring and Non-Restoring division algo for CSEARoy10
 
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015vtunotesbysree
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
Presentation_Parallel GRASP algorithm for job shop scheduling
Presentation_Parallel GRASP algorithm for job shop schedulingPresentation_Parallel GRASP algorithm for job shop scheduling
Presentation_Parallel GRASP algorithm for job shop schedulingAntonio Maria Fiscarelli
 
IRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
IRJET- Radix 8 Booth Encoded Interleaved Modular MultiplicationIRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
IRJET- Radix 8 Booth Encoded Interleaved Modular MultiplicationIRJET Journal
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithmsDanish Javed
 
Hadoop secondary sort and a custom comparator
Hadoop secondary sort and a custom comparatorHadoop secondary sort and a custom comparator
Hadoop secondary sort and a custom comparatorSubhas Kumar Ghosh
 

Similar to Complexity of algorithms (20)

Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
A unique sorting algorithm with linear time & space complexity
A unique sorting algorithm with linear time & space complexityA unique sorting algorithm with linear time & space complexity
A unique sorting algorithm with linear time & space complexity
 
An OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
An OpenCL Method of Parallel Sorting Algorithms for GPU ArchitectureAn OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
An OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
 
Quicksort algorithm and implantation process
Quicksort algorithm and implantation processQuicksort algorithm and implantation process
Quicksort algorithm and implantation process
 
Implementation of low power divider techniques using
Implementation of low power divider techniques usingImplementation of low power divider techniques using
Implementation of low power divider techniques using
 
Implementation of low power divider techniques using radix
Implementation of low power divider techniques using radixImplementation of low power divider techniques using radix
Implementation of low power divider techniques using radix
 
The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...Analysis of different multiplication algorithm and FPGA implementation of rec...
Analysis of different multiplication algorithm and FPGA implementation of rec...
 
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
SQUARE ROOT SORTING ALGORITHM
SQUARE ROOT SORTING ALGORITHMSQUARE ROOT SORTING ALGORITHM
SQUARE ROOT SORTING ALGORITHM
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
Restoring and Non-Restoring division algo for CSE
Restoring and Non-Restoring division algo for CSERestoring and Non-Restoring division algo for CSE
Restoring and Non-Restoring division algo for CSE
 
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)
 
Presentation_Parallel GRASP algorithm for job shop scheduling
Presentation_Parallel GRASP algorithm for job shop schedulingPresentation_Parallel GRASP algorithm for job shop scheduling
Presentation_Parallel GRASP algorithm for job shop scheduling
 
IRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
IRJET- Radix 8 Booth Encoded Interleaved Modular MultiplicationIRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
IRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 
Hadoop secondary sort and a custom comparator
Hadoop secondary sort and a custom comparatorHadoop secondary sort and a custom comparator
Hadoop secondary sort and a custom comparator
 

Recently uploaded

Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...amitlee9823
 
Chapter-1.3-Four-Basic-Computer-periods.pptx
Chapter-1.3-Four-Basic-Computer-periods.pptxChapter-1.3-Four-Basic-Computer-periods.pptx
Chapter-1.3-Four-Basic-Computer-periods.pptxAnjieVillarba1
 
Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class Service 100% Saf...
Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class  Service 100% Saf...Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class  Service 100% Saf...
Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class Service 100% Saf...shivangimorya083
 
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
How To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation IssueHow To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation IssueTerry Sayther Automotive
 
Call me @ 9892124323 Call Girl in Andheri East With Free Home Delivery
Call me @ 9892124323 Call Girl in Andheri East With Free Home DeliveryCall me @ 9892124323 Call Girl in Andheri East With Free Home Delivery
Call me @ 9892124323 Call Girl in Andheri East With Free Home DeliveryPooja Nehwal
 
Hyundai World Rally Team in action at 2024 WRC
Hyundai World Rally Team in action at 2024 WRCHyundai World Rally Team in action at 2024 WRC
Hyundai World Rally Team in action at 2024 WRCHyundai Motor Group
 
John Deere Tractors 6130M 6140M Diagnostic Manual
John Deere Tractors  6130M 6140M Diagnostic ManualJohn Deere Tractors  6130M 6140M Diagnostic Manual
John Deere Tractors 6130M 6140M Diagnostic ManualExcavator
 
Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...amitlee9823
 
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理ezgenuh
 
John Deere 335 375 385 435 Service Repair Manual
John Deere 335 375 385 435 Service Repair ManualJohn Deere 335 375 385 435 Service Repair Manual
John Deere 335 375 385 435 Service Repair ManualExcavator
 
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Delhi Call girls
 
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...anilsa9823
 
How To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative ErrorHow To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative ErrorAndres Auto Service
 
Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...amitlee9823
 
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp NumberVip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Numberkumarajju5765
 
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...amitlee9823
 
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国ezgenuh
 

Recently uploaded (20)

Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
 
Chapter-1.3-Four-Basic-Computer-periods.pptx
Chapter-1.3-Four-Basic-Computer-periods.pptxChapter-1.3-Four-Basic-Computer-periods.pptx
Chapter-1.3-Four-Basic-Computer-periods.pptx
 
Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class Service 100% Saf...
Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class  Service 100% Saf...Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class  Service 100% Saf...
Hot Modals Call Girls (Delhi) Dwarka9711199171✔️ High Class Service 100% Saf...
 
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
How To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation IssueHow To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation Issue
 
Call me @ 9892124323 Call Girl in Andheri East With Free Home Delivery
Call me @ 9892124323 Call Girl in Andheri East With Free Home DeliveryCall me @ 9892124323 Call Girl in Andheri East With Free Home Delivery
Call me @ 9892124323 Call Girl in Andheri East With Free Home Delivery
 
Hyundai World Rally Team in action at 2024 WRC
Hyundai World Rally Team in action at 2024 WRCHyundai World Rally Team in action at 2024 WRC
Hyundai World Rally Team in action at 2024 WRC
 
Stay Cool and Compliant: Know Your Window Tint Laws Before You Tint
Stay Cool and Compliant: Know Your Window Tint Laws Before You TintStay Cool and Compliant: Know Your Window Tint Laws Before You Tint
Stay Cool and Compliant: Know Your Window Tint Laws Before You Tint
 
John Deere Tractors 6130M 6140M Diagnostic Manual
John Deere Tractors  6130M 6140M Diagnostic ManualJohn Deere Tractors  6130M 6140M Diagnostic Manual
John Deere Tractors 6130M 6140M Diagnostic Manual
 
(INDIRA) Call Girl Surat Call Now 8250077686 Surat Escorts 24x7
(INDIRA) Call Girl Surat Call Now 8250077686 Surat Escorts 24x7(INDIRA) Call Girl Surat Call Now 8250077686 Surat Escorts 24x7
(INDIRA) Call Girl Surat Call Now 8250077686 Surat Escorts 24x7
 
Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Mira Road Call On 9920725232 With Body to body massage ...
 
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
 
John Deere 335 375 385 435 Service Repair Manual
John Deere 335 375 385 435 Service Repair ManualJohn Deere 335 375 385 435 Service Repair Manual
John Deere 335 375 385 435 Service Repair Manual
 
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
 
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
 
How To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative ErrorHow To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
 
Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Kadugodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp NumberVip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
 
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
 

Complexity of algorithms

  • 1. This Presentation: • Describes the Quicksort algorithm, • Shows its Java source code, • Explains how to derive its time complexity, • Tests whether the performance of the Java implementation matches the expected runtime behavior, • Introduces various algorithm optimizations (combination with Insertion Sort and Dual-Pivot Quicksort) • Measures and compares their speed. • Quicksort-Algorithm • Source Code • Time Complexity
  • 2. ANIMATED VISUALIZATION OF THE QUICKSORT ALGORITHM. THE HORIZONTAL LINES ARE PIVOT VALUES. Class Sorting algorithm Worst-case performance O(n 2 ) Best-case performance O(n log n) (simple partition) or O(n) (three-way partition and equal keys) Average performance O(n log n) Worst-case space complexity O(n) auxiliary (naive) O(log n) auxiliary (Hoare 1962)
  • 3. Quicksort Algorithm Quicksort works according to the "divide and conquer" principle: The subarrays to the left and right of the pivot element are still unsorted after partitioning
  • 4. We now have four sections: Section A turned into A1 and A2; B turned into B1 and B2. The two partitions A2a and A2b that emerged from A2 in this step are again of length one
  • 5. QUICKSORT PARTITIONING • In the example from above this works as follows: • The first element from the left, which is larger than pivot element 6, is 7. • The first element from the right, which is smaller than the 6, is the 4. • The 7 and the 4 are swapped. We continue searching and find the 8 from the left (the 1 is already on the correct side) and the 5 from the right (the 9 is also already on the correct side). We swap 8 and 5:
  • 6. Now the left and right search positions meet at 2. The swapping ends here To put the pivot element at the beginning of the right partition, we swap the 8 with the 6:
  • 7. THE PIVOT ELEMENT • Advantage of the "last element" pivot strategy • Disadvantage of the "last element" pivot strategy • Alternative pivot strategies The partitioning is complete: The 6 is in the correct position, the numbers to the left are smaller, and the numbers to the right are larger. So, we have reached the state that was shown in the previous section after the first partitioning:
  • 10. The best-case time complexity of Quicksort is O(n log n)
  • 11. AVERAGE-CASE TIME COMPLEXITY Worst-case Time Complexity The worst-case time complexity of Quicksort is O(n²)
  • 12. JAVA QUICKSORT RUNTIME • It sorts arrays of sizes 1,024, 2,048, 4,096, etc. Up to a maximum of 536,870,912 (= 229), but aborts if a single sorting process takes 20 seconds or longer. • It applies the sorting algorithm to unsorted input data and input data sorted in ascending and descending order. • It first runs two warmup phases to allow the hotspot to optimize the code. • The process is repeated until the process is killed.
  • 13. RUNTIME MEASUREMENT OF THE QUICKSORT ALGORITHM VARIANTS • The simple algorithm is the fastest. • For all algorithm variants, the pivot strategy right is fastest, closely followed by middle, then median with a slightly larger distance (the overhead is higher than the gain here). RANDOM is slowest (generating random numbers is expensive). • For all pivot strategies, variant 1 is the fastest, variant 3 the second fastest, and variant 2 is the slowest.
  • 14. RUNTIME MEASUREMENTS FOR DIFFERENT PIVOT STRATEGIES AND ARRAY SIZES
  • 15. The data shows: For both unsorted and sorted input data, doubling the array size requires slightly more than twice the time. This corresponds to the expected quasilinear runtime – O(n log n).
  • 16.
  • 17. OVERVIEW OF ALL MEASUREMENT RESULTS Here you can find the measurement results again as a diagram (We have omitted input data sorted in descending order for clarity):
  • 18. QUICKSORT OPTIMIZED: COMBINATION WITH INSERTION SORT The source code changes compared to the standard quicksort are very straightforward and are limited to the quicksort() method. Here is the method from the standard algorithm once again:
  • 19. And here is the optimized version. The variables insertionSort and quicksort are instances of the respective sorting algorithm. Only the code block commented with "Threshold for insertion sort reached?" has been added in the middle of the method:
  • 20. QUICKSORT/INSERTION SORT PERFORMANCE The CompareImprovedQuickSort program measures the time needed to sort about 5.5 million elements at different thresholds for switching to Insertion Sort.
  • 21. HERE ARE THE MEASUREMENTS IN GRAPHICAL REPRESENTATION:
  • 22. DUAL-PIVOT QUICKSORT The following diagram shows an example of partitioning with two pivot elements at the "thirds" positions: Dual- Pivot Quicksort (with additional optimizations) is used in the JDK by the method Arrays.sort())
  • 23. DUAL-PIVOT QUICKSORT SOURCE CODE Compared to the regular algorithm, the quicksort() method calls itself recursively not for two but three partitions:
  • 24. Then again, two search pointers run over the array from left and right and compare and swap the elements to be eventually divided into three partitions. How exactly they do this can be read reasonably well from the source code.
  • 25.
  • 26. DUAL-PIVOT QUICKSORT COMBINED WITH INSERTION SORT Just like the regular Quicksort, Dual-Pivot Quicksort can be combined with Insertion Sort. Dual-Pivot Quicksort Performance
  • 27. COMPARING ALL QUICKSORT OPTIMIZATIONS
  • 28. STABILITY OF QUICKSORT Because of the way elements within the partitioning are divided into subsections, elements with the same key can change their original order. Here is a simple example: The array [7, 8, 7, 2, 6] should be partitioned with the pivot strategy "right element". (We marked the second 7 as 7' to distinguish it from the first one).
  • 29. The first element from the left that is greater than 6 is the first 7. The first 7 is no longer ahead, but behind the second 7
  • 30. OPTIMIZATION Two other important optimizations, also widely used in practice, are: 1. Make sure at most O(log n) space is used. 2. When the number of elements is less than the threshold k, simply stop; then after the whole array has been processed, perform insertion sort on it
  • 31. CONCLUSION Quicksort is an efficient, unstable sorting algorithm with time complexity of o(n log n) in the best and average case and o(n²) in the worst case.