Question: Introduction Write a C program called search.c. Implement two search functions which determine the location (index) of value s in an ordered array (ascending order)

Introduction

Write a C program called search.c. Implement two search functions which determine the location (index) of value s in an ordered array (ascending order) and return -1 if the value s is not present in the array. Each function uses a different algorithm to locate a value in an array.

Signature of two functions:

int sequential_search(int *list, int size, int toSearch); int binary_search(int *list, int size, int toSearch); 
Parameter comment
list an array of values pre-sorted in ascending order
size the amount of values in the array
toSearch the value to search

Instructions

In your sequential_search function, simply scan through the array and compares each array element with the value you want to search. If a match occurs the index of the matched element will be returned. If no match is found after the full scan, -1 will be returned.

In your binary_search function, considers the element at the middle of the array where middle = (left + right) / 2. The variable left and right store the left and right index that defines the current section of the array that may contain your toSearch value. At the first round, when you are looking at the whole array, left is 0 and right is size - 1. It then determines if the value of the middle element in section of the array is greater, less, or equal to toSearch value. If it is equal to toSearch, the value has been found and the index location is returned. If it is greater than toSearch, your toSearch may be located in the left half of the current section, then your left will stay same and your right will be changed to the position on the left next to the middle. If the value is less than toSearch, your toSearch may be located in the right half of the current section, then your left should be changed to the the position on the right next to the middle and your right will stay same. This process of halving the array and looking at the middle element is repeated until the element is found or not located in any of the position tested (when left is greater than right). Below is some pseudo code for this logic:

// initialize your left, and right while (left <= right) { // so your search will be done once left > right // loop body, look at the middle value and update left and right accordingly, return middle once a match is found } // not found if you reach here, return not found value 

The main program initializes an array of 10 values in ascending order and a value to search (make your numbers). The two search function should be called respectively and the result be displayed using the following format depending on the location.

The value of s has been found at location l in the array. Or The value of s is not present in the array. Replace s and l with the value to search and the index (if found).

You may start with the array value as {1, 4, 6, 18, 33, 56, 64, 71, 99, 102} and set value to search to 64 to try.

Sample run 1 (searching 64):

The value of 64 has been found at location 6 in the array. The value of 64 has been found at location 6 in the array. 

Sample run 2 (searching 60, not present in array):

The value of 60 is not present in the array. The value of 60 is not present in the array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!