SlideShare a Scribd company logo
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).
Complexity of algorithms
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.
Complexity of algorithms
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 (20)

PPT
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
PPTX
Java.util.concurrent.concurrent hashmap
Srinivasan Raghvan
 
PPT
Sorting Algorithms
multimedia9
 
PDF
Parallel quicksort cz. 1
Mikołaj Olszewski
 
PDF
Parallel sorting Algorithms
GARIMA SHAKYA
 
PPT
Heaps & Adaptable priority Queues
Priyanka Rana
 
PDF
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
PDF
Insertion sort
Abdelrahman Saleh
 
PPTX
Radix and Merge Sort
Gelo Maribbay
 
PPT
Searching algorithms
Trupti Agrawal
 
PPTX
sorting algorithm graphical method
Shantanu Mishra
 
PDF
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
University of Technology - Iraq
 
PPTX
Bitonic Sort in Shared SIMD Array Processor
Asanka Dilruk
 
PPTX
Sorting algorithms
Eleonora Ciceri
 
PDF
Svd filtered temporal usage clustering
Liang Xie, PhD
 
DOC
Insertion sort
Dorina Isaj
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPT
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
PPT
Maps&hash tables
Priyanka Rana
 
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Java.util.concurrent.concurrent hashmap
Srinivasan Raghvan
 
Sorting Algorithms
multimedia9
 
Parallel quicksort cz. 1
Mikołaj Olszewski
 
Parallel sorting Algorithms
GARIMA SHAKYA
 
Heaps & Adaptable priority Queues
Priyanka Rana
 
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
Insertion sort
Abdelrahman Saleh
 
Radix and Merge Sort
Gelo Maribbay
 
Searching algorithms
Trupti Agrawal
 
sorting algorithm graphical method
Shantanu Mishra
 
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
University of Technology - Iraq
 
Bitonic Sort in Shared SIMD Array Processor
Asanka Dilruk
 
Sorting algorithms
Eleonora Ciceri
 
Svd filtered temporal usage clustering
Liang Xie, PhD
 
Insertion sort
Dorina Isaj
 
Sorting Algorithms
Pranay Neema
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Maps&hash tables
Priyanka Rana
 

Similar to Complexity of algorithms (20)

PDF
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
PDF
A unique sorting algorithm with linear time & space complexity
eSAT Journals
 
PDF
An OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
Waqas Tariq
 
PPTX
Quicksort algorithm and implantation process
samiulhasan0186
 
PDF
Implementation of low power divider techniques using
eSAT Publishing House
 
PDF
Implementation of low power divider techniques using radix
eSAT Journals
 
PDF
The International Journal of Engineering and Science (The IJES)
theijes
 
PPTX
VCE Unit 01 (1).pptx
skilljiolms
 
PDF
Analysis of different multiplication algorithm and FPGA implementation of rec...
IRJET Journal
 
PDF
FQ-ViT: Post-Training Quantization for Fully Quantized Vision Transformer
Sungchul Kim
 
PDF
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
Filip Krikava
 
PPT
Data Structures 6
Dr.Umadevi V
 
PDF
SQUARE ROOT SORTING ALGORITHM
MirOmranudinAbhar
 
PDF
Basics in algorithms and data structure
Eman magdy
 
PPTX
Restoring and Non-Restoring division algo for CSE
ARoy10
 
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
PDF
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
vtunotesbysree
 
PDF
Welcome to International Journal of Engineering Research and Development (IJERD)
IJERD Editor
 
PDF
Presentation_Parallel GRASP algorithm for job shop scheduling
Antonio Maria Fiscarelli
 
PDF
IRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
IRJET Journal
 
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
A unique sorting algorithm with linear time & space complexity
eSAT Journals
 
An OpenCL Method of Parallel Sorting Algorithms for GPU Architecture
Waqas Tariq
 
Quicksort algorithm and implantation process
samiulhasan0186
 
Implementation of low power divider techniques using
eSAT Publishing House
 
Implementation of low power divider techniques using radix
eSAT Journals
 
The International Journal of Engineering and Science (The IJES)
theijes
 
VCE Unit 01 (1).pptx
skilljiolms
 
Analysis of different multiplication algorithm and FPGA implementation of rec...
IRJET Journal
 
FQ-ViT: Post-Training Quantization for Fully Quantized Vision Transformer
Sungchul Kim
 
Integrating Adaptation Mechanisms Using Control Theory Centric Architecture M...
Filip Krikava
 
Data Structures 6
Dr.Umadevi V
 
SQUARE ROOT SORTING ALGORITHM
MirOmranudinAbhar
 
Basics in algorithms and data structure
Eman magdy
 
