C++ The sequential search algorithm as given in this chapter does not assume tha
ID: 3773348 • Letter: C
Question
C++
The sequential search algorithm as given in this chapter does not assume that the list is in order. Therefore, it usually works the same for both sorted and unsorted lists. However, if the elements of the list are sorted, you can somewhat improve the performance of the sequential search algorithm. For example, if the search item is not in the list, you can stop the search as soon as you find an element in the list that is larger than the search item. Write the function seqOrdSearch to implement a version of the sequential search algorithm for sorted lists. Add this function to the class orderedArrayListType and write a program to test it.
Explanation / Answer
bool seqOrdSearch(orderedArrayListType list, int key){
node *n1 = list.head;
while(n1 != NULL){
if(n1->data == key) return true;
else if(n1->data < key) n1 = n1->next;
else return false;
}
return false
}
/* the assumptions:
node struct is as follows:
struct node{
int data;
node *next;
};
orderedArrayListType class is as follows:
class orderedArrayListType{
node *head;
}
If the definitions are different, please comment and I'll modify the function accordingly!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.