Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
AP CSP · AP® Computer Science Principles
AP Computer Science Principles: Data, Algorithms, and Impact · Big Idea 3: Algorithms and Programming

Lists, Binary Search, and Procedures

Your music app has a playlist of 1,000,000 songs. You type a title and the song pops up almost instantly. Does the app really look at every song, one by one? In this lesson you will see how lists store data, how a clever search can find an item in about 20 steps, and how procedures let you reuse code.

Lists: Storing Many Values

A list is an ordered collection of items stored under one name. Instead of score1, score2, score3, … you write one list: scores ← [88, 92, 75]. This is data abstraction: one name stands for many values, so your program doesn't care how many there are.

Each item has an index, its position in the list. In AP pseudocode, the first item is at index 1. (Python starts at 0, so be careful when you switch.)

A list named nums with four items, 4, 9, 1 and 7, at indexes 1 through 4 4 9 1 7 index 1 index 2 index 3 index 4 nums

Here are the list operations from the AP reference sheet:

Operation What it does
nums[i] the item at index i
nums[i] ← v replace the item at index i
INSERT(nums, i, v) put v at index i; later items shift right
APPEND(nums, v) add v at the end
REMOVE(nums, i) delete the item at index i; later items shift left
LENGTH(nums) number of items

To visit every item, use FOR EACH:

total ← 0

FOR EACH x IN nums {

  total ← total + x

}

On each pass, x holds the next item, from first to last.

Practice★☆☆Not started
Let nums ← [4, 9, 1, 7]. Remember that AP list positions start at 1. a) What is nums[2]? b) What is LENGTH(nums)? c) What is LENGTH(nums) after APPEND(nums, 5)?

Let nums ← [4, 9, 1, 7]. Remember that AP list positions start at 1.

  1. a) What is nums[2]?
  1. b) What is LENGTH(nums)?
  1. c) What is LENGTH(nums) after APPEND(nums, 5)?
Solve
Practice★☆☆Not started
What is the final content of list? Show the list after each step.
list ← [10, 20, 30]

INSERT(list, 2, 15)

REMOVE(list, 1)

What is the final content of list? Show the list after each step.

Solve

Procedures, Parameters, and Return Values

A procedure is a named block of code you can call whenever you need it. It can take parameters (inputs) and send back a result with RETURN.

PROCEDURE countAbove(list, limit) {

  count ← 0

  FOR EACH item IN list {

    IF (item > limit) {

      count ← count + 1

    }

  }

  RETURN(count)

}
  • list and limit are the parameters: placeholders in the definition.

  • When you call countAbove([72, 85, 90], 80), the values you pass in are the arguments.

  • RETURN(count) ends the procedure and hands back a value. The call can be used like a value: DISPLAY(countAbove(scores, 80)).

This is procedural abstraction. You can use countAbove without thinking about how it counts, and if you fix a bug inside it, every place that calls it is fixed too. Write once, use many times.

Practice★★☆Not started
a) What does countAbove([72, 85, 90, 64, 88], 80) return? b) What does countAbove([72, 85, 90, 64, 88], 90) return? c) Describe in one sentence what the procedure does.
PROCEDURE countAbove(list, limit)

{

  count ← 0

  FOR EACH item IN list

  {

    IF (item > limit)

    {

      count ← count + 1

    }

  }

  RETURN(count)

}
  1. a) What does countAbove([72, 85, 90, 64, 88], 80) return?
  1. b) What does countAbove([72, 85, 90, 64, 88], 90) return?
  1. c) Describe in one sentence what the procedure does.
Solve
Practice★★☆Not started
a) Evaluate mystery(4, 9). b) Evaluate mystery(mystery(3, 8), 5). c) What does the procedure do? Identify the parameters and the return value.
PROCEDURE mystery(a, b)

{

  IF (a > b)

  {

    RETURN(a)

  }

  ELSE

  {

    RETURN(b)

  }

}
  1. a) Evaluate mystery(4, 9).
  1. b) Evaluate mystery(mystery(3, 8), 5).
  1. c) What does the procedure do? Identify the parameters and the return value.
Solve

Libraries and APIs

You don't have to write every procedure yourself. A library is a collection of ready-made procedures that other programmers wrote. An API (application programming interface) describes how to call those procedures: their names, the parameters they expect, and what they return. For example, a weather API might give you a procedure getTemperature(city) that returns a number. You only need the documentation to use it, not the code inside. That is procedural abstraction again.

When you use someone else's library, give attribution: credit the author and follow the license.

Putting It Together: Edge Cases

Lists and procedures work together. Here is a procedure that finds an average:

PROCEDURE average(list) {

  total ← 0

  FOR EACH x IN list { total ← total + x }

  RETURN(total / LENGTH(list))

}

For [80, 90], total is 170 and it returns 170 / 2 = 85. But what about an empty list? Then LENGTH(list) is 0 and the program divides by zero, which is an error. Always test edge cases: empty lists, one item, the first or last index.

A fix uses selection:

IF (LENGTH(list) = 0) { RETURN(0) }

placed at the start of the procedure.

Back to the start: the music app can find a song among 1,000,000 in about 20 checks because the song list is sorted and binary search halves it every time ($2^{20} \approx 1{,}000{,}000$).

Practice★★★Not started
a) Trace average([80, 90, 70, 100]). What is returned? b) What goes wrong if the list is empty? Rewrite the procedure so that it returns 0 for an empty list. c) Explain…
PROCEDURE average(list)

{

  total ← 0

  FOR EACH x IN list

  {

    total ← total + x

  }

  RETURN(total / LENGTH(list))

}
  1. a) Trace average([80, 90, 70, 100]). What is returned?
  1. b) What goes wrong if the list is empty? Rewrite the procedure so that it returns 0 for an empty list.
  1. c) Explain how procedural abstraction helps a teacher who uses this procedure in many parts of a gradebook program.
Solve

Summary

See all the exercises in this section