Chapter 8 Searching and Sorting Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

In an average case, an item is just as likely to be found near the beginning of the array as near the end. Typically, for an array of N items, the linear search will locate an item in _______ attempts.

N/2

___ ___ ____ are used to calculate the maximum number of comparisons the binary search will make on an array of any size.

Powers of 2

If a linear search is performed on an array, and it is known that some items are searched for more frequently than others, how can the contents of the array be reordered to improve the average performance of the search?

The items frequently searched for can be stored near the beginning of the array.

Describe the difference between the linear search and the binary search.

The linear search algorithm simply uses a loop to step through each element of an array, comparing each element's value with the value being searched for. The binary search algorithm, which requires the values in the array to be sorted in order, starts searching at the element in the middle of the array. If the middle element's value is greater than the value being searched for, the algorithm next tests the element in the middle of the first half of the array. If the middle element's value is less than the value being searched for, the algorithm next tests the element in the middle of the last half of the array. Each time the array tests an array element and does not find the value being searched for, it eliminates half of the remaining portion of the array. This method continues until the value is found, or there are no more elements to test. The binary search is more efficient than the linear search.

The _____ ___ is a clever algorithm that is much more efficient than the linear search. Its only requirement is that the values in the array be sorted in order.

binary search

On average, with an array of 20,000 elements, how many comparisons will the linear search perform? (Assume the items being searched for are consistently found in the array.)

10,000

With an array of 20,000 elements, what is the maximum number of comparisons the binary search will perform?

15

If an array has 50,000 elements, the linear search will make a comparison with ________ of them in a typical case.

25,000

the binary search is much more efficient than the linear search. Every time it makes a comparison and fails to find the desired item, it ___ ___ of the remaining portion of the array that must be searched.

eliminates half

When the linear search fails to locate an item, it must make a comparison with ___ ___ ___ __. As the number of failed search attempts increases, so does the average number of comparisons. Obviously, the linear search should not be used on large arrays if the speed is important.

every element in the array

The advantage of the linear search is its ________. It is very easy to understand and implement. Furthermore, it doesn't require the data in the array to be stored in any particular order.

simplicity

A ___ ____ is a technique for stepping through an array and rearranging its contents in some order.

sorting algorithm

For example, consider an array with 1,000 elements. If the binary search fails to find an item on the first attempt, the number of elements that remains to be searched is ____. If the item is not found on the second attempt, the number of elements that remains to be searched is ____. This process continues until the binary search has either located the desired item or determined that it is not in the array. With 1,000 elements, this takes no more than 10 comparisons.

500, 250

The_________ is a very simple algorithm. Also known as the sequential search algorithm, it uses a loop to sequentially step through an array, starting with the first element.

linear search

The bubble sort is an easy way to arrange data in ascending or descending order. It is called the bubble sort algorithm because as it ___________, certain values "bubble" toward the end of the array with each pass.

makes passes through and compares the elements of the array

Sorting algorithms are used to arrange data into some _______.

order


Ensembles d'études connexes

AP Gov - Civil Rights and Civil Liberties

View Set

AUD SU 3.4: Understanding the entity and its environment

View Set

Week 5: Module 2, Section A, Ch 1, 2, 3

View Set

Chapter 13 - Formation of Sales and Lease Contracts

View Set

Intro to Psychology-Chapter 6 & 8 practice tests!!!

View Set

True/False: Characteristics of Prokaryotic and Eukaryotic Cells

View Set

Nursing Fundamentals - Comprehensive Scenarios

View Set