SlideShare une entreprise Scribd logo
1  sur  12
Review!
● Insertion Sort
– Good for small data sets
● Used as the recursive base case for Rubinius' .sort
method
– Terrible big O
– Very minimal memory usage
– Stable
● Merge Sort
– Great all around application
● Extremely predictable
– A bit memory intensive
● O(n) extra memory
– Stable
Quick Sort
What is this infernal contraption?
● Space-optimized divide and conquer
recursive sorting of course!
● Most direct competitor to merge sort
● Instead of pulling apart the entire array
into bite size chunks, it measures the
entire thing against one value (the
pivot) and determines where it
permanently belongs, then sorts each
“half” on either side the same way
What is this infernal contraption?
● Not adaptive, officially
– Algorithm's performance acts the
same no matter what data is passed
to it, but luck on the pivot choice and
repeated values can affect runtime
● Not stable
– Keys of the same value can switch
places with each other
What is this infernal contraption?
● When compared to merge sort:
– Worse worst-case runtime
● Mergesort: always O(n log n)
● Quicksort: usually O(n log n) though in rare cases can be
O(n**2)
– Less extra memory required
● Mergesort: O(n) extra memory for auxiliary operations
● Quicksort
–Uses an in-place “pivot” for comparison
–After comparing to the pivot, each half is sorted
recursively, which requires at most O(log n) extra space
Show me the stats
● 10,000 elements, 1..10,000 already sorted
– 0.029761 seconds ~> Insertion sort
– 0.016278 seconds ~> Merge sort
– 0.021049 seconds ~> Quick sort
● 10,000 elements, 1..10,000 reverse sorted
– 2.324039 seconds ~> Insertion sort
– 0.019173 seconds ~> Merge sort
– 0.021986 seconds ~> Quick sort
● 10,000 elements, random values up to 10,000
– 1.259223 seconds ~> Insertion sort
– 0.027815 seconds ~> Merge sort
– 0.026452 seconds ~> Quick sort
● 20,000 elements, random values up to 20,000
– 4.691325 seconds ~> Insertion sort
– 0.055601 seconds ~> Merge sort
– 0.057677 seconds ~> Quick sort
● 30,000 elements, random values up to 30,000
– 10.571314 seconds ~> Insertion sort
– 0.086017 seconds ~> Merge sort
– 0.095844 seconds ~> Quick sort
● 5mil elements, random values up to 5mil
– 21.037956 seconds ~> Merge sort
– 20.530810 seconds ~> Quick sort
Built-in .sort still owns though
● 10,000 elements, 1..10,000 already sorted
– 0.000106 seconds vs 0.021049 seconds
● 10,000 elements, 1..10,000 reverse sorted
– 0.000112 seconds vs 0.021986 seconds
● 10,000 elements, random values up to 10,000
– 0.001657 seconds vs 0.026452 seconds
● 20,000 elements, random values up to 20,000
– 0.003481 seconds vs 0.057677 seconds
● 30,000 elements, random values up to 30,000
– 0.005254 seconds vs 0.095844 seconds
● 5mil elements, random values up to 5mil
– 1.288245 seconds vs 20.530810 seconds
Built-in .sort still owns though
● Which is funny because I lied accidentally...
– Although Rubinius uses Merge sort with Insertion sort to handle
base cases, MRI Ruby uses quick sort with various pointers
● What the hell, why is mine slower?
–Ruby's .sort is written in C and optimized by people who
know exactly what they're doing
–Since we wrote ours in actual Ruby, it has to deal with
integer (or string) objects instead of just values
● Remember? EVERYTHING in Ruby is an object
Built-in .sort still owns though
OH BOY! TIME TO IMPLEMENT
Bro, do you even logic?
● First method: “Quicksort” (three params: array, left
(default = 0), right (default array.size-1))
– If left < right
● pivot_index = find the median of the size of the array
● new_pivot_index = the result of calling secondary method
“partition”
● Recursively run “quicksort” method on the first half of the
array up to (but not including the “new_pivot_index”)
● Recursively run “quicksort” method on the second half of the
array from the “new_pivot_index” (but not including it) to the
end of the array
– Return the array
Bro, do you even logic? Pt. 2
● Second method: “Partition” (four params: array, left, right,
pivot_index)
– Find the pivot value using array[pivot_index]
– Switch the rightmost value of the array with the pivot_value
● (Parallel assignment)
– Create a store_index variable and set it equal to left
– From left to right exclusive do |n|
●
If the value at array[n] is less than pivot_value
– Switch array[n] with array[store_index]
– Increment store_index
– Switch rightmost value of array with store_index
– Return the final value of the store_index variable
● This value gets assigned to the “new_pivot_index” variable from the first
method call, so we can then finish the first call we made

Contenu connexe

Tendances

Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Chapter 14 quick_sort
Chapter 14 quick_sortChapter 14 quick_sort
Chapter 14 quick_sortTerry Yoast
 
Complexity of algorithms
Complexity of algorithmsComplexity of algorithms
Complexity of algorithmsJasur Ahmadov
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityMotaleb Hossen Manik
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sortKrish_ver2
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
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
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting AlgorithmsDamian T. Gordon
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationZakaria Hossain
 

Tendances (20)

Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Chapter 14 quick_sort
Chapter 14 quick_sortChapter 14 quick_sort
Chapter 14 quick_sort
 
Complexity of algorithms
Complexity of algorithmsComplexity of algorithms
Complexity of algorithms
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
 
Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
 
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
 
Quick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili SaghafiQuick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili Saghafi
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting Algorithms
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 

En vedette

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentationirdginfo
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - CorrectedAndres Mendez-Vazquez
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughYoshi Watanabe
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort AlgorithmTobias Straub
 
