Upcoming SlideShare
×

# Intro to Sorting + Insertion Sort

387 views
308 views

Published on

Introduction to sorting algorithms with a broad overview (in Ruby pseudocode) of each sort's main features, beginning with Insertion Sort. *gifs do not show properly

Published in: Engineering, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
387
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
12
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Intro to Sorting + Insertion Sort

1. 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. 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. 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. 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. 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. 6. The Unicorn Sort ● O(1) worst case runtime ● In place ● Stable ● Not adaptive ● Doesn't exist
7. 7. Insertion Sort
8. 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. 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. 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. 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. 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. 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. 14. OH BOY! TIME TO IMPLEMENT
15. 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. 16. Need a copy of this? ● http://www.slideshare.net/nicholascase520/intro -to-sorting-insertion-sort