C++ Programming II Chapter 8 PowerPoint

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

search algorithm

A ___ is a method of finding a specific item that is stored in an array or other collection of data.

The Bubble Sort

An easy way to arrange data in ascending or descending order. It starts by comparing the first two elements in the array. If element 0 is greater than element 1, they are exchanged. The sort repeatedly passes through the array until no exchanges are made.

int x3, while, if, return, else if, else, return

Binary Search

efficient very large sorted in order

Binary Search Algorithm • Advantages •Much more ___ than the linear search algorithm. •Works well for searching ___ arrays. • Disadvantages •Requires all array elements to be ___

bool, int, do, for, if, while,

Bubble Sort

•Linear Search: average of 500 comparisons •Binary Search: no more than 10 comparisons (2^10 = 1,024)

For an array with 1,000 elements, give the average number of comparisons made by linear search and binary search respectively.

Linear Search: average of 50,000,000 comparisons Binary Search: no more than 27 comparisons (2^27= 134,217,728)

For an array with 100,000,000 elements, give the average number of comparisons made by linear search and binary search respectively.

Linear Search: average of 25,000 comparisons Binary Search: no more than 16 comparisons (2^16= 65,536)

For an array with 50,000 elements, give the average number of comparisons made by linear search and binary search respectively.

linear: 500 (1,000 / 2) binary: 10 (2^? = 1,000)

If searching an array with 1,000 elements, how many comparisons on average will the linear search algorithm make vs the binary search algorithm?

end of the array

If the item is not in the array, the linear search algorithm will search to the...

the item is not in the array

In the binary search algorithm, first > last implies that...

Bubble Sort (disadvantage)

Inefficient for large arrays because items only move by one element at a time.

for, if, return, return

Linear Search

implement understand particular order (i.e., data does not have to be sorted.) small arrays.

Linear Search Algorithm - Advantages Simple Easy to ___ Easy to ___ Does not require the data in the array to be in any ___, Very suitable for ___

very large arrays ARRAY_SIZE ARRAY_SIZE / 2 ARRAY_SIZE

Linear Search Algorithm - Disadvantages • Can require a lot of CPU time for ___ • Maximum number of comparisons = ___ •Average number of comparisons = ___ •If search item is not in the array, then the number of comparisons = ___

sequential search (because it searches each element sequentially)

The linear search algorithm is also called a...?

not a valid subscript

The reason -1 is returned when the search value is not found in the array is because -1 is...

linear binary

Two search algorithms ___ search ___ search

linear search algorithm

Uses a loop to try to find a specific item in an array. It compares the item with the contents of the 1st array element, then the 2nd array element, then the 3rd array element, and so on.

stops

When the item is found the linear search algorithm ___.

int x2, for, for, if, if,

Selection Sort

descending order

Sorting Algorithms - values are arranged from largest to smallest

ascending order

Sorting Algorithms The following list of names is sorted in.... Abby Billy Gloria Jack Paul Zack

descending order

Sorting Algorithms The following list of names is sorted in.... Zack Paul Jack Gloria Billy Abby

sorting algorithm

Sorting Algorithms a technique for scanning through an array and rearranging its contents in some specific order.

ascending order

Sorting Algorithms - values are arranged from smallest to largest

Selection (advantage)

The ___ sort usually performs fewer swaps because it moves some items immediately to their final position.

int linearSearch(dtype array[], int array_size, dtype value) { ____for (int c = 0; c < array_size, c++) _________if (array[c] == value) ____________return c; ____return -1; } ( Pass the array you want to sort, the size of the array (if not declared as a global constant), and the "value" that you're searching for. !!make sure the data type of the array matches the data type of the value!! Then, use a for loop to sort through each element of the array in question. You'll want to stop when the count variable is = to the size of the array because if the array has 40 elements, they will be stored from 0 to 39. Element 40 will have nothing stored in it, and in fact, will not exist. Under the for loop will be an if statement that simply compares the value of the array at index c to the value being searched for. If the data in the array at that index equals the value being searched for, the if statement will be true, which will execute the next line. The next line will return the value of c, which in this case is equal to the index number of the array that contains the desired data. (It is worth noting that because this statement is a return statement, it aborts the function and force-terminates the for loop.) With this information, you now know where in the array the information is and can use that to display relevant output. !! Don't forget to have some variable set to receive the value returned by the linearSearch function !! If the for loop terminates entirely, it means that the value being searched for wasn't found. Because the if-statement is never true, it never returns the value of c, and so in the end the line below gets executed: the return -1; line. -1 is called a flag. It's usually used in c++ as a warning of some kind. In this case, it serves as an indication that the information (value) being searched for was not found in the array. )

Write the linear search algorithm (short v.)


Conjuntos de estudio relacionados

Chapter 11 - Alternative Products

View Set

Chapter 18: Communication and Collaboration

View Set

Psychiatric/Mental Health HESI Practice Exam

View Set

PrepU Query Quiz: Labor and Delivery

View Set

Chapter 15 - Brain and Cranial Nerves

View Set