Computer Science Unit 5

¡Supera tus tareas y exámenes ahora con Quizwiz!

According to the following table, how many lookups would be required in the worst case to find a number in list of 10000 elements using linear search? Type your answer in the text box.

10000

The function shown in this graph is known as the base-2 logarithm function, y = log2(x). Which search algorithm behaves like this function?

Binary search

There are 32 students standing in a classroom. Two different algorithms are given for finding the average height of the students. Algorithm A Step 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 B Step 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?

Both Algorithm A and Algorithm B always calculate the correct average.

Bubble sort

Bubble sort is an example of a comparison sort. It repeatedly compares two values, if the first is greater than the second it swaps the values until it reaches the end of the list. It starts the process again at the beginning of list and repeats until no swaps have occurred during a pass through the list. (Not efficient - worst case n*n passes quadratic function)

Bucket sort

Bucket sort does not compare one card with another. Rather, it looks at the card's value and places it into the appropriate bucket. Once all the cards are in their buckets, they are collected together in order. This sort is the fastest of the three examples we studied. (Efficiency - fastest of the sorts - 2 n linear function)

Under which of the following conditions is it most beneficial to use a heuristic approach to solve a problem?

When the problem cannot be solved in a reasonable time and an approximate solution is acceptable.

Sorting

You are "putting objects in order"

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]

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, orange, papaya, pumpkin, squash, tomato]

Selection

a conditional instruction that lets the program branch between two or more alternatives. Examples are if, and if / else statements

Sequence

a sequence of instructions or statements. Statements in a program or steps in an algorithm are executed in the order in which they are given

Repetition

a structure that repeats one or more instructions. Examples of looping structures are for loops and while loops

Index

allows access to an element

The Halting Problemis an example of

an undecidable problem

Undecidable problems

are problems for which no algorithm can possibly be written. (Example the halting problem)

To say that bucket sort is more efficient than bubble sort means that _________________.

as the size of the list grows, bucket sort will be faster than bubble sort.

Intractable problems

can only be solved by exponential algorithms. There is an algorithm to solve the problem but it is too inefficient to solve the problem as N grows large. Example cryptography

Lists

data structure - allows us to reference multiple data elements using one variable name

List of lists

each item in the list is also a list

A teacher uses the following program to adjust student grades on an assignment by adding 5 points to each student's original grade. However, if adding 5 points to a student's original grade causes the grade to exceed 100 points, the student will receive the maximum possible score of 100 points. The students' original grades are stored in the list gradeList, which is indexed from 1 to n. i ← 1 REPEAT n TIMES { <MISSING CODE> i ← i + 1 } The teacher has the following procedures available. Which of the following code segments can replace <MISSING CODE> so that the program works as intended? Select two answers.

gradeList[i] ← min (gradeList[i] + 5, 100) gradeList[i] ← gradeList[i] + 5 IF (gradeList[i] > 100) { gradeList[i] ← 100 }

Binary search

he data must be sorted - Divide the search space into two parts. Keep the half that possibly contains the secret number and throw away or ignore the other half. Binary search is an example of a divide and conquer approach to solving the problem, so-called, because it breaks the big problem into smaller problems and works on the smaller problems. (Efficiency log n Logarithmic function)

In talking about sorting algorithms in general, a sort algorithm's efficiency refers to ______________________.

how long it takes to arrange the values in order.

Semantic

logic error - occurs at run time - code does not produce desired output - Maybe the programmer mistakenly had the block of code to displayed a label in sequence before the block of code that updated the value to be displayed.

Sequential search

start at beginning of list - check each element until you find what you are looking for or reach end of list. (Efficiency - n Linear function )

Syntax

the set of rules for the programming language are broken - compiler detects these errors and code will not run until corrected - Maybe the programmer attempts to use an incorrect data type

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?

tomato

Google Search

when you search the web you are actually searching Google's index of the web. Google's spider programs are constantly traversing the web, collecting millions of web pages and organizing them into an index. When you do a Web search Google's algorithms are searching that index.indexes of the web.

According to the following table, how many lookups would be required in the worst case to find a number in a sorted list of 10000 elements using binary search? Type your answer in the text box.

14

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?

17

For a list of 500 numbers, at most how many guesses would it take using binary search to guess the secret number 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.

9

1 point Which of the following characteristics is true of bubble sort? Choose all that apply.

A comparison-based algorithm. An N2 algorithm.

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. 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 searching a sorted list, which search algorithm is the better choice?

Binary search

True or False: An algorithm can be found for any computational problem whatsoever.

False

True or false: All intractable problems (that cannot be solved in a reasonable time) are bad.

False

Heuristic algorithms

For some intractable problems we can use a heuristic algorithm to get "good enough" results. Example Traveling salesman and scheduling problems

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.

Intractable

A summer camp offers a morning session and an afternoon session. The list morningList contains the names of all children attending the morning session, and the list afternoonList contains the names of all children attending the afternoon session. Only children who attend both sessions eat lunch at the camp. The camp director wants to create lunchList, which will contain the names of children attending both sessions. The following code segment is intended to create lunchList, which is initially empty. It uses the procedure IsFound (list, name), which returns true if name is found in list and returns false otherwise. FOR EACH child IN morningList { <MISSING CODE> } Which of the following could replace <MISSING CODE> so that the code segment works as intended?

IF (IsFound (afternoonList, child)) { APPEND (lunchList, child) }

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 < x) x ← item

For searching an unordered list, which search algorithm is the better choice?

Linear search

For which of the problems would the binary search algorithm be useful? Choose all that apply.

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 could the linear search algorithm be used? Choose all that apply.

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.

Merge sort

Merge sort is another comparison sorting algorithm, so called because it merges the cards into ever larger piles of cards. Two data items are sorted then merged into groups of 4 sorted items, then 8 sorted items until all the elements are merged into one sorted list. Much more efficient than bubble sort (Efficiency doubling each pass - so n log n logarithmic function)

Which of the following characteristics is true of bucket sort? Choose all that apply.

More efficient than bubble sort. A linear algorithm


Conjuntos de estudio relacionados

Chapter 2: Beginnings of English America, 1607-1660

View Set

Enzymes and hormones of the digestive System

View Set

Chapter 5: Price Controls and Quotas: Meddling with Markets

View Set

What Are Natural Resources? Practice - 100%

View Set

Common Ethical Issues Unit 2 Lesson One Personal versus Organizational Ethics

View Set