Q:

Jump Search Implementation using C++

belongs to collection: C++ programs on various topics

0

Jump Search Implementation using C++

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Implementation of Jump Search using C++

//Implementation of jump search using c++

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int jumpSearchProg(vector <int> arr, int noToSearch, int ArrayLim)
{
	int previous = 0;
	int step = sqrt(ArrayLim);
	//Step to skip elements for jumping

	while (arr[min(step,ArrayLim)-1] < noToSearch)
	{
		previous = step;
		step += sqrt(ArrayLim);
		if(previous >= ArrayLim) return -1;
	}

	/*Applying linear Search and starting from the previous elements*/
	while (arr[previous] < noToSearch)
	{
		previous++;
	/*If element has not found yet then it means element is not present in the array*/
		if (previous == min(step, ArrayLim)) return -1;
	}
	// if we found the element then

	if (arr[previous] == noToSearch)
		return previous;

	return -1;
}

//Start of main
int main()
{
	int n,NoToSr;
	cout<<"Enter the length of the array:"<<endl;
	cin>>n;
	vector<int> arr(n);

	cout<<"Enter the elements of the array"<<endl;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}

	cout<<"Enter the number to be searched:"<<endl;
	cin>>NoToSr;

	//function calling
	int result = jumpSearchProg(arr, NoToSr, n);

	//displayin foud number
	cout<<"Number = "<<NoToSr<<"is found at index = "<<result<<endl;
	return 0;
}

Output

Jump search implementation using C++ - Output

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to check if a number is power of 2 or not in C... >>
<< Implementation of Round Robin CPU Scheduling algor...