Heapsort
HeapsortHeapsort
Heapsortmmoylan
 

En vedette (20)

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
lecture 6
lecture 6lecture 6
lecture 6
 
Heaps
HeapsHeaps
Heaps
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
 
Quicksort
QuicksortQuicksort
Quicksort
 
Bubble sort algorithm
Bubble sort algorithmBubble sort algorithm
Bubble sort algorithm
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
 
Heapsort
HeapsortHeapsort
Heapsort
 

Similaire à Quick sort

Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortNicholas Case
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxMohammed472103
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharpMicheal Ogundero
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptxAnSHiKa187943
 
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
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsSiddhantShelake
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
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
 

Similaire à Quick sort (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion Sort
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
16 mergesort
16 mergesort16 mergesort
16 mergesort
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
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
 
Sorting
SortingSorting
Sorting
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithms
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
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
 
Complexity in array
Complexity in arrayComplexity in array
Complexity in array
 

Dernier

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
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
 
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
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
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
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
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
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
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
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 

Dernier (20)

POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
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
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
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
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
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...
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
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
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
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
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 

Quick sort

  • 1. Review! ● Insertion Sort – Good for small data sets ● Used as the recursive base case for Rubinius' .sort method – Terrible big O – Very minimal memory usage – Stable ● Merge Sort – Great all around application ● Extremely predictable – A bit memory intensive ● O(n) extra memory – Stable
  • 3. What is this infernal contraption? ● Space-optimized divide and conquer recursive sorting of course! ● Most direct competitor to merge sort ● Instead of pulling apart the entire array into bite size chunks, it measures the entire thing against one value (the pivot) and determines where it permanently belongs, then sorts each “half” on either side the same way
  • 4. What is this infernal contraption? ● Not adaptive, officially – Algorithm's performance acts the same no matter what data is passed to it, but luck on the pivot choice and repeated values can affect runtime ● Not stable – Keys of the same value can switch places with each other
  • 5. What is this infernal contraption? ● When compared to merge sort: – Worse worst-case runtime ● Mergesort: always O(n log n) ● Quicksort: usually O(n log n) though in rare cases can be O(n**2) – Less extra memory required ● Mergesort: O(n) extra memory for auxiliary operations ● Quicksort –Uses an in-place “pivot” for comparison –After comparing to the pivot, each half is sorted recursively, which requires at most O(log n) extra space
  • 6. Show me the stats ● 10,000 elements, 1..10,000 already sorted – 0.029761 seconds ~> Insertion sort – 0.016278 seconds ~> Merge sort – 0.021049 seconds ~> Quick sort ● 10,000 elements, 1..10,000 reverse sorted – 2.324039 seconds ~> Insertion sort – 0.019173 seconds ~> Merge sort – 0.021986 seconds ~> Quick sort ● 10,000 elements, random values up to 10,000 – 1.259223 seconds ~> Insertion sort – 0.027815 seconds ~> Merge sort – 0.026452 seconds ~> Quick sort ● 20,000 elements, random values up to 20,000 – 4.691325 seconds ~> Insertion sort – 0.055601 seconds ~> Merge sort – 0.057677 seconds ~> Quick sort ● 30,000 elements, random values up to 30,000 – 10.571314 seconds ~> Insertion sort – 0.086017 seconds ~> Merge sort – 0.095844 seconds ~> Quick sort ● 5mil elements, random values up to 5mil – 21.037956 seconds ~> Merge sort – 20.530810 seconds ~> Quick sort
  • 7. Built-in .sort still owns though ● 10,000 elements, 1..10,000 already sorted – 0.000106 seconds vs 0.021049 seconds ● 10,000 elements, 1..10,000 reverse sorted – 0.000112 seconds vs 0.021986 seconds ● 10,000 elements, random values up to 10,000 – 0.001657 seconds vs 0.026452 seconds ● 20,000 elements, random values up to 20,000 – 0.003481 seconds vs 0.057677 seconds ● 30,000 elements, random values up to 30,000 – 0.005254 seconds vs 0.095844 seconds ● 5mil elements, random values up to 5mil – 1.288245 seconds vs 20.530810 seconds
  • 8. Built-in .sort still owns though ● Which is funny because I lied accidentally... – Although Rubinius uses Merge sort with Insertion sort to handle base cases, MRI Ruby uses quick sort with various pointers ● What the hell, why is mine slower? –Ruby's .sort is written in C and optimized by people who know exactly what they're doing –Since we wrote ours in actual Ruby, it has to deal with integer (or string) objects instead of just values ● Remember? EVERYTHING in Ruby is an object
  • 9. Built-in .sort still owns though
  • 10. OH BOY! TIME TO IMPLEMENT
  • 11. Bro, do you even logic? ● First method: “Quicksort” (three params: array, left (default = 0), right (default array.size-1)) – If left < right ● pivot_index = find the median of the size of the array ● new_pivot_index = the result of calling secondary method “partition” ● Recursively run “quicksort” method on the first half of the array up to (but not including the “new_pivot_index”) ● Recursively run “quicksort” method on the second half of the array from the “new_pivot_index” (but not including it) to the end of the array – Return the array
  • 12. Bro, do you even logic? Pt. 2 ● Second method: “Partition” (four params: array, left, right, pivot_index) – Find the pivot value using array[pivot_index] – Switch the rightmost value of the array with the pivot_value ● (Parallel assignment) – Create a store_index variable and set it equal to left – From left to right exclusive do |n| ● If the value at array[n] is less than pivot_value – Switch array[n] with array[store_index] – Increment store_index – Switch rightmost value of array with store_index – Return the final value of the store_index variable ● This value gets assigned to the “new_pivot_index” variable from the first method call, so we can then finish the first call we made