1 00:00:00,000 --> 00:00:09,450 Welcome back. 2 00:00:09,450 --> 00:00:11,100 In today's lesson, 3 00:00:11,100 --> 00:00:15,480 you're going to learn a powerful new command for sorting. 4 00:00:15,480 --> 00:00:20,535 Sorting is a big deal in the computer science curriculum. 5 00:00:20,535 --> 00:00:22,560 In a typical computer science curriculum, 6 00:00:22,560 --> 00:00:24,840 you would learn several different sorting algorithms and 7 00:00:24,840 --> 00:00:27,915 you'd analyze properties of those algorithms. 8 00:00:27,915 --> 00:00:33,085 In my first programming course back as an undergraduate, 9 00:00:33,085 --> 00:00:38,540 I had to implement something called merge sort where you take the items, 10 00:00:38,540 --> 00:00:41,300 you keep chopping them in half until you get to very 11 00:00:41,300 --> 00:00:44,420 smallest which are already sorted and it turns out 12 00:00:44,420 --> 00:00:48,950 that taking two already sorted lists and merging them together 13 00:00:48,950 --> 00:00:53,870 into an even bigger sorted list is something that you can do pretty easily. 14 00:00:53,870 --> 00:00:55,400 It goes pretty fast, 15 00:00:55,400 --> 00:01:00,110 and so we build up from these small sorted list until we have the whole thing sorted. 16 00:01:00,110 --> 00:01:03,050 I spent days and days in the computer lab. 17 00:01:03,050 --> 00:01:06,040 It was before we had personal computers. 18 00:01:06,040 --> 00:01:09,460 So, I had to go to the computing lab and every time I hit run, 19 00:01:09,460 --> 00:01:15,080 I had to wait for the mainframe to run the program and print out 20 00:01:15,080 --> 00:01:21,300 a stack of papers telling me how the program would run. 21 00:01:21,300 --> 00:01:23,940 After many hours over several days, 22 00:01:23,940 --> 00:01:27,620 I proudly took a stack back, 23 00:01:27,620 --> 00:01:29,210 back home and showed all my roommates, 24 00:01:29,210 --> 00:01:31,385 "Yeah, I finally did it." 25 00:01:31,385 --> 00:01:34,720 It won't be so hard for you. 26 00:01:34,720 --> 00:01:40,400 We are not going to look at details of sorting algorithms. 27 00:01:40,400 --> 00:01:43,295 We're just going to use a built-in Python function. 28 00:01:43,295 --> 00:01:47,780 But we do want you to have a little mental model of what happens inside of 29 00:01:47,780 --> 00:01:51,830 a sorting algorithm because it's going to help you to figure out how to invoke it well. 30 00:01:51,830 --> 00:01:54,950 We've given you these great videos from [inaudible] into university 31 00:01:54,950 --> 00:01:59,375 illustrating sorting algorithms using Hungarian folk dances. 32 00:01:59,375 --> 00:02:04,055 You'll watch a couple alternative Hungarian dances showing different sorting algorithms. 33 00:02:04,055 --> 00:02:06,470 Don't worry about the details of the algorithms, 34 00:02:06,470 --> 00:02:09,350 but do notice there's something that they have in common. 35 00:02:09,350 --> 00:02:11,180 They always involve a bunch of 36 00:02:11,180 --> 00:02:15,750 pairwise comparisons which are interactions between a pair of dancers, 37 00:02:15,750 --> 00:02:19,595 two dancers will look at each other or each wearing a number, 38 00:02:19,595 --> 00:02:21,380 and they look at their numbers, 39 00:02:21,380 --> 00:02:24,350 and they do dancing and the one with the higher number 40 00:02:24,350 --> 00:02:27,725 always ends up on the right at the end of that interaction. 41 00:02:27,725 --> 00:02:31,085 After they've done a whole bunch of these pairwise comparisons, 42 00:02:31,085 --> 00:02:33,920 the dancers are in order by their numbers. 43 00:02:33,920 --> 00:02:37,540 So again, the particular sorting algorithm is not our focus here. 44 00:02:37,540 --> 00:02:41,630 We're just going to call a function that does the 45 00:02:41,630 --> 00:02:46,685 sorting for us and whatever sequence we give it comes back sorted in the order we wanted. 46 00:02:46,685 --> 00:02:48,740 At the end of this lesson, 47 00:02:48,740 --> 00:02:52,970 you will be able to invoke the sorted function to sort any sequence. 48 00:02:52,970 --> 00:02:55,670 You'll be able to specify either low to 49 00:02:55,670 --> 00:02:59,405 high order or high to low using the reverse parameter. 50 00:02:59,405 --> 00:03:04,160 You'll be able to specify a property to sort by using a key function. 51 00:03:04,160 --> 00:03:05,960 And in a later lesson, 52 00:03:05,960 --> 00:03:08,420 you'll learn some more advanced sorting things. 53 00:03:08,420 --> 00:03:11,490 So, we'll see you at the end.