SlideShare a Scribd company logo
1 of 16
What do sorts do?
● Take this: [6, 24, 10, 76, 35, 0, 37]
– Make it into this: [0, 6, 10, 24, 35, 37, 76]
● Or this: [“dolphin”, “yak”, “caribou”]
– Into: [“caribou”, “dolphin”, “yak”]
● How do you sort?
Which sort should you use?
● Factors
– No sort is good at everything, there will always be
tradeoffs depending on the qualities of your data
and your machine's circumstances
● http://www.sorting-algorithms.com
– Helpful overview of the different sorts, their
runtimes, properties, and best applications for each
of the major sorting methods
Which sort should you use?
● Memory usage
– Some sorts are “in place” and do not require much
more memory to run
– Other sorts use space liberally in recursion to make an
easier workload for the computer
– Pick two of the three:
● Fast runtime
● Low memory overhead
● Ease of coding/implementation
Which sort should you use?
● Stability
– Unstable sorts may change the relative order of keys of
the same value
– Stable sorts do not change the relative order of keys of
the same value
– So...?
● Consider this scenario
– You sort a list of flights by departure time
– You then sort that list by destination
– If the second sort was stable, you would result with an array of flights
sorted by destination ordered by increasing departure times
Which sort should you use?
● Adaptive or not?
– Efficiency changes depending on presortedness
(yes that's a word now) of the input array
– Types of presortedness to consider
● Pure random
● Nearly sorted
● Reverse sorted
● Few unique keys
– Many of the same values
The Unicorn Sort
● O(1) worst case runtime
● In place
● Stable
● Not adaptive
● Doesn't exist
Insertion Sort
The littlest sort that could... kinda
● Simplest sort technique
– No tricky recursion
– Only one operation to consider
– When mentally sorting, humans
naturally tend to use some sort of
insertion-like method
The littlest sort that could... kinda
● Adaptive
– Efficient for small data sets
– Data sets that are already close to
being sorted create the best case
scenario and quickest runtime
– Reverse order, on the other hand, is
bad news bears
The littlest sort that could... kinda
● Stable
– Does not change the relative order of
keys
● In-Place
– No extra memory usage from
recursion
– “Low overhead”
The littlest sort that could... kinda
● Summary!
– Simple to implement
– Efficient at small data sets
● Often used as the recursive base
case for other sorts
– Stable
– No (well, barely any) extra memory
usage
The littlest sort that could... kinda
● Runtime!
– Best case ~> already sorted array
● O(n) comparisons, O(1) swaps
– Worst case ~> reverse sorted array
● O(n**2) comparisons, O(n**2) swaps
● Literally swapping every element in the array
– Average ~> O(n**2) comparisons and swaps
● BAD for large data sets
● Space!
– O(n) total, O(1) for auxiliary operations
Show me the stats
● require 'benchmark'
– puts Benchmark.measure { block }
● Insertion sort, are you good at sorting?
– 10,000 elements, 1..10,000 already sorted
● 0.029761 seconds
– 10,000 elements, 1..10,000 reverse sorted
● 2.324039 seconds
– 10,000 elements, random values up to 10,000
● 1.259223 seconds
– 20,000 elements, random values up to 20,000
● 4.691325 seconds
– 30,000 elements, random values up to 30,000
● 10.571314 seconds
OH BOY! TIME TO IMPLEMENT
Bro, do you even logic?
● For each n in a range of the numbers 1..(array.size-1)
– Pull position n out of the array destructively
● Create value_to_insert variable ~> (array[n])
● Create insertion_index variable ~> (n)
– While the value of n > 0 and the value_to_insert is smaller than
the value at the position before insertion_index in the array
● Looking at the value before array[n]...
– Is it bigger than array[n]?
● insertion_index should be decremented
● “while” code runs again
– Is it smaller than array[n]?
● The “while” loop is broken
– value_to_insert should be inserted at insertion_index
● Each loop finished; return the array
Need a copy of this?
● http://www.slideshare.net/nicholascase520/intro
-to-sorting-insertion-sort

More Related Content

What's hot

01 intro to algorithm--updated 2015
01 intro to algorithm--updated 201501 intro to algorithm--updated 2015
01 intro to algorithm--updated 2015Hira Gul
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsVivek Bhargav
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithmssangeetha s
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2Kumar
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructureKumar
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis Shaista Qadir
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
Parallel sorting Algorithms
Parallel  sorting AlgorithmsParallel  sorting Algorithms
Parallel sorting AlgorithmsGARIMA SHAKYA
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsMohamed Loey
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 

What's hot (20)

01 intro to algorithm--updated 2015
01 intro to algorithm--updated 201501 intro to algorithm--updated 2015
01 intro to algorithm--updated 2015
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithms
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Aa sort-v4
Aa sort-v4Aa sort-v4
Aa sort-v4
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
chapter 1
chapter 1chapter 1
chapter 1
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructure
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Parallel sorting Algorithms
Parallel  sorting AlgorithmsParallel  sorting Algorithms
Parallel sorting Algorithms
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Parallel quicksort cz. 1
Parallel quicksort cz. 1Parallel quicksort cz. 1
Parallel quicksort cz. 1
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 

Viewers also liked

Insertion sort
Insertion sortInsertion sort
Insertion sortaditya raj
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Insertion Sort Demo
Insertion Sort DemoInsertion Sort Demo
Insertion Sort Demorentjen
 
Data Structure Insertion sort
Data Structure Insertion sort Data Structure Insertion sort
Data Structure Insertion sort Mahesh Dheravath
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sortKrish_ver2
 
Praktikum 05 Sistem Basis Data
Praktikum 05 Sistem Basis DataPraktikum 05 Sistem Basis Data
Praktikum 05 Sistem Basis DataAditya Nugroho
 
A Cost-Effective and Scalable Merge Sort Tree on FPGAs
A Cost-Effective and Scalable Merge Sort Tree on FPGAsA Cost-Effective and Scalable Merge Sort Tree on FPGAs
A Cost-Effective and Scalable Merge Sort Tree on FPGAsTakuma Usui
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort_fahad_shaikh
 
Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sortsmita gupta
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 
Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Jea Hyeun Jung
 
Merge sort
Merge sortMerge sort
Merge sortKumar
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort AlgorithmGail Carmichael
 

Viewers also liked (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Insertion Sort Demo
Insertion Sort DemoInsertion Sort Demo
Insertion Sort Demo
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Data Structure Insertion sort
Data Structure Insertion sort Data Structure Insertion sort
Data Structure Insertion sort
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Praktikum 05 Sistem Basis Data
Praktikum 05 Sistem Basis DataPraktikum 05 Sistem Basis Data
Praktikum 05 Sistem Basis Data
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
A Cost-Effective and Scalable Merge Sort Tree on FPGAs
A Cost-Effective and Scalable Merge Sort Tree on FPGAsA Cost-Effective and Scalable Merge Sort Tree on FPGAs
A Cost-Effective and Scalable Merge Sort Tree on FPGAs
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)
 
Merge sort
Merge sortMerge sort
Merge sort
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 

Similar to Intro to Sorting + Insertion Sort

Benchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbersBenchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbersJustin Dorfman
 
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity  what s behind the numbers...Benchmarks, performance, scalability, and capacity  what s behind the numbers...
Benchmarks, performance, scalability, and capacity what s behind the numbers...james tong
 
DAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithmsDAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithmsDEVARSHHIRENBHAIPARM
 
Software Engineering Advice from Google's Jeff Dean for Big, Distributed Systems
Software Engineering Advice from Google's Jeff Dean for Big, Distributed SystemsSoftware Engineering Advice from Google's Jeff Dean for Big, Distributed Systems
Software Engineering Advice from Google's Jeff Dean for Big, Distributed Systemsadrianionel
 
Solving Large Scale Optimization Problems using CPLEX Optimization Studio
Solving Large Scale Optimization Problems using CPLEX Optimization StudioSolving Large Scale Optimization Problems using CPLEX Optimization Studio
Solving Large Scale Optimization Problems using CPLEX Optimization Studiooptimizatiodirectdirect
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 
Teaching PostgreSQL to new people
Teaching PostgreSQL to new peopleTeaching PostgreSQL to new people
Teaching PostgreSQL to new peopleTomek Borek
 
Cluster Dynamics in Solr Autoscaling - Andrzej Białecki, Lucidworks
Cluster Dynamics in Solr Autoscaling - Andrzej Białecki, LucidworksCluster Dynamics in Solr Autoscaling - Andrzej Białecki, Lucidworks
Cluster Dynamics in Solr Autoscaling - Andrzej Białecki, LucidworksLucidworks
 
Accelerating Data Processing in Spark SQL with Pandas UDFs
Accelerating Data Processing in Spark SQL with Pandas UDFsAccelerating Data Processing in Spark SQL with Pandas UDFs
Accelerating Data Processing in Spark SQL with Pandas UDFsDatabricks
 
Deep Learning Tutorial
Deep Learning Tutorial Deep Learning Tutorial
Deep Learning Tutorial Ligeng Zhu
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkSergey Petrunya
 
Distributed systems and consistency
Distributed systems and consistencyDistributed systems and consistency
Distributed systems and consistencyseldo
 
Redis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HARedis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HADave Nielsen
 

Similar to Intro to Sorting + Insertion Sort (20)

Quick sort
Quick sortQuick sort
Quick sort
 
Benchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbersBenchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbers
 
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity  what s behind the numbers...Benchmarks, performance, scalability, and capacity  what s behind the numbers...
Benchmarks, performance, scalability, and capacity what s behind the numbers...
 
DAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithmsDAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithms
 
Software Engineering Advice from Google's Jeff Dean for Big, Distributed Systems
Software Engineering Advice from Google's Jeff Dean for Big, Distributed SystemsSoftware Engineering Advice from Google's Jeff Dean for Big, Distributed Systems
Software Engineering Advice from Google's Jeff Dean for Big, Distributed Systems
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
CPP12 - Algorithms
CPP12 - AlgorithmsCPP12 - Algorithms
CPP12 - Algorithms
 
Solving Large Scale Optimization Problems using CPLEX Optimization Studio
Solving Large Scale Optimization Problems using CPLEX Optimization StudioSolving Large Scale Optimization Problems using CPLEX Optimization Studio
Solving Large Scale Optimization Problems using CPLEX Optimization Studio
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Teaching PostgreSQL to new people
Teaching PostgreSQL to new peopleTeaching PostgreSQL to new people
Teaching PostgreSQL to new people
 
No stress with state
No stress with stateNo stress with state
No stress with state
 
Cluster Dynamics in Solr Autoscaling - Andrzej Białecki, Lucidworks
Cluster Dynamics in Solr Autoscaling - Andrzej Białecki, LucidworksCluster Dynamics in Solr Autoscaling - Andrzej Białecki, Lucidworks
Cluster Dynamics in Solr Autoscaling - Andrzej Białecki, Lucidworks
 
Accelerating Data Processing in Spark SQL with Pandas UDFs
Accelerating Data Processing in Spark SQL with Pandas UDFsAccelerating Data Processing in Spark SQL with Pandas UDFs
Accelerating Data Processing in Spark SQL with Pandas UDFs
 
Deep Learning Tutorial
Deep Learning Tutorial Deep Learning Tutorial
Deep Learning Tutorial
 
Task and Data Parallelism
Task and Data ParallelismTask and Data Parallelism
Task and Data Parallelism
 
Dconf2015 d2 t4
Dconf2015 d2 t4Dconf2015 d2 t4
Dconf2015 d2 t4
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmark
 
Distributed systems and consistency
Distributed systems and consistencyDistributed systems and consistency
Distributed systems and consistency
 
Connected Components Labeling
Connected Components LabelingConnected Components Labeling
Connected Components Labeling
 
Redis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HARedis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HA
 

Recently uploaded

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 

Recently uploaded (20)

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 

Intro to Sorting + Insertion Sort

  • 1. What do sorts do? ● Take this: [6, 24, 10, 76, 35, 0, 37] – Make it into this: [0, 6, 10, 24, 35, 37, 76] ● Or this: [“dolphin”, “yak”, “caribou”] – Into: [“caribou”, “dolphin”, “yak”] ● How do you sort?
  • 2. Which sort should you use? ● Factors – No sort is good at everything, there will always be tradeoffs depending on the qualities of your data and your machine's circumstances ● http://www.sorting-algorithms.com – Helpful overview of the different sorts, their runtimes, properties, and best applications for each of the major sorting methods
  • 3. Which sort should you use? ● Memory usage – Some sorts are “in place” and do not require much more memory to run – Other sorts use space liberally in recursion to make an easier workload for the computer – Pick two of the three: ● Fast runtime ● Low memory overhead ● Ease of coding/implementation
  • 4. Which sort should you use? ● Stability – Unstable sorts may change the relative order of keys of the same value – Stable sorts do not change the relative order of keys of the same value – So...? ● Consider this scenario – You sort a list of flights by departure time – You then sort that list by destination – If the second sort was stable, you would result with an array of flights sorted by destination ordered by increasing departure times
  • 5. Which sort should you use? ● Adaptive or not? – Efficiency changes depending on presortedness (yes that's a word now) of the input array – Types of presortedness to consider ● Pure random ● Nearly sorted ● Reverse sorted ● Few unique keys – Many of the same values
  • 6. The Unicorn Sort ● O(1) worst case runtime ● In place ● Stable ● Not adaptive ● Doesn't exist
  • 8. The littlest sort that could... kinda ● Simplest sort technique – No tricky recursion – Only one operation to consider – When mentally sorting, humans naturally tend to use some sort of insertion-like method
  • 9. The littlest sort that could... kinda ● Adaptive – Efficient for small data sets – Data sets that are already close to being sorted create the best case scenario and quickest runtime – Reverse order, on the other hand, is bad news bears
  • 10. The littlest sort that could... kinda ● Stable – Does not change the relative order of keys ● In-Place – No extra memory usage from recursion – “Low overhead”
  • 11. The littlest sort that could... kinda ● Summary! – Simple to implement – Efficient at small data sets ● Often used as the recursive base case for other sorts – Stable – No (well, barely any) extra memory usage
  • 12. The littlest sort that could... kinda ● Runtime! – Best case ~> already sorted array ● O(n) comparisons, O(1) swaps – Worst case ~> reverse sorted array ● O(n**2) comparisons, O(n**2) swaps ● Literally swapping every element in the array – Average ~> O(n**2) comparisons and swaps ● BAD for large data sets ● Space! – O(n) total, O(1) for auxiliary operations
  • 13. Show me the stats ● require 'benchmark' – puts Benchmark.measure { block } ● Insertion sort, are you good at sorting? – 10,000 elements, 1..10,000 already sorted ● 0.029761 seconds – 10,000 elements, 1..10,000 reverse sorted ● 2.324039 seconds – 10,000 elements, random values up to 10,000 ● 1.259223 seconds – 20,000 elements, random values up to 20,000 ● 4.691325 seconds – 30,000 elements, random values up to 30,000 ● 10.571314 seconds
  • 14. OH BOY! TIME TO IMPLEMENT
  • 15. Bro, do you even logic? ● For each n in a range of the numbers 1..(array.size-1) – Pull position n out of the array destructively ● Create value_to_insert variable ~> (array[n]) ● Create insertion_index variable ~> (n) – While the value of n > 0 and the value_to_insert is smaller than the value at the position before insertion_index in the array ● Looking at the value before array[n]... – Is it bigger than array[n]? ● insertion_index should be decremented ● “while” code runs again – Is it smaller than array[n]? ● The “while” loop is broken – value_to_insert should be inserted at insertion_index ● Each loop finished; return the array
  • 16. Need a copy of this? ● http://www.slideshare.net/nicholascase520/intro -to-sorting-insertion-sort