
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
WRITE THE MAIN.CPP FOR THIS PROGRAM
a. Write a version of the sequential search
b. Consider the following list: 2, 20, 38, 41, 49, 56, 62, 70, 88, 95, 100, 135, 145
Using a sequential search on ordered lists, that you designed in (a), how many comparisons are required to determine whether the following items are in the list? (Recall that comparisons mean item comparisons, not index comparisons.) (1, 2)
-
2
-
57
-
88
-
70
-
135
Write a program to test the function you designed.
Note: Have the function,seqOrdSearch, return -1 if the item is not found in the list. (return the index of the item if found).
![```cpp
template<class elemType>
int seqOrdSearch(const elemType list[], int listLength, const elemType& item)
{
int loc;
bool found = false;
loc = 0;
while(loc < listLength && !found)
if (list[loc] == item)
found = true;
else
loc++;
if(found)
return loc;
else
return -1; // return -1 if item not found
} //end seqOrdSearch
```
### Explanation
This C++ code snippet implements a sequential search algorithm for an ordered list. It searches for a specified item (`item`) in a list of elements (`list`) of a given length (`listLength`).
- **Template Function:** The function `seqOrdSearch` is a template function, meaning it can handle any data type defined as `elemType`.
- **Function Parameters:**
- `const elemType list[]`: An array of elements to search through.
- `int listLength`: The number of elements in the array.
- `const elemType& item`: The item to be searched for in the array.
- **Variables:**
- `int loc`: Used to traverse the array, initialized to 0.
- `bool found`: A flag to indicate if the item is found, initialized to `false`.
- **Logic:**
- The `while` loop iterates through the list as long as `loc` is less than `listLength` and `found` is `false`.
- During each iteration, it checks if the current element `list[loc]` is equal to `item`. If so, `found` is set to `true`.
- If the item is found, the function returns the index `loc`. Otherwise, it increments `loc` and continues the search.
- If the loop completes without finding the item, the function returns `-1`, indicating that the item is not present in the list.
This algorithm is efficient for small to moderately sized ordered lists. However, for larger datasets, other search algorithms like binary search might be more efficient.](https://content.bartleby.com/qna-images/question/6acd07aa-a856-40a6-adeb-f0b8d4e56f5a/056f3acb-7cee-45c6-b722-86c040015dae/v1lzf2x_thumbnail.png)
Transcribed Image Text:```cpp
template<class elemType>
int seqOrdSearch(const elemType list[], int listLength, const elemType& item)
{
int loc;
bool found = false;
loc = 0;
while(loc < listLength && !found)
if (list[loc] == item)
found = true;
else
loc++;
if(found)
return loc;
else
return -1; // return -1 if item not found
} //end seqOrdSearch
```
### Explanation
This C++ code snippet implements a sequential search algorithm for an ordered list. It searches for a specified item (`item`) in a list of elements (`list`) of a given length (`listLength`).
- **Template Function:** The function `seqOrdSearch` is a template function, meaning it can handle any data type defined as `elemType`.
- **Function Parameters:**
- `const elemType list[]`: An array of elements to search through.
- `int listLength`: The number of elements in the array.
- `const elemType& item`: The item to be searched for in the array.
- **Variables:**
- `int loc`: Used to traverse the array, initialized to 0.
- `bool found`: A flag to indicate if the item is found, initialized to `false`.
- **Logic:**
- The `while` loop iterates through the list as long as `loc` is less than `listLength` and `found` is `false`.
- During each iteration, it checks if the current element `list[loc]` is equal to `item`. If so, `found` is set to `true`.
- If the item is found, the function returns the index `loc`. Otherwise, it increments `loc` and continues the search.
- If the loop completes without finding the item, the function returns `-1`, indicating that the item is not present in the list.
This algorithm is efficient for small to moderately sized ordered lists. However, for larger datasets, other search algorithms like binary search might be more efficient.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Hold on to do asap.arrow_forward2. A positive integer is perfect if it equals the sum of all of its factors, excluding the number itself. Using a list comprehension, define a function perfects :: Int -> [Int] that returns the list of all perfect numbers up to a given limit. For example: > perfects 500 [6,28,496] Many variations of this exercise are possible: o A number which is less than the sum of its proper divisors is called abundant. o A number which is greater than the sum of its proper divisions is called deficient. o A number for which the sum of all its divisors (including itself) is greater than the sum of the divisors of any smaller number is called highly abundant. For each of these variations, write a function which finds all the numbers with the stated property below a given number.arrow_forwardUsing the function provided, write a function that decides whether two lists are "identical". That is, if cycling by any number N makes the lists identical, the function should return True, otherwise, it should return False. for example identical([1,2,3,4,5], [5,4,1,2,3,]) should give True and identical([1,2,3,4,5], [1,2,3,5,4]) should give False.arrow_forward
- Sorting ascends if a list is empty or all items except the last are less than or equal to their successors. Define isSorted to return True if a list is sorted and False otherwise. (Hint: Loop through a list of length 2 or more and compare pairs of items left to right, returning False if the first item is greater.) Main function output: def main(): Print (isSorted(lyst)) lyst = [1] print(isSorted(lyst)) Lyst list(range(10)) print(isSorted(lyst)) lyst[9]=3 print(isSorted(lyst)) %3D True Falsearrow_forwardUsing the function provided, write a function that decides whether two lists are "identical". That is, if cycling by any number N makes the lists identical, the function should return True, otherwise, it should return False. for example identical([1,2,3,4,5], [5,4,1,2,3,]) should give True and identical([1,2,3,4,5,6], [1,2,3,5,4]) should give False.arrow_forwardPython helparrow_forward
- Write a function named merge-sort in Scheme that takes a list of integers as input and returns a sorted list. (Hint: Use the merge and split-list methods that you just wrote. You will have to cut-and-paste the code into your answer.) For example, (merge-sort '(5 3 1 2 4)) should return '(1 2 3 4 5) • (every-other '()) should return '()arrow_forward3 2 5 6 4 1 In each blank, enter the list after the given iteration. Put one space between each pair of numbers. Do not put commas or any other character in your response besides the numbers and spaces. Do not put a space at the beginning or end of your response. What will the list be after the first iteration of selection sort? incorrect What will the list be after the second iteration of selection sort? incorrect What will the list be after the third iteration of selection sort? incorrect What will the list be after the fourth iteration of selection sort?arrow_forwardhave been given a singly linked list of integers. Write a function that returns the index/position of integer data denoted by 'N' (if it exists). Return -1 otherwise. Note :Assume that the Indexing for the singly linked list always starts from 0.Input format :The first line contains an Integer 'T' which denotes the number of test cases. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line contains the integer value 'N'. It denotes the data to be searched in the given singly linked list. Remember/Consider :While specifying the list elements for input, -1 indicates the end of the singly linked list and hence -1 would never be a list element. Output format :For each test case, return the index/position of 'N' in the singly linked list. Return -1, otherwise. Output for every test case will be printed in a separate line. Note:You do not need to print anything; it has already been taken care of. Just implement…arrow_forward
- This is for Python version 3.8.5 and please include pseudocode. nums = [8, 15, 6, 17, 33, 20, 14, 9, 12]Write Python code that: uses a function to print the size of the list named nums uses a function to print the largest number in nums prints the element 14 by using its index makes a slice named subnums containing only 33, 20, and 14arrow_forwardWrite a version of the sequential search algorithm that can be used to search a sorted list. Write a program to test the sequential search algorithm. Use either the function bubbleSort or insertionSort to sort the list before the search. Your program should prompt the user to enter 10 digits and then prompt the user to enter a digit to search - if the list contains this digit, output its position to the console: x is found at position y If the digit is not contained in the list, output the following: x is not in the listarrow_forwardI need to find the asnwer to this question I do have the code from the lab before I just need to know how to add that one to the other code. 3.9 LAB: Smallest and largest numbers in a list using min and max built-in functions NOTE1: Please make sure you complete previous lab successfully before you proceed with this Lab assignment NOTE2: Make sure to copy and past your code from previous lab into here and update it to complete this lab Write a program that reads a list of integers into a list as long as the integers are greater than zero, then outputs the list values and the smallest and largest integers in the list using min and max built-in functions . Ex: If the input is: 10 5 3 21 2 -6 the output is: [10, 5, 3, 21, 2] 2 and 21 This is the code of the lab before that wants me to add to it.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education