Unit 5 Test Review
LENGTH
(list): evaluates to the number of elements currently in the given list.
REMOVE
(list, i): removes the item at index i and moves up all items after the ith item.
Insert
(list, i, value) : inserts value into the list at index i, moving down all other items at and after i in the list.
APPEND
(list, value): adds value to the end of the list.
merge sort
As you can see, ________ starts with the cards in piles of 1 card each. Then on each pass, it merges them into piles of 2 cards, then 4 cards, then 8 cards, and so on, until all the cards are merged into one sorted pile. You probably also noticed that it was quite a bit faster than bubble sort.
They're not comparison sorts because you're not comparing the cards to one another, and are instead organizing them into categories such as suits or place values.
Bubble and Merge Sort are referred to as comparison sorts because the values of the two pieces of data are compared during each step. Why are the bucket and radix sort not comparison sorts?
comparison sort
Bubble sort is an example of a ________.
Radix
Bucket sort is actually an example of a more general non-comparison sort called ________. The word ________ is another word for base and the original idea behind ________ sorting is to sort numbers by their digits.
Barack
What name occurs at index 3 in the following list? Initialize global names to make a list Abe George Barack Harry Teddy
A pentagon with sides of length 100 pixels
What would be the image drawn by this algorithm? For each number from 1 to 5 by 1 do call forward 100 pixels call turn 72 degrees □ A square with sides of length 100 pixels □ A triangle with sides of length 72 pixels □ A pentagon with sides of length 72 pixels □ A pentagon with sides of length 100 pixels
Binary
Which algorithm behaves like the base 2 logarithm function, y = log2(x)? □ Sequential □ Binary
IF (item < x) x ← item
Which code below could be placed in the following loop to print out the item in a list that has the lowest (minimum) value? list ← [1, 0, 4, 2] x ← 99 FOR EACH item IN list { <MISSING CODE> } DISPLAY(x) □ IF (item > 99) x ← item □ IF (item > x) x ← item □ IF (item < 99) x ← item □ IF (item < x) x ← item
A comparison-based algorithm. An N^2 algorithm.
Which of the following characteristics is true of bubble sort?Choose all that apply. □ A comparison-based algorithm. □ Useful only for sorting numbers. □ An N^2 algorithm. □ More efficient than bucket sort. □ Widely used to sort large data sets.
More efficient than bubble sort. A linear algorithm
Which of the following characteristics is true of bucket sort?Choose all that apply. □ A comparison-based algorithm. □ Useful only for sorting numbers. □ An N2 algorithm. □ More efficient than bubble sort. □ A linear algorithm
14
how many lookups would be required in the worst case to find a number in a sorted list of 10000 elements using binary search
10000
how many lookups would be required in the worst case to find a number in list of 10000 elements using linear search?
Bubble sort
‣ It repeatedly compares two cards, placing the smaller one on the left pile. ‣ The ________ is so-called because on each pass through the data, the highest value "bubbles" to the top.
Decision problem
A problem that has a yes or no answer
Optimization problem
A problem with the goal of finding the best (optimal) solution among many
Binary search
Al algorithm that repeatedly divides a sorted listed to narrow in on the searched-for item
Instance of a problem
An ________ also includes specific input. For example, sorting is a problem, and sorting the list (2,3,1,7) is ________.
Linear/sequential search
An algorithm that checks every element in a list from start to the end of the list to find an item
Heuristic algorithm
An algorithm that finds an approximate solution for a hard problem; helpful for finding a solution in a reasonable amount of time
Sorting algorithm
An algorithm that puts a list into alphabetic or numeric order
□ Accessing an element by index: list[i] where i is an index from 1 to the length of the list. □ Saving an element of a list into a variable like x: x ← list[i] □ Assigning a value to an element of a list:list[i] ← x assigns the value of x to list[i]. list[i] ← list[j] assigns the value of list[j] to list[i]. □ Insert □ Append □ Remove □ Length
Basic operations of lists include:
Unreasonable time
Exponential time
9
For a list of 500 numbers, at most how many iterations would the loop in binary search run to find a number? For example, if this was a guessing game, at most how many guesses would it take using binary search to guess a secret number from 1-500, if after each guess you were told whether your guess was too high or too low or just right? Type your answer into the text box.
Binary Search
For searching a sorted list, which search algorithm is the better choice? □ Linear search □ Binary search
Linear search
For searching an unordered list, which search algorithm is the better choice? □ Linear search □ Binary search
🟃 Looking up a phone number in the phone book given the person's full (unique) name. 🟃 Looking up a word in a dictionary. 🟃 Looking up a person's name in the phone book given the person's phone number. 🟃 Guessing a secret number between 1 and 100.
For which of the problems could the linear search algorithm be used? Choose all that apply. □ Arranging a deck of cards from the lowest to the highest value cards. □ Looking up a phone number in the phone book given the person's full (unique) name. □ Looking up a word in a dictionary. □ Looking up a person's name in the phone book given the person's phone number. □ Guessing a secret number between 1 and 100.
🟃 Looking up a phone number in the phone book given the person's full (unique) name. 🟃 Looking up a word in a dictionary.
For which of the problems would the binary search algorithm be useful? Choose all that apply. □ Arranging a deck of cards from the lowest to the highest value cards. □ Looking up a phone number in the phone book given the person's full (unique) name. □ Looking up a word in a dictionary. □ Looking up a person's name in the phone book given the person's phone number. □ Finding the smallest number in a list of numbers arranged randomly.
🟃 Arranging a deck of cards from the lowest to the highest value cards. 🟃 Sorting a stack of paper money into denominations -- i.e., $1, $5, $10 etc. 🟃 Arranging books on a bookshelf by author's last name.
For which of the problems would the bubble sort algorithm provide an appropriate solution. Choose all that apply. □ Arranging a deck of cards from the lowest to the highest value cards. □ Looking up a name in the phone book. □ Sorting a stack of paper money into denominations -- i.e., $1, $5, $10 etc. □ Sorting a basket of laundry into socks, shirts, shorts, and sheets. □ Arranging books on a bookshelf by author's last name.
The traveling salesman problem
Given a list of cities and the distances between them, find the shortest path visiting each city once and returning to the start
Undecidable problems
Have no algorithm that can be constructed that always leads to a correct yes-or-no answer
Efficiency
How well an algorithm uses time and memory/space resources, CPU and RAM
3
If you were using binary search to find the number 14 in the following list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], how many iterations would be required to find 14 in the list? □ 2 □ 3 □ 4 □ 14
how long it takes to arrange the values in order.
In talking about sorting algorithms in general, a sort algorithm's efficiency refers to ______________________. □ how many comparisons are needed to sort the values. □ whether the algorithm correctly arranges the values in order. □ whether or not the algorithm contains a bug. □ how long it takes to arrange the values in order. □ how many swaps are needed to sort the values.
13
In the bubble sort demo, 13 cards are being sorted. How many passes does this version of the algorithm require to sort the cards?
Parameters
Input variables for a procedure
5
What is the length of the following list? Type your answer into the textbox. Initialize global names to make a list Abe George Barack Harry Teddy
Intractable
Is the following problem tractable (solvable in a reasonable amount of time) or intractable (cannot be solved in a reasonable amount of time)? For any length string of letters using any combination of the letters 'a' through 'z', write down all possible strings. □ Tractable □ Intractable
FOR EACH item in List { IF (item = target) DISPLAY "Found target!" }
Linear Search Pseudocode
divide and conquer
Merge sort, like binary search, is another example of a ________ approach to solving the problem, so-called, because it breaks the big problem into smaller problems and works on the smaller problems. In this case, the deck is divided into piles of 1 card each before merging the piles.
comparison
Not all sorts are ________ sorts.
Bucket sort
One example of a non-comparison sort, is the ________ ________, which uses some feature of the values being sorted to place them into distinct buckets. The buckets are then combined together.
Reasonable time
Polynomial time
Decidable problems
Problems in which an algorithm can be constructed to answer 'yes; or 'no' for all inputs (e.g., 'is the number even?')
Intractable problems
Problems that are practically impossible to solve in a <b
low ← 0 high ← N middle ← item (low+high)/2 (compute the middle of the list, rounded down) REPEAT UNTIL (middle = target OR low > high) { IF (target < middle) high ← middle - 1 (This cuts off the top half of the list) IF (target > middle) low ← middle + 1 (This cuts off the bottom half of the list) middle ← item (low+high)/2 (compute new middle) } IF (middle = target) DISPLAY "Found target!" ELSE DISPLAY "Target not in list"
Pseudocode for binary search algorithm
Brute force
Solve by trial and error; trying every possible option
17
Suppose you are sorting the following list of numbers in ascending order using bubble sort: [16, 5, -1, 4, 12, 17, 3, 10, 5, 9]. After the first pass through the numbers, what value would appear on the right of the list? □ 16 □ 17 □ 9 □ -1 □ 5
[apple, banana, lemon, orange, papaya, pumpkin, squash, tomato]
Suppose you are sorting the following list of words in alphabetical order using bubble sort: [apple, banana, lemon, tomato, orange, squash, papaya, pumpkin]. Which of the following gives the correct order of the list after two passes through the list? □ [apple, banana, lemon, tomato, orange, squash, papaya, pumpkin] □ [apple, banana, lemon, squash, tomato, orange, papaya, pumpkin] □ [apple, banana, lemon, orange, papaya, pumpkin, tomato, squash] □ [apple, banana, lemon, orange, papaya, pumpkin, squash, tomato] □ [apple, banana, lemon, orange, papaya, squash, tomato, pumpkin]
tomato
Suppose you are sorting the following list of words into alphabetical order using bubble sort: [apple, orange, banana, papaya, lemon, pumpkin, squash, tomato]. After the first pass through the list, what word would appear on the right of the list? □ squash □ tomato □ pumpkin □ papaya
an undecidable problem.
The Halting Problemis an example of □ an intractable problem. □ an exponential problem. □ an undecidable problem. □ a difficult problem.
decision problem
The halting problem is a ________
Procedural abstraction
The practice of organizing and encapsulating algorithms in named procedures that can then be invoked by name. An example would be the 'sqrt(x)', square root of x, which encapsulates the algorithm for calculating the square root of x.
The halting problem
The undecidable problem of determining whether a computer program will halt (produce an answer) at some point or loop forever on a given input
(C) Both Algorithm A and Algorithm B always calculate the correct average.
There are 32 students standing in a classroom. Two different algorithms are given for finding the average height of the students.Algorithm AStep 1: All students stand.Step 2: A randomly selected student writes his or her height on a card and is seated.Step 3: A randomly selected standing student adds his or her height to the value on the card, records the new value on the card, and is seated. The previous value on the card is erased.Step 4: Repeat step 3 until no students remain standing.Step 5: The sum on the card is divided by 32. The result is given to the teacher.Algorithm BStep 1: All students stand.Step 2: Each student is given a card. Each student writes his or her height on the card.Step 3: Standing students form random pairs at the same time. Each pair adds the numbers written on their cards and writes the result on one student's card; the other student is seated. The previous value on the card is erased.Step 4: Repeat step 3 until one student remains standing.Step 5: The sum on the last student's card is divided by 32. The result is given to the teacher.Which of the following statements is true? (A) Algorithm A always calculates the correct average, but Algorithm B does not. (B) Algorithm B always calculates the correct average, but Algorithm A does not. (C) Both Algorithm A and Algorithm B always calculate the correct average. (D) Neither Algorithm A nor Algorithm B calculates the correct average.
binary search algorithm
There is a very efficient algorithm for the guessing game problem, known as the ________.
Arguments
Theses specify the values of the parameters when a procedure is called
The procedure always draws a square with sides of size 50. The parameter L is ignored,
To drawSquare L do for each number from 1 to 4 by 1 do call forward 50 pixels call turn 90 degrees □ The procedure draws a pentagon not a square □ The procedure draws a triangle not a square □ The procedure always draws a square with sides of size 50. The parameter L is ignored, □ The procedure parameter isn't specified correctly
7
To guess a number between 1 and 100, what's the maximum number of guesses your algorithm would take?
as the size of the list grows, bucket sort will be faster than bubble sort.
To say that bucket sort is more efficient than bubble sort means that _________________. □ for any size list, bucket sort will always be faster than bubble sort. □ as the size of the list grows, bucket sort will be faster than bubble sort. □ bucket sort requires fewer comparisons than bubble sort. □ bucket sort requires fewer swaps than bubble sort.
False
True or False: An algorithm can be found for any computational problem whatsoever. □ True □ False
False
True or false: All intractable problems (that cannot be solved in a reasonable time) are bad. □ True □ False
(C) When the problem cannot be solved in a reasonable time and an approximate solution is acceptable.
Under which of the following conditions is it most beneficial to use a heuristic approach to solve a problem? (A) When the problem can be solved in a reasonable time and an approximate solution is acceptable. (B) When the problem can be solved in a reasonable time and an exact solution is needed. (C) When the problem cannot be solved in a reasonable time and an approximate solution is acceptable. (D) When the problem cannot be solved in a reasonable time and an exact solution is needed.
More efficient
Usually means it runes faster or uses less space
[1, 3, 5, 4]
What are the values in the list after executing the following code: list ← [ 0, 3, 5 ] APPEND( list, 4 ) INSERT( list, 2, 1 ) REMOVE( list, 1 ) □ [1, 3, 5, 4] □ [1, 2, 3, 4] □ [0, 3, 4, 5] □ [0, 3, 5, 4]
Displays 4 which is the maximum (largest) value in the list.
What does the following code do? list ← 1, 0, 4, 2 x ← -1 FOR EACH item IN list IF item > x x ← item DISPLAY x □ Displays 0 which is the minimum (lowest) value in the list. □ Displays 1 which is the first item in the list. □ Displays -1 which is the value of x. □ Displays 4 which is the maximum (largest) value in the list.
Bucket
What is the fastest of the 3 sorts?