CS 106B, Lecture 25 Sorting

Total Page:16

File Type:pdf, Size:1020Kb

CS 106B, Lecture 25 Sorting CS 106B, Lecture 25 Sorting This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved. This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Marty Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others. Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others Plan for Today • Analyze several algorithms to do the same task: sorting – Big-Oh in the real world 2 Sorting • sorting: Rearranging the values in a collection into a specific order. – can be solved in many ways: Algorithm Description bogo sort shuffle and pray bubble sort swap adjacent pairs that are out of order selection sort look for the smallest element, move to front insertion sort build an increasingly large sorted front portion merge sort recursively divide the data in half and sort it heap sort place the values into a binary heap then dequeue quick sort recursively "partition" data based on a pivot value bucket sort cluster elements into smaller groups, sort the groups radix sort sort integers by last digit, then 2nd to last, then ... 3 Bogo sort • bogo sort: Orders a list of values by repetitively shuffling them and checking if they are sorted. – name comes from the word "bogus"; a.k.a. "bogus sort" The algorithm: – Scan the list, seeing if it is sorted. If so, stop. – Else, shuffle the values in the list and repeat. • This sorting algorithm (obviously) has terrible performance! – What is its runtime? 4 Bogo sort code // Places the elements of v into sorted order. void bogoSort(Vector<int>& v) { while (!isSorted(v)) { shuffle(v); // from shuffle.h } } // Returns true if v's elements are in sorted order. bool isSorted(Vector<int>& v) { for (int i = 0; i < v.size() - 1; i++) { if (v[i] > v[i + 1]) { return false; } } return true; } 5 Bogo sort runtime • How long should we expect bogo sort to take? – related to probability of shuffling into sorted order – assuming shuffling code is fair, probability equals 1 / (number of permutations of N elements) = 1/N! – average case performance: O(N * N!) – worst case performance: O(∞) – What is the best case performance? 6 Selection sort example • selection sort: Repeatedly swap smallest unplaced value to front. index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 22 18 12 -4 27 30 36 50 7 68 91 56 2 85 42 98 25 • After 1st, 2nd, and 3rd passes: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 18 12 22 27 30 36 50 7 68 91 56 2 85 42 98 25 index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 12 22 27 30 36 50 7 68 91 56 18 85 42 98 25 index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 22 27 30 36 50 12 68 91 56 18 85 42 98 25 7 Selection sort code // Rearranges elements of v into sorted order. void selectionSort(Vector<int>& v) { for (int i = 0; i < v.size() - 1; i++) { // find index of smallest remaining value int min = i; for (int j = i + 1; j < v.size(); j++) { if (v[j] < v[min]) { min = j; } } // swap smallest value to proper place, v[i] if (i != min) { int temp = v[i]; v[i] = v[min]; v[min] = temp; } } } 8 Insertion sort • insertion sort: orders a list of values by repetitively inserting a particular value into a sorted subset of the list • more specifically: – consider the first item to be a sorted sublist of length 1 – insert second item into sorted sublist, shifting first item if needed – insert third item into sorted sublist, shifting items 1-2 as needed – ... – repeat until all values have been inserted into their proper positions – How people line up when they have different arrival times! • Runtime: O(N2). – Generally somewhat faster than selection sort for most inputs. 9 Insertion sort example – Makes N-1 passes over the array. – At the end of pass i, the elements that occupied A[0]…A[i] originally are still in those spots and in sorted order. index 0 1 2 3 4 5 6 7 value 15 2 8 1 17 10 12 5 pass 1 2 15 8 1 17 10 12 5 pass 2 2 8 15 1 17 10 12 5 pass 3 1 2 8 15 17 10 12 5 pass 4 1 2 8 15 17 10 12 5 pass 5 1 2 8 10 15 17 12 5 pass 6 1 2 8 10 12 15 17 5 pass 7 1 2 5 8 10 12 15 17 10 Insertion sort code // Rearranges the elements of v into sorted order. void insertionSort(Vector<int>& v) { for (int i = 1; i < v.size(); i++) { int temp = v[i]; // slide elements right to make room for v[i] int j = i; while (j >= 1 && v[j - 1] > temp) { v[j] = v[j - 1]; j--; } v[j] = temp; } } 11 Bucket/radix sort • bucket sort: arrange items into buckets or bins repeatedly • radix sort: sort integers by 1s, then 10s, then 100s, ... – O(N) when used with data in a known fixed range (!) 12 Announcements • MiniBrowser is due today, Calligraphy will be released later today – Multiple parts, please start early (2nd and 3rd parts are harder than the 1st part) • Final is a week from Saturday, at 8:30AM – Practice exam will be released in the next few days • Please give us feedback! cs198.stanford.edu • Course feedback: – A note on LaIR/Piazza 13 Merge sort • merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: – Divide the list into two roughly equal halves. – Sort the left half. – Sort the right half. – Merge the two sorted halves into one sorted list. – Often implemented recursively. – An example of a "divide and conquer" algorithm. • Invented by John von Neumann in 1945 – Runtime: O(N log N). Somewhat faster for asc/descending input. 14 Merge sort example index 0 1 2 3 4 5 6 7 value 22 18 12 -4 58 7 31 42 split 22 18 12 -4 58 7 31 42 split split 22 18 12 -4 58 7 31 42 split split split split 22 18 12 -4 58 7 31 42 merge merge merge merge 18 22 -4 12 7 58 31 42 merge merge -4 12 18 22 7 31 42 58 merge -4 7 12 18 22 31 42 58 15 Merging sorted halves 16 Merge sort code // Rearranges the elements of v into sorted order using // the merge sort algorithm. void mergeSort(Vector<int>& v) { if (v.size() >= 2) { // split vector into two halves Vector<int> left = v.subList(0, v.size() / 2); Vector<int> right = v.subList(v.size() / 2 + 1, (v.size() - 1) / 2); // recursively sort the two halves mergeSort(left); mergeSort(right); // merge the sorted halves into a sorted whole v.clear(); merge(v, left, right); } } 17 Merge halves code // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted void merge(Vector<int>& result, Vector<int>& left, Vector<int>& right) { int leftIndex = 0; int rightIndex = 0; for (int i = 0; i < left.size() + right.size(); i++) { if (rightIndex >= right.size() || (leftIndex < left.size() && left[leftIndex] <= right[rightIndex])) { result += left[leftIndex]; // take from left leftIndex++; } else { result += right[rightIndex]; // take from right rightIndex++; } } } 18 Runtime intuition • Merge sort performs O(N) operations on each level. (width) – Each level splits the data in 2, so there are log2 N levels. (height) – Product of these = N * log2 N = O(N log N). (area) – Example: N = 32. Performs ~ log2 32 = 5 levels of N operations each: 32 16 N 2 8 4 height = log 2 1 width = N 19 Quick sort • quick sort: Orders a list of values by partitioning the list around one element called a pivot, then sorting each partition. – invented by British computer scientist C.A.R. Hoare in 1960 • Quick sort is another divide and conquer algorithm: – Choose one element in the list to be the pivot. – Divide the elements so that all elements less than the pivot are to its left and all greater (or equal) are to its right. – Conquer by applying quick sort (recursively) to both partitions. • Runtime: O(N log N) average, but O(N2) worst case. – Generally somewhat faster than merge sort. 20 Choosing a "pivot" • The algorithm will work correctly no matter which element you choose as the pivot. – A simple implementation can just use the first element. • But for efficiency, it is better if the pivot divides up the array into roughly equal partitions. – What kind of value would be a good pivot? A bad one? index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 8 18 12 -4 27 30 36 50 7 68 91 56 2 85 42 98 25 21 Partitioning an array • Swap the pivot to the last array slot, temporarily. • Repeat until done partitioning (until i,j meet): – Starting from i = 0, find an element a[i] ≥ pivot.
Recommended publications
  • Algorithms, Searching and Sorting
    Python for Data Scientists L8: Algorithms, searching and sorting 1 Iterative and recursion algorithms 2 Iterative algorithms • looping constructs (while and for loops) lead to iterative algorithms • can capture computation in a set of state variables that update on each iteration through loop 3 Iterative algorithms • “multiply x * y” is equivalent to “add x to itself y times” • capture state by • result 0 • an iteration number starts at y y y-1 and stop when y = 0 • a current value of computation (result) result result + x 4 Iterative algorithms def multiplication_iter(x, y): result = 0 while y > 0: result += x y -= 1 return result 5 Recursion The process of repeating items in a self-similar way 6 Recursion • recursive step : think how to reduce problem to a simpler/smaller version of same problem • base case : • keep reducing problem until reach a simple case that can be solved directly • when y = 1, x*y = x 7 Recursion : example x * y = x + x + x + … + x y items = x + x + x + … + x y - 1 items = x + x * (y-1) Recursion reduction def multiplication_rec(x, y): if y == 1: return x else: return x + multiplication_rec(x, y-1) 8 Recursion : example 9 Recursion : example 10 Recursion : example 11 Recursion • each recursive call to a function creates its own scope/environment • flow of control passes back to previous scope once function call returns value 12 Recursion vs Iterative algorithms • recursion may be simpler, more intuitive • recursion may be efficient for programmer but not for computers def fib_iter(n): if n == 0: return 0 O(n) elif n == 1: return 1 def fib_recur(n): else: if n == 0: O(2^n) a = 0 return 0 b = 1 elif n == 1: for i in range(n-1): return 1 tmp = a else: a = b return fib_recur(n-1) + fib_recur(n-2) b = tmp + b return b 13 Recursion : Proof by induction How do we know that our recursive code will work ? → Mathematical Induction To prove a statement indexed on integers is true for all values of n: • Prove it is true when n is smallest value (e.g.
    [Show full text]
  • Sorting Algorithm 1 Sorting Algorithm
    Sorting algorithm 1 Sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: 1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); 2. The output is a permutation, or reordering, of the input. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds. Classification Sorting algorithms used in computer science are often classified by: • Computational complexity (worst, average and best behaviour) of element comparisons in terms of the size of the list . For typical sorting algorithms good behavior is and bad behavior is .
    [Show full text]
  • Sorting Algorithm 1 Sorting Algorithm
    Sorting algorithm 1 Sorting algorithm A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: 1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); 2. The output is a permutation (reordering) of the input. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2006). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and upper and lower bounds. Classification Sorting algorithms are often classified by: • Computational complexity (worst, average and best behavior) of element comparisons in terms of the size of the list (n). For typical serial sorting algorithms good behavior is O(n log n), with parallel sort in O(log2 n), and bad behavior is O(n2).
    [Show full text]
  • Comparison Sorts Name Best Average Worst Memory Stable Method Other Notes Quicksort Is Usually Done in Place with O(Log N) Stack Space
    Comparison sorts Name Best Average Worst Memory Stable Method Other notes Quicksort is usually done in place with O(log n) stack space. Most implementations on typical in- are unstable, as stable average, worst place sort in-place partitioning is case is ; is not more complex. Naïve Quicksort Sedgewick stable; Partitioning variants use an O(n) variation is stable space array to store the worst versions partition. Quicksort case exist variant using three-way (fat) partitioning takes O(n) comparisons when sorting an array of equal keys. Highly parallelizable (up to O(log n) using the Three Hungarian's Algorithmor, more Merge sort worst case Yes Merging practically, Cole's parallel merge sort) for processing large amounts of data. Can be implemented as In-place merge sort — — Yes Merging a stable sort based on stable in-place merging. Heapsort No Selection O(n + d) where d is the Insertion sort Yes Insertion number of inversions. Introsort No Partitioning Used in several STL Comparison sorts Name Best Average Worst Memory Stable Method Other notes & Selection implementations. Stable with O(n) extra Selection sort No Selection space, for example using lists. Makes n comparisons Insertion & Timsort Yes when the data is already Merging sorted or reverse sorted. Makes n comparisons Cubesort Yes Insertion when the data is already sorted or reverse sorted. Small code size, no use Depends on gap of call stack, reasonably sequence; fast, useful where Shell sort or best known is No Insertion memory is at a premium such as embedded and older mainframe applications. Bubble sort Yes Exchanging Tiny code size.
    [Show full text]
  • An Efficient Evolutionary Algorithm for Solving
    An Efficient Evolutionary Algorithm for Solving Incrementally Structured Problems Jason Ansel Maciej Pacula Saman Amarasinghe Una-May O’Reilly Computer Science and Artificial Intelligence Laboratory Massachusetts Institute of Technology Cambridge, MA, USA {jansel,mpacula,saman,unamay}@csail.mit.edu ABSTRACT of interest. Usually solving a small instance is both simpler Many real world problems have a structure where small (because the search space is smaller) and less expensive (be- problem instances are embedded within large problem in- cause the evaluation cost is lower). Reusing a sub-solution stances, or where solution quality for large problem instances or using it as a starting point makes finding a solution to is loosely correlated to that of small problem instances. This a larger instance quicker. This shortcut is particularly ad- structure can be exploited because smaller problem instances vantageous if solution evaluation cost grows with instance typically have smaller search spaces and are cheaper to eval- size. It becomes more advantageous if the evaluation result uate. We present an evolutionary algorithm, INCREA, which is noisy or highly variable which requires additional evalua- is designed to incrementally solve a large, noisy, computa- tion sampling. tionally expensive problem by deriving its initial population This shortcut is vulnerable to local optima: a small in- through recursively running itself on problem instances of stance solution might become entrenched in the larger solu- smaller sizes. The INCREA algorithm also expands and tion but not be part of the global optimum. Or, non-linear shrinks its population each generation and cuts off work effects between variables of the smaller and larger instances that doesn't appear to promise a fruitful result.
    [Show full text]
  • Performance Analysis of Sorting Algorithms
    Masaryk University Faculty of Informatics Performance analysis of Sorting Algorithms Bachelor's Thesis Matej Hul´ın Brno, Fall 2017 Declaration Hereby I declare that this paper is my original authorial work, which I have worked out on my own. All sources, references, and literature used or excerpted during elaboration of this work are properly cited and listed in complete reference to the due source. Matej Hul´ın Advisor: prof. RNDr. Ivana Cern´a,CSc.ˇ i Acknowledgement I would like to thank my advisor, prof. RNDr. Ivana Cern´a,CSc.ˇ for her advice and support throughout the thesis work. iii Abstract The aim of the bachelor thesis was to provide an extensive overview of sorting algorithms ranging from the pessimal and quadratic time solutions, up to those, that are currently being used in contemporary standard libraries. All of the chosen algorithms and some of their variants are described in separate sections, that include pseudocodes, images and discussion of their asymptotic space and time complexity in the best, average and the worst case. The second and main goal of the thesis was to perform a performance analysis. For this reason, the selected algorithm variants, that perform optimally, have been implemented in C++. All of them have undergone several performance analysis tests, that involved 8 sequence types with different kind and level of presortedness. The measurements showed, that timsort is not necessarily faster than mergesort on sequences, that feature only very limited amount of presortedness. In addition, the results revealed, that timsort is com- petetive with quicksorts on sequences of larger objects, that require a lot of comparisons.
    [Show full text]
  • An Intuitive Introduction to Data Structures, 2Nd Edition
    An Intuitive Introduction to Data Structures, 2nd Edition Brian Heinold Department of Mathematics and Computer Science Mount St. Mary’s University ©2019 Brian Heinold Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 4.0 Unported License Preface This book covers standard topics in data structures including running time analysis, dynamic arrays, linked lists, stacks, queues, recursion, binary trees, binary search trees, heaps, hashing, sets, maps, graphs, and sorting. It is based on Data Structures and Algorithms classes I’ve taught over the years. The first time I taught the course, I used a bunch of data structures and introductory programming books as reference, and while I liked parts of all the books, none of them approached things quite in the way I wanted, so I decided to write my own book. I originally wrote this book in 2012. This version is substantially revised and reorganized from that earlier version. My approach to topics is a lot more intuitive than it is formal. If you are looking for a formal approach, there are many books out there that take that approach. I’ve tried to keep explanations short and to the point. When I was learning this material, I found that just reading about a data structure wasn’t helping the material to stick, so I would instead try to implement the data structure myself. By doing that, I was really able to get a sense for how things work. This book contains many implementations of data structures, but it is recommended that you either try to implement the data structures yourself or work along with the examples in the book.
    [Show full text]
  • Sorting, Templates, and Generic Programming 11
    June 7, 1999 10:10 owltex Sheet number 21 Page number 525 magenta black Sorting, Templates, and Generic Programming 11 No transcendent ability is required in order to make useful discoveries in science; the edifice of science needs its masons, bricklayers, and common labourers as well as its foremen, master-builders, and architects. In art nothing worth doing can be done without genius; in science even a very moderate capacity can contribute to a supreme achievement. Bertrand Russell Mysticism and Logic Many human activities require collections of items to be put into some particular order. The post office sorts mail by ZIP code for efficient delivery; telephone books are sorted by name to facilitate finding phone numbers; and hands of cards are sorted by suit to make it easier to go fish. Sorting is a task performed well by computers; the study of different methods of sorting is also intrinsically interesting from a theoretical standpoint. In Chapter 8 we saw how fast binary search is, but binary search requires a sorted list as well as random access. In this chapter we’ll study different sorting algorithms and methods for comparing these algorithms. We’ll extend the idea of conforming interfaces we studied in Chapter 7 (see Section 7.2) and see how conforming interfaces are used in template classes and functions. We’ll study sorting from a theoretical perspective, but we’ll also emphasize techniques for making sorting and other algorithmic programming more efficient in practice using generic programming and function objects. 11.1 Sorting an Array There are several elementary sorting algorithms, details of which can be found in books on algorithms; Knuth [Knu98b] is an encyclopedic reference.
    [Show full text]
  • Data Structures
    KARNATAKA STATE OPEN UNIVERSITY DATA STRUCTURES BCA I SEMESTER SUBJECT CODE: BCA 04 SUBJECT TITLE: DATA STRUCTURES 1 STRUCTURE OF THE COURSE CONTENT BLOCK 1 PROBLEM SOLVING Unit 1: Top-down Design – Implementation Unit 2: Verification – Efficiency Unit 3: Analysis Unit 4: Sample Algorithm BLOCK 2 LISTS, STACKS AND QUEUES Unit 1: Abstract Data Type (ADT) Unit 2: List ADT Unit 3: Stack ADT Unit 4: Queue ADT BLOCK 3 TREES Unit 1: Binary Trees Unit 2: AVL Trees Unit 3: Tree Traversals and Hashing Unit 4: Simple implementations of Tree BLOCK 4 SORTING Unit 1: Insertion Sort Unit 2: Shell sort and Heap sort Unit 3: Merge sort and Quick sort Unit 4: External Sort BLOCK 5 GRAPHS Unit 1: Topological Sort Unit 2: Path Algorithms Unit 3: Prim‘s Algorithm Unit 4: Undirected Graphs – Bi-connectivity Books: 1. R. G. Dromey, ―How to Solve it by Computer‖ (Chaps 1-2), Prentice-Hall of India, 2002. 2. M. A. Weiss, ―Data Structures and Algorithm Analysis in C‖, 2nd ed, Pearson Education Asia, 2002. 3. Y. Langsam, M. J. Augenstein and A. M. Tenenbaum, ―Data Structures using C‖, Pearson Education Asia, 2004 4. Richard F. Gilberg, Behrouz A. Forouzan, ―Data Structures – A Pseudocode Approach with C‖, Thomson Brooks / COLE, 1998. 5. Aho, J. E. Hopcroft and J. D. Ullman, ―Data Structures and Algorithms‖, Pearson education Asia, 1983. 2 BLOCK I BLOCK I PROBLEM SOLVING Computer problem-solving can be summed up in one word – it is demanding! It is an intricate process requiring much thought, careful planning, logical precision, persistence, and attention to detail.
    [Show full text]
  • Abductive Knowledge Induction from Raw Data
    Abductive Knowledge Induction From Raw Data Wang-Zhou Dai , Stephen H. Muggleton Department of Computing, Imperial College London, London, UK w.dai, s.muggleton @imperial.ac.uk f g Abstract and programs [Dong et al., 2019; Gaunt et al., 2017]. How- ever, it is hard to design a unified differentiable module to ac- For many reasoning-heavy tasks involving raw in- curately represent general relational knowledge, which may puts, it is challenging to design an appropriate end- contain complex inference structures such as recursion [Glas- to-end learning pipeline. Neuro-Symbolic Learn- machers, 2017; Garcez et al., 2019]. ing, divide the process into sub-symbolic percep- Therefore, many researchers propose to break the end-to- tion and symbolic reasoning, trying to utilise data- end learning pipeline apart, and build a hybrid model that driven machine learning and knowledge-driven rea- consists of smaller modules where each of them only ac- soning simultaneously. However, they suffer from counts for one specific function [Glasmachers, 2017]. A rep- the exponential computational complexity within resentative branch in this line of research is Neuro-Symbolic the interface between these two components, where (NeSy) AI [De Raedt et al., 2020; Garcez et al., 2019] aim- the sub-symbolic learning model lacks direct super- ing to bridge System 1 and System 2 AI [Kahneman, 2011; vision, and the symbolic model lacks accurate input Bengio, 2017], i.e., neural-network-based machine learning facts. Hence, most of them assume the existence of and symbolic-based relational inference. a strong symbolic knowledge base and only learn However, the lack of supervision in the non-differentiable the perception model while avoiding a crucial prob- interface between neural and symbolic systems, based on the lem: where does the knowledge come from? In facts extracted from raw data and their truth values, leads to this paper, we present Abductive Meta-Interpretive high computational complexity in learning [Li et al., 2020; Learning (MetaAbd) that unites abduction and in- Dai et al., 2019].
    [Show full text]
  • ECE-250 Course Slides -- Sorting Algorithms
    Sorting Carlos Moreno cmoreno @ uwaterloo.ca Photo courtesy of ellenmc / Flickr: EIT-4103 http://www.flickr.com/photos/ellenmc/4508741746/ https://ece.uwaterloo.ca/~cmoreno/ece250 Sorting Standard reminder to set phones to silent/vibrate mode, please! Sorting ● During today's class: ● Introduce Sorting and related concepts ● Discuss some aspects of the run time of sorting algorithms ● Look into some of the basic (inefficient) algorithms ● Introduce merge sort – Discuss its run time Sorting ● Basic definition: ● Process of taking a list of objects with a linear ordering (a1, a2, ⋯ ,an−1 ,an) and output a permutation of the list (a ,a , ⋯ ,a ,a ) k 1 k 2 k n−1 k n such that a ⩽ a ⩽ ⋯ ⩽ a ⩽ a k 1 k 2 k n−1 k n Sorting ● More often than not, we're interested in sorting a list of “records” in order by some field (e.g., we have a list of students with the various grades — assignments, midterm, labs, etc.) and we want to sort by student ID, or we want to sort by final grade, etc. Sorting ● However, in our discussions of the various sort algorithms, we will assume that: ● We're sorting values (objects that can be directly compared). – The more general case can be seen as an implementation detail ● We're using arrays for both input and output of the sequences of values. Sorting ● We will be most interested in algorithms that can be performed in-place. ● That is, requiring Θ(1) additional memory (a fixed number of local variables) ● Some times, the definition is stated as requiring o(n) additional memory (i.e., strictly less than linear amount of memory); this allows recursive methods that divide the range in two parts and proceed recursively (Θ(log n) recursive calls, which means a “hidden” memory usage of Θ(log n) Sorting ● The intuition of in-place refers to doing all the operations in the same area where the input values are, without requiring allocation of a second array of the same size.
    [Show full text]
  • MIT6 0001F16 Searching and Sorting Algorithms
    SEARCHING AND SORTING ALGORITHMS (download slides and .py files and follow along!) 6.0001 LECTURE 12 6.0001 LECTURE 12 1 SEARCH ALGORITHMS § search algorithm – method for finding an item or group of items with specific properes within a collecon of items § collecon could be implicit ◦ example – find square root as a search problem ◦ exhausve enumeraon ◦ bisecon search ◦ Newton-Raphson § collecon could be explicit ◦ example – is a student record in a stored collecon of data? 6.0001 LECTURE 12 2 SEARCHING ALGORITHMS § linear search • brute force search (aka Brish Museum algorithm) • list does not have to be sorted § bisecon search • list MUST be sorted to give correct answer • saw two different implementaons of the algorithm 6.0001 LECTURE 12 3 LINEAR SEARCH ON UNSORTED LIST: RECAP def linear_search(L, e): found = False for i in range(len(L)): if e == L[i]: found = True return found § must look through all elements to decide it’s not there § O(len(L)) for the loop * O(1) to test if e == L[i] § overall complexity is O(n) – where n is len(L) 6.0001 LECTURE 12 4 LINEAR SEARCH ON SORTED LIST: RECAP def search(L, e): for i in range(len(L)): if L[i] == e: return True if L[i] > e: return False return False § must only look unl reach a number greater than e § O( len(L)) for the loop * O(1) to test if e == L[i] § overall complexity is O(n) – where n is len(L) 6.0001 LECTURE 12 5 USE BISECTION SEARCH: RECAP 1.
    [Show full text]