8.1 focus on software engineering into to search algorithm

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

How does binary search work?

Its starts in the middle of the array. if that element is the number we are looking for it ends the search. the value in the middle element is either less than or greater than the value we are looking for. 1. if it is greater we will look in the first half of the array. 2. if it is smaller then we will look in the second half of the array. so on an so on

swapping array elements int a = 1; int b = 9; what happens if we do a = b; b = a;

a = b causes 9 to be assigned to a. The old value 1 is thrown away. b = a because the variable a contains 9, this assigns 9 to b. So both variables contain 9. We need a third variable to successfully swap these variables. temp will serve as a temporary storage location. int temp; temp = a; a = b; b = temp;

Binary search's reqirements is what?

that the array be sorted.

what is the psuedo code for the linear search algorithm?

1. Set found to false 2. Set position to -1 3. Set index to 0 while found is false and index < number of elements --- if list[index] is equal to search value --------found = true --------position = index ---add 1 to index end while return position.

What are the steps to the linear search algorithm?

1. in main set up the array 2. set result to equal the call of your linear search function. The call will send (arrayName, size, the value we are looking for) 3. set up if else for if -1 is returned we print "value not found" else we print "value was found." 4. set up our linear function. int index = 0 int position = -1 bool found = false 5. function body while (index is less than size && !found) if index++ return position

efficiency binary search

Binary Search is efficient because everytime it does not find the desired value it eliminates the half of the remaining portion of the array.

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

a power o 2 is raised to the power of some number. So find the smallest power of 2 that is greater than or equal to the number of elements in the array. ex. maximum of 16 comparison will be made on an array of 50,000 elements. (2^16 = 65,536)

search algorithm concept:

a search algorithm is a method of locating a specific item in a larger collection of data.

linear search

also known as sequential search algorithm, uses a loop to sequentially step through an array, starting with the first element. IT compares each element with the value being searched for, and stops when either the value is found or the end of the array is encountered. If the value being searched for is not in the array, the algorithm will unsuccessfully search to the end of the array.

bubble sort

is an easy way to arrange data in ascending or descending order. it is called bubble sort because as it passes through and compares the elements of the array, certain values bubble toward the end of the array with each pass. ex. if you are using bubble sort to sort an array in descending order, the smaller values move toward the end.

What is the advantage and disadvantage of linear search algorithm?

its simplicity, it does not requre the data in the array to be stored in any particular order. Its disadvantage is inefficiency. if the array has a large amount of elements if will take a long time to go through the array.

selection sort

moves items immediately to their final position in the array. The smallest value in the array is located and moved to element 0. This process continues until all of the elements have been place in their proper order.

in an average case, an item is just as likekly to be found near the beginning of the array as near the end.

typically, for an array of N items, the linear search will lovate an item in N/2 attempts. if an array has 50,000 elements, the linear search will make a comparison with 25,000 of them in a typical case. N/2 is the average number of comparisons. the maximum number of comparisons is always N.

bubble sort is inefficient why?

values move only one element at a time toward their final destination.


Conjuntos de estudio relacionados

GS ENVS 102 CH 8 Tsunami Versus Wind-Caused Waves

View Set

Chapter 7 America Secedes from the Empire 1775-1783

View Set

Chapter 13: Fluid and Electrolytes: Balance and Disturbance Prep U

View Set

Principles of Accounting - Chapter Two Study Guide

View Set