Straight sequential search algorithm

Also, note that the problem statement says to initialize the data to the sample given so you should not be using arr[9] = target. Your code should work for target = 167010 though.
  • #1
Sumaya
29
0

Homework Statement



A company has a number of employees whose records, for simplicity, contain:

Employee number Employee name

Write separate programs, each of which searches for a key whose value is to be input, using the sequential search variations given below. Let each program initialize the data to the sample given.

a) a straight sequential search algorithm.
b) a sentinel search algorithm.
c) a probability search algorithm.


i didn't know what the underline means ?
is it required to use the hash function or my solution for part a is right ?

Homework Equations



emp # emp name

121267 Ali Mohammed
045128 Samir Hasan
379452 Majid Sabri
160252 Tawfiq Faris
378845 Basil Ali
070918 Saddiq mahmod
166702 Husain Khalid
572556 Jamil Yasir
167010 Zaid Amr



The Attempt at a Solution



Code:
#include<iostream>
using namespace std;

const int arraySize = 1000;
struct employee
{
	char name[arraySize];
	int number;
};

int main()
{


	employee arr[9]={"Ali Mohammed",121267,"Samir Hasan",45128,"Majid Sabri",379452,"Tawfiq Faris",160252,"Basil Ali",378845,"Saddiq mahmod",70918,"Husain Khalid",166702,"Jamil Yasir",572556,"Zaid Amr",167010};
	int size=17;
	int target,flag,n=9,i;
		cin>>target;
		i=0;

	while(i<n)
	{
		if(target==arr[i].number)
		{
			flag=1;
		    break;
		}
		i++;
	}
	if(flag)
		cout<<"found"<<"   "<<arr[i].name<<endl;
	else
		cout<<"not found "<<i<<endl;

	return 0;
}


thanx alot
 
Physics news on Phys.org
  • #2
Sumaya said:
I didn't know what the underline means ?
You mean the fact it was underlined, or what the underlined phrase means? The problem statement wants you to write 3 different programs.

Sumaya said:
Is it required to use the hash function or my solution for part a is right?
A sequential search does not use a hash so part a looks OK, but you should initialize flag = 0 when declaring it. Also you may want to use typedef for the structure:

Code:
typedef struct
{
	char name[arraySize];
	int number;
}employee;
 
  • #3
rcgldr said:
You mean the fact it was underlined, or what the underlined phrase means? The problem statement wants you to write 3 different programs.

A sequential search does not use a hash so part a looks OK, but you should initialize flag = 0 when declaring it. Also you may want to use typedef for the structure:

Code:
typedef struct
{
	char name[arraySize];
	int number;
}employee;

thanks for your notes

thinking ... if i want to use hash function is it possible, that i find an element by using given index to go directly to element arragain ... thnx a lot this is my try for part b) a sentinel search algorithm.
Code:
#include "stdafx.h"
#include<iostream>
using namespace std;
const int arraySize = 1000;
typedef struct
{
	char name[arraySize];
	int number;
}employee;

int main()
{	employee arr[10]={"Ali Mohammed",121267,"Samir Hasan",45128,"Majid Sabri",379452,"Tawfiq Faris",160252,"Basil Ali",378845,"Saddiq mahmod",70918,"Husain Khalid",166702,"Jamil Yasir",572556,"Zaid Amr",167010};
	
	int target,flag=0,n=10,i;
		cin>>target;
		arr[9].number=target;
		for(i=0;target!=arr[i].number;i++)
			;
		if(i<9)
			cout<<"found   "<<arr[i].name<<endl;
		else
			cout<<"not found"<<endl;	return 0;
}
c) a probability search algorithm.

Code:
#include<iostream>
using namespace std;
//int hashfunction(char[],int)
const int arraySize = 1000;
typedef struct
{
	char name[arraySize];
	int number;
}employee;

int main()
{

const int n=9;
	employee arr[9]={"Ali Mohammed",121267,"Samir Hasan",45128,"Majid Sabri",379452,"Tawfiq Faris",160252,"Basil Ali",378845,"Saddiq mahmod",70918,"Husain Khalid",166702,"Jamil Yasir",572556,"Zaid Amr",167010};
	
	int target,flag=0,i=0;
		cin>>target;
		if(target<=arr[n-1].number)
		{
			while(target>=arr[i].number)
			{
				if (target==arr[i].number)
				{
					cout<<"found   "<<arr[i].name<<endl;
					flag=1;
					break;
				}
				i++;
			}
		}

if(flag==0)
cout<<"not found"<<endl;

return 0;
}
i do have wrong output with part c
where is my mistake ?
again thank you
 
Last edited:
  • #4
Sumaya said:
i do have wrong output with part c
For part c, you need code to handle the case where target < arr.number. Are the employee structures supposed to be sorted by number before you use the method in part c?
 
  • #5
Where is your sentinel value in part b?
 

What is a straight sequential search algorithm?

A straight sequential search algorithm is a simple method for searching through a list or array of data to find a specific value. It works by starting at the beginning of the list and checking each element sequentially until the desired value is found or the end of the list is reached.

How does a straight sequential search algorithm work?

The algorithm begins by comparing the first element in the list to the desired value. If they match, the search is complete. If not, it moves on to the next element and repeats the process until a match is found or the end of the list is reached.

What is the efficiency of a straight sequential search algorithm?

The efficiency of a straight sequential search algorithm is O(n), where n is the number of elements in the list. This means that in the worst case scenario, the algorithm will have to check every element in the list before finding the desired value.

When is a straight sequential search algorithm useful?

A straight sequential search algorithm is useful when searching through small or unsorted lists. It is also a good choice when the data is not expected to be in any particular order, as it does not require the data to be pre-sorted.

What are the limitations of a straight sequential search algorithm?

One limitation of a straight sequential search algorithm is that it can be slow when searching through large lists. This is because it has to check each element in the list one by one. Additionally, if the desired value is near the end of the list, it will take longer to find compared to if it were near the beginning.

Back
Top