{"captions":[{"content":"TOMMY: Let's take a look at selection sort, an algorithm","startTime":6762,"duration":3218,"startOfParagraph":false},{"content":"for taking a list of numbers and sorting them.","startTime":9980,"duration":2820,"startOfParagraph":false},{"content":"An algorithm, remember, is simply a step-by-step","startTime":12800,"duration":2950,"startOfParagraph":false},{"content":"procedure for accomplishing a task.","startTime":15750,"duration":2620,"startOfParagraph":false},{"content":"The basic idea behind selection sort is to divide","startTime":18370,"duration":3100,"startOfParagraph":false},{"content":"our list into two portions--","startTime":21470,"duration":1920,"startOfParagraph":false},{"content":"a sorted portion and an unsorted portion.","startTime":23390,"duration":3420,"startOfParagraph":false},{"content":"At each step of the algorithm, a number is moved from the","startTime":26810,"duration":3390,"startOfParagraph":false},{"content":"unsorted portion to the sorted portion until eventually the","startTime":30200,"duration":3600,"startOfParagraph":false},{"content":"entire list is sorted.","startTime":33800,"duration":2080,"startOfParagraph":false},{"content":"So here's a list of six numbers--","startTime":35880,"duration":2630,"startOfParagraph":false},{"content":"23, 42, 4, 16, 8, and 15.","startTime":38510,"duration":5500,"startOfParagraph":false},{"content":"Right now the entire list is considered unsorted.","startTime":44010,"duration":3670,"startOfParagraph":false},{"content":"Even though a number like 16 may already be in its correct","startTime":47680,"duration":4090,"startOfParagraph":false},{"content":"location, our algorithm has no way of knowing that until the","startTime":51770,"duration":4270,"startOfParagraph":false},{"content":"entire list is sorted.","startTime":56040,"duration":1940,"startOfParagraph":false},{"content":"So we'll consider every number to be unsorted until we sort","startTime":57980,"duration":3375,"startOfParagraph":false},{"content":"it ourselves.","startTime":61355,"duration":2445,"startOfParagraph":false},{"content":"We know that we want the list to be in ascending order.","startTime":63800,"duration":3090,"startOfParagraph":false},{"content":"So we'll want to build up the sorted portion of our list","startTime":66890,"duration":3310,"startOfParagraph":false},{"content":"from left to right, smallest to largest.","startTime":70200,"duration":3080,"startOfParagraph":false},{"content":"To do that, we'll need to find the minimum unsorted element","startTime":73280,"duration":4690,"startOfParagraph":false},{"content":"and put it at the end of the sorted portion.","startTime":77970,"duration":3380,"startOfParagraph":false},{"content":"Since this list is not sorted, the only way to do that is to","startTime":81350,"duration":4020,"startOfParagraph":false},{"content":"look at each element in the unsorted portion, remembering","startTime":85370,"duration":3960,"startOfParagraph":false},{"content":"which element is the lowest and comparing","startTime":89330,"duration":2680,"startOfParagraph":false},{"content":"each element to that.","startTime":92010,"duration":1760,"startOfParagraph":false},{"content":"So we'll first look at the 23.","startTime":93770,"duration":2380,"startOfParagraph":false},{"content":"This is the first element we've seen, so we'll remember","startTime":96150,"duration":2500,"startOfParagraph":false},{"content":"it as the minimum.","startTime":98650,"duration":1400,"startOfParagraph":false},{"content":"Next we'll look at 42.","startTime":100050,"duration":2270,"startOfParagraph":false},{"content":"42 is larger than 23, so 23 is still the minimum.","startTime":102320,"duration":4400,"startOfParagraph":false},{"content":"Next is the 4 which is less than 23, so we'll remember 4","startTime":106720,"duration":4490,"startOfParagraph":false},{"content":"as the new minimum.","startTime":111210,"duration":1670,"startOfParagraph":false},{"content":"Next is 16 which is larger than 4, so 4","startTime":112880,"duration":3500,"startOfParagraph":false},{"content":"is still the minimum.","startTime":116380,"duration":1600,"startOfParagraph":false},{"content":"8 is larger than 4, and 15 is larger than 4, so 4 must be","startTime":117980,"duration":5690,"startOfParagraph":false},{"content":"the smallest unsorted element.","startTime":123670,"duration":2310,"startOfParagraph":false},{"content":"So even though as humans we can immediately see that 4 is","startTime":125980,"duration":3370,"startOfParagraph":false},{"content":"the minimum element, our algorithm needs to look at","startTime":129350,"duration":2950,"startOfParagraph":false},{"content":"every unsorted element, even after we've found the 4-- the","startTime":132300,"duration":3410,"startOfParagraph":false},{"content":"minimum element.","startTime":135710,"duration":1150,"startOfParagraph":false},{"content":"So now that we've found the minimum element, 4, we'll want","startTime":136860,"duration":3040,"startOfParagraph":false},{"content":"to move it into the sorted portion of the list.","startTime":139900,"duration":3510,"startOfParagraph":false},{"content":"Since this is the first step, this means we want to put 4 at","startTime":143410,"duration":3910,"startOfParagraph":false},{"content":"the beginning of the list.","startTime":147320,"duration":2360,"startOfParagraph":false},{"content":"Right now 23 is at the beginning of the list, so","startTime":149680,"duration":3360,"startOfParagraph":false},{"content":"let's swap the 4 and the 23.","startTime":153040,"duration":3040,"startOfParagraph":false},{"content":"So now our list looks like this.","startTime":156080,"duration":2790,"startOfParagraph":false},{"content":"We know that 4 must be in its final location, because it is","startTime":158870,"duration":3840,"startOfParagraph":false},{"content":"both the smallest element and the element at the beginning","startTime":162710,"duration":3180,"startOfParagraph":false},{"content":"of the list.","startTime":165890,"duration":1070,"startOfParagraph":false},{"content":"So this means that we don't ever need to move it again.","startTime":166960,"duration":3690,"startOfParagraph":false},{"content":"So let's repeat this process to add another element to the","startTime":170650,"duration":3260,"startOfParagraph":false},{"content":"sorted portion of the list.","startTime":173910,"duration":2000,"startOfParagraph":false},{"content":"We know that we don't need to look at the 4, because it's","startTime":175910,"duration":3040,"startOfParagraph":false},{"content":"already sorted.","startTime":178950,"duration":1050,"startOfParagraph":false},{"content":"So we can start at the 42, which we'll remember as the","startTime":180000,"duration":3540,"startOfParagraph":false},{"content":"minimum element.","startTime":183540,"duration":1750,"startOfParagraph":false},{"content":"So next we'll look at the 23 which is less than 42, so we","startTime":185290,"duration":3410,"startOfParagraph":false},{"content":"remember 23 is the new minimum.","startTime":188700,"duration":2920,"startOfParagraph":false},{"content":"Next we see the 16 which is less than 23, so","startTime":191620,"duration":3250,"startOfParagraph":false},{"content":"16 is the new minimum.","startTime":194870,"duration":1930,"startOfParagraph":false},{"content":"Now we look at the 8 which is less than 16, so","startTime":196800,"duration":2920,"startOfParagraph":false},{"content":"8 is the new minimum.","startTime":199720,"duration":1410,"startOfParagraph":false},{"content":"And finally 8 is less than 15, so we know that 8 is a minimum","startTime":201130,"duration":4770,"startOfParagraph":false},{"content":"unsorted element.","startTime":205900,"duration":1880,"startOfParagraph":false},{"content":"So that means we should append 8 to the sorted","startTime":207780,"duration":2880,"startOfParagraph":false},{"content":"portion of the list.","startTime":210660,"duration":1790,"startOfParagraph":false},{"content":"Right now 4 is the only sorted element, so we want to place","startTime":212450,"duration":3540,"startOfParagraph":false},{"content":"the 8 next to the 4.","startTime":215990,"duration":2420,"startOfParagraph":false},{"content":"Since 42 is the first element in the unsorted portion of the","startTime":218410,"duration":3510,"startOfParagraph":false},{"content":"list, we'll want to swap the 42 and the 8.","startTime":221920,"duration":5340,"startOfParagraph":false},{"content":"So now our list looks like this.","startTime":227260,"duration":2420,"startOfParagraph":false},{"content":"4 and 8 represent the sorted portion of the list, and the","startTime":229680,"duration":4150,"startOfParagraph":false},{"content":"remaining numbers represent the unsorted","startTime":233830,"duration":2610,"startOfParagraph":false},{"content":"portion of the list.","startTime":236440,"duration":1820,"startOfParagraph":false},{"content":"So let's continue with another iteration.","startTime":238260,"duration":2370,"startOfParagraph":false},{"content":"We start with 23 this time, since we don't need to look at","startTime":240630,"duration":3220,"startOfParagraph":false},{"content":"the 4 and the 8 anymore because they've","startTime":243850,"duration":1920,"startOfParagraph":false},{"content":"already been sorted.","startTime":245770,"duration":1890,"startOfParagraph":false},{"content":"16 is less than 23, so we'll remember","startTime":247660,"duration":2610,"startOfParagraph":false},{"content":"16 as the new minimum.","startTime":250270,"duration":1800,"startOfParagraph":false},{"content":"16 is less than 42, but 15 is less than 16, so 15 must be","startTime":252070,"duration":6079,"startOfParagraph":false},{"content":"the minimum unsorted element.","startTime":258149,"duration":2331,"startOfParagraph":false},{"content":"So now we want to swap the 15 and the 23 to","startTime":260480,"duration":4100,"startOfParagraph":false},{"content":"give us this list.","startTime":264580,"duration":1730,"startOfParagraph":false},{"content":"The sorted portion of the list consists of 4, 8 and 15, and","startTime":266310,"duration":4190,"startOfParagraph":false},{"content":"these elements are still unsorted.","startTime":270500,"duration":2710,"startOfParagraph":false},{"content":"But it just so happens that the next unsorted element, 16,","startTime":273210,"duration":3690,"startOfParagraph":false},{"content":"is already sorted.","startTime":276900,"duration":1580,"startOfParagraph":false},{"content":"However, there's no way for our algorithm to know that 16","startTime":278480,"duration":3580,"startOfParagraph":false},{"content":"is already in its correct location, so we still need to","startTime":282060,"duration":3170,"startOfParagraph":false},{"content":"repeat exactly the same process.","startTime":285230,"duration":2640,"startOfParagraph":false},{"content":"So we see that 16 is less than 42, and 16 is less than 23, so","startTime":287870,"duration":5880,"startOfParagraph":false},{"content":"16 must be the minimum element.","startTime":293750,"duration":2480,"startOfParagraph":false},{"content":"It's impossible to swap this element with itself, so we can","startTime":296230,"duration":2780,"startOfParagraph":false},{"content":"simply leave it in this location.","startTime":299010,"duration":2770,"startOfParagraph":false},{"content":"So we need one more pass of our algorithm.","startTime":301780,"duration":2880,"startOfParagraph":false},{"content":"42 is greater than 23, so 23 must be the","startTime":304660,"duration":4710,"startOfParagraph":false},{"content":"minimum unsorted element.","startTime":309370,"duration":1600,"startOfParagraph":false},{"content":"Once we swap the 23 and the 42, we end up with our final","startTime":310970,"duration":6440,"startOfParagraph":false},{"content":"sorted list--","startTime":317410,"duration":1120,"startOfParagraph":false},{"content":"4, 8, 15, 16, 23, 42.","startTime":318530,"duration":4860,"startOfParagraph":false},{"content":"We know 42 must be in the correct place since it's the","startTime":323390,"duration":3440,"startOfParagraph":false},{"content":"only element left, and that's selection sort.","startTime":326830,"duration":3380,"startOfParagraph":false},{"content":"Let's now formalize our algorithm with some","startTime":330210,"duration":1890,"startOfParagraph":false},{"content":"pseudocode.","startTime":332100,"duration":2440,"startOfParagraph":false},{"content":"On line one, we can see that we need to integrate over","startTime":334540,"duration":3220,"startOfParagraph":false},{"content":"every element of the list.","startTime":337760,"duration":1770,"startOfParagraph":false},{"content":"Except the last element, since the 1 element","startTime":339530,"duration":2620,"startOfParagraph":false},{"content":"list is already sorted.","startTime":342150,"duration":2080,"startOfParagraph":false},{"content":"On line two, we consider the first element of the unsorted","startTime":344230,"duration":3870,"startOfParagraph":false},{"content":"portion of the list to be the minimum, as we did with our","startTime":348100,"duration":2980,"startOfParagraph":false},{"content":"example, so we have something to compare to.","startTime":351080,"duration":2670,"startOfParagraph":false},{"content":"Line three begins a second loop in which we iterate over","startTime":353750,"duration":3510,"startOfParagraph":false},{"content":"each unsorted element.","startTime":357260,"duration":1910,"startOfParagraph":false},{"content":"We know that after i iterations, the sorted portion","startTime":359170,"duration":2980,"startOfParagraph":false},{"content":"of our list must have i elements in it since each step","startTime":362150,"duration":3180,"startOfParagraph":false},{"content":"sorts one element.","startTime":365330,"duration":1560,"startOfParagraph":false},{"content":"So the first unsorted element must be in position i plus 1.","startTime":366890,"duration":4880,"startOfParagraph":false},{"content":"On line four, we compare the current element to the minimum","startTime":371770,"duration":3670,"startOfParagraph":false},{"content":"element that we've seen so far.","startTime":375440,"duration":2310,"startOfParagraph":false},{"content":"If the current element is smaller than the minimum","startTime":377750,"duration":2810,"startOfParagraph":false},{"content":"element, then we remember the current element as the new","startTime":380560,"duration":3310,"startOfParagraph":false},{"content":"minimum on line five.","startTime":383870,"duration":2380,"startOfParagraph":false},{"content":"Finally, on lines six and seven, we swap the minimum","startTime":386250,"duration":3650,"startOfParagraph":false},{"content":"element with the first unsorted element, thereby","startTime":389900,"duration":3180,"startOfParagraph":false},{"content":"adding it to the sorted portion of the list.","startTime":393080,"duration":3910,"startOfParagraph":false},{"content":"Once we have an algorithm, an important question to ask","startTime":396990,"duration":3040,"startOfParagraph":false},{"content":"ourselves as programmers is how long did that take?","startTime":400030,"duration":3340,"startOfParagraph":false},{"content":"We'll first ask the question how long does it take for this","startTime":403370,"duration":3600,"startOfParagraph":false},{"content":"algorithm to run in the worst case?","startTime":406970,"duration":3100,"startOfParagraph":false},{"content":"Recall we represent this running","startTime":410070,"duration":1570,"startOfParagraph":false},{"content":"time with big O notation.","startTime":411640,"duration":3420,"startOfParagraph":false},{"content":"In order to determine the minimum unsorted element, we","startTime":415060,"duration":3590,"startOfParagraph":false},{"content":"essentially had to compare each element in the list to","startTime":418650,"duration":3230,"startOfParagraph":false},{"content":"every other element in the list.","startTime":421880,"duration":2160,"startOfParagraph":false},{"content":"Intuitively, this sounds like an O of n squared operation.","startTime":424040,"duration":4390,"startOfParagraph":false},{"content":"Looking at our pseudocode, we also have a loop nested inside","startTime":428430,"duration":3620,"startOfParagraph":false},{"content":"another loop, which indeed sounds like an O","startTime":432050,"duration":2370,"startOfParagraph":false},{"content":"of n squared operation.","startTime":434420,"duration":2060,"startOfParagraph":false},{"content":"However, remember that we didn't need to look over the","startTime":436480,"duration":2770,"startOfParagraph":false},{"content":"entire list when determining the minimum unsorted element?","startTime":439250,"duration":4210,"startOfParagraph":false},{"content":"Once we knew that the 4 was sorted, for example, we didn't","startTime":443460,"duration":3140,"startOfParagraph":false},{"content":"need to look at it again.","startTime":446600,"duration":1570,"startOfParagraph":false},{"content":"So does this lower the running time?","startTime":448170,"duration":2850,"startOfParagraph":false},{"content":"For our list of length 6, we needed to make five","startTime":451020,"duration":3490,"startOfParagraph":false},{"content":"comparisons for the first element, four comparisons for","startTime":454510,"duration":3480,"startOfParagraph":false},{"content":"the second element, and so on.","startTime":457990,"duration":2760,"startOfParagraph":false},{"content":"That means that the total number of steps is the sum of","startTime":460750,"duration":3940,"startOfParagraph":false},{"content":"the integers from 1 to the length of the list minus 1.","startTime":464690,"duration":4470,"startOfParagraph":false},{"content":"We can represent this with a summation.","startTime":469160,"duration":1845,"startOfParagraph":false},{"content":"We won't go into summations here.","startTime":477980,"duration":1930,"startOfParagraph":false},{"content":"But it turns out that this summation is equal to n times","startTime":479910,"duration":4990,"startOfParagraph":false},{"content":"n minus 1 over 2.","startTime":484900,"duration":2640,"startOfParagraph":false},{"content":"Or equivalently, n squared over 2 minus n over 2.","startTime":487540,"duration":6680,"startOfParagraph":false},{"content":"When talking about asymptotic runtime, this n squared term","startTime":494220,"duration":4640,"startOfParagraph":false},{"content":"is going to dominate this n term.","startTime":498860,"duration":3210,"startOfParagraph":false},{"content":"So selection sort is O of n squared.","startTime":502070,"duration":5780,"startOfParagraph":false},{"content":"Recall that in our example, selection sort still needed to","startTime":507850,"duration":3610,"startOfParagraph":false},{"content":"check if a number that was already sorted","startTime":511460,"duration":2390,"startOfParagraph":false},{"content":"needed to be moved.","startTime":513850,"duration":1600,"startOfParagraph":false},{"content":"So that means that if we ran selection sort over an already","startTime":515450,"duration":3479,"startOfParagraph":false},{"content":"sorted list, it would require the same number of steps as it","startTime":518929,"duration":4141,"startOfParagraph":false},{"content":"would when running over a completely unsorted list.","startTime":523070,"duration":3270,"startOfParagraph":false},{"content":"So selection sort has a best case performance of n squared,","startTime":526340,"duration":5130,"startOfParagraph":false},{"content":"which we represent with omega n squared.","startTime":531470,"duration":5350,"startOfParagraph":false},{"content":"And that's it for selection sort.","startTime":536820,"duration":1780,"startOfParagraph":false},{"content":"Just one of the many algorithms we can","startTime":538600,"duration":2030,"startOfParagraph":false},{"content":"use to sort a list.","startTime":540630,"duration":1760,"startOfParagraph":false},{"content":"My name is Tommy, and this is cs50.","startTime":542390,"duration":3520,"startOfParagraph":false}]}