Restoring and Non-Restoring division algo for CSE
ARoy10
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
VTU 4TH SEM CSE MICROPROCESSORS SOLVED PAPERS OF JUNE-2014 & JUNE-2015
vtunotesbysree
 
Welcome to International Journal of Engineering Research and Development (IJERD)
IJERD Editor
 
Presentation_Parallel GRASP algorithm for job shop scheduling
Antonio Maria Fiscarelli
 
IRJET- Radix 8 Booth Encoded Interleaved Modular Multiplication
IRJET Journal
 
Ad

Recently uploaded (20)

PDF
Referensi Materi pembelajaran dan pengayaan
alimarsikal
 
PDF
Top Tips to Prevent Tire Damage and Extend Tire Life
AutoNation Service Centre Vancouver
 
PDF
Voltage Intensifier Circuit (VIC) and Water Fuel Cell (WFC) Guide_ Simplified...
securesupplies
 
PPTX
Meeting 1 Electrical Engineering Project Design-1.pptx
sitompulvictoryland2
 
PDF
Engine PTO (Power Take Off) Volvo EC360BLC Service Manual.pdf
Service Repair Manual
 
PDF
Water outlet Volvo EC360B LC Service Manual.pdf
Service Repair Manual
 
PPTX
Top IT Products in Hyderabad – Powering Smarter Workplaces.pptx
Indoteq Office Automation Systems
 
PPTX
定制学历UEX在读证明信西班牙埃斯特雷马杜拉大学毕业证明,UEX录取通知书
Taqyea
 
PDF
Daewoo Doosan 430 450 Plus Operator Manual.pdf
Service Repair Manual
 
PDF
lx465 new holland specs Service Repair Manual.pdf
Service Repair Manual
 
PDF
Volvo BM L150 Wheel Loader Service PDF Repair Manual.pdf
Service Repair Manual
 
PDF
Daewoo Doosan 430 440 450 Plus Operator Manual.pdf
Service Repair Manual
 
PDF
Fuel filter Volvo EC360BLC D10 Excavator Service Manual.pdf
Service Repair Manual
 
PPTX
英国学位证(UON毕业证书)诺丁汉大学毕业证书如何办理
Taqyea
 
PDF
Engine oil filter Volvo EC360B LC Excavator Service Manual.pdf
Service Repair Manual
 
PPTX
Advanced product quality planning_Presentation.pptx
qualitynewswan
 
PDF
Arctic Cat 700 H1 Efi 2008 Service Repair Manual.pdf
Service Repair Manual
 
PPTX
What Is the Automatic Transmission Malfunction on a VW
Das European Autohaus
 
PDF
EC360B LC EC360BLC Volvo Excavator Service Repair Manual.pdf
Service Repair Manual
 
PPTX
5 Why Training Presentation for Industry
aoberoi874
 
Referensi Materi pembelajaran dan pengayaan
alimarsikal
 
Top Tips to Prevent Tire Damage and Extend Tire Life
AutoNation Service Centre Vancouver
 
Voltage Intensifier Circuit (VIC) and Water Fuel Cell (WFC) Guide_ Simplified...
securesupplies
 
Meeting 1 Electrical Engineering Project Design-1.pptx
sitompulvictoryland2
 
Engine PTO (Power Take Off) Volvo EC360BLC Service Manual.pdf
Service Repair Manual
 
Water outlet Volvo EC360B LC Service Manual.pdf
Service Repair Manual
 
Top IT Products in Hyderabad – Powering Smarter Workplaces.pptx
Indoteq Office Automation Systems
 
定制学历UEX在读证明信西班牙埃斯特雷马杜拉大学毕业证明,UEX录取通知书
Taqyea
 
Daewoo Doosan 430 450 Plus Operator Manual.pdf
Service Repair Manual
 
lx465 new holland specs Service Repair Manual.pdf
Service Repair Manual
 
Volvo BM L150 Wheel Loader Service PDF Repair Manual.pdf
Service Repair Manual
 
Daewoo Doosan 430 440 450 Plus Operator Manual.pdf
Service Repair Manual
 
Fuel filter Volvo EC360BLC D10 Excavator Service Manual.pdf
Service Repair Manual
 
英国学位证(UON毕业证书)诺丁汉大学毕业证书如何办理
Taqyea
 
Engine oil filter Volvo EC360B LC Excavator Service Manual.pdf
Service Repair Manual
 
Advanced product quality planning_Presentation.pptx
qualitynewswan
 
Arctic Cat 700 H1 Efi 2008 Service Repair Manual.pdf
Service Repair Manual
 
What Is the Automatic Transmission Malfunction on a VW
Das European Autohaus
 
EC360B LC EC360BLC Volvo Excavator Service Repair Manual.pdf
Service Repair Manual
 
5 Why Training Presentation for Industry
aoberoi874
 
Ad

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).
  • 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.
  • 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.