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

More Related Content

What's hot

Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Abdelrahman Saleh
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Eleonora Ciceri
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal 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 algorithms
Jasur Ahmadov
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed 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 complexity
Motaleb Hossen Manik
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
Krish_ver2
 
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
University of Science and Technology Chitttagong
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
VARSHAKUMARI49
 
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
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
Professor Lili Saghafi
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting Algorithms
Damian 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 Animation
Zakaria Hossain
 

What's hot (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
 

Viewers also liked

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 
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
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
Quick sort
Quick sortQuick sort
Quick sort
fika sweety
 
Quick Sort
Quick SortQuick Sort
Quick Sort
SamZai VicToria
 
lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
Heaps
HeapsHeaps
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
Krish_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 - Corrected
Andres 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 through
Yoshi Watanabe
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Mohammed Hussein
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
Tobias Straub
 
Heapsort
HeapsortHeapsort
Heapsort
mmoylan
 

Viewers also liked (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
 

Similar to Quick sort

Merge sort
Merge sortMerge sort
Merge sort
Nicholas Case
 
Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion Sort
Nicholas Case
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
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 csharp
Micheal Ogundero
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
AnSHiKa187943
 
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
 
Sorting
SortingSorting
Sorting
FahadSaeed39
 
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
SiddhantShelake
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
sajinis3
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
PRIANKA R
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
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 analysis
Radhika Talaviya
 

Similar to 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
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
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
 

Recently uploaded

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
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
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
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
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
 
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
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 

Recently uploaded (20)

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
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
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
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
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...
 
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
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 

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