--- tags: Setup-Summer21 --- # Lecture 18 Setup/Prep We've been talking about how different data structures yield different runtimes for the same problem. We've talked a little bit about how different data structures have different space needs (and how that can connect to runtime). Our choices of algorithms and even general code organization also have implications for how much time and space our programs use. In this class, we'll explore the connection between code organization and memory usage. ## Prep We are going to start with `quicksort`. As you might recall, this algorithm takes the following approach: ``` quicksort(numList) = if numList.length <= 1 return empty else pivot = the first item in the list smaller = the items in numList smaller than pivot larger = the items in numList larger than pivot return append quicksort(smaller), pivot, quicksort(larger) ``` We'll learn how to implement quicksort without creating additional lists for the smaller/larger elements. ## For Reference Here's the code sketch that we'll use for the second exercise. Don't put it in a programming environment, it's just here in case you want a copy for note taking. ``` // AveragePos1 temps = List(67, 45, 0, 66, -21, 50) posTemps = temps.filter(t => t > 0) avgTemp = posTemps.sum / posTemps.length println(avgTemp) // Point A ``` ``` // AveragePos2 temps = List(67, 45, 0, 66, -21, 50) function avgPos(data: List[Int]) = { posTemps = temps.filter(t => t > 0) return posTemps.sum / posTemps.length } println(avgPos(temps)) // Point B ``` ``` class runsScored(String player, int inning1, int inning2) { data = List(new runsScored("Rohit", 6, 12), new runsScored("Virat", 11, 72), new runsScored("Rishabh", 91, 11)) // compute some information about each scorecard entry sums = data.map(d => (d.inning1 + d.inning2)) // write out the computed data (to file, to the screen, etc) for (s in sums) { println("a player scored " + s) } } ```