Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Jump Search/jump_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the elements in the sorted order: ";
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int d;
cout<<"Enter the element to search: ";
cin>>d;
int x,i;
cout<<"Enter the skip count: (Preferred: "<<int(sqrt(n))<<" )";
cin>>x;
for(i=0;i<n;i+=x){
if(a[i+x]>d){
break;
}
}
for(int t=i;t<n;t++){
if(a[t]==d){
cout<<"Element was found at position: "<<t;
break;
}
else if(t==n-1 && a[t]!=d){
cout<<"Element was not found!"<<endl;
}
}
}
3 changes: 3 additions & 0 deletions Jump Search/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h3> Jump Search </h3>
Jump Search is a faster algorithm than Binary search. It is just a modified form of binary search where a small range of numbers are found in which the desired number lies by jumping some indexes in between. Then the desired index is found out by using linear search in that particular small range. The number of indices jumped is preferred to be the square root of the length of array.<br>
Time Complexity: O(n/m + (m-1)) where n is the length of array and m is the jump size. <br>