Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LinearSearchResursive.java and update README.md #356

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/java/LinearSearchRecursive.java.md">
<img align="center" height="25" src="./logos/java.svg" />
</a>
</td>
<td> <!-- Python -->
Expand Down Expand Up @@ -420,13 +420,13 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- C++ -->
<a href="./CONTRIBUTING.md">
<a href="./src/java/CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/java/SentinelLinearSearch.java.md">
<img align="center" height="25" src="./logos/java.svg" />
</a>
</td>
<td> <!-- Python -->
Expand Down
17 changes: 17 additions & 0 deletions src/java/LinearSearchRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class LinearSearchRecursive {
public static void main(String[] args) {
int[] arr = {10,20,50,60,30,60,70,80,22,56,2,7,9,0,44};
System.out.println(LinearRecursive(arr, 0, 9));
System.out.println(LinearRecursive(arr, 0, 45));
}

public static int LinearRecursive(int[] arr, int i, int key){

if(i<arr.length){
if(arr[i] == key) return i;
else return LinearRecursive(arr, i+1, key);
}

return -1;
}
}
32 changes: 32 additions & 0 deletions src/java/SentinelLinearSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class SentinelLinearSearch {
public static void main(String[] args){
int[] arr = {10,20,50,60,30,60,70,80,22,56,2,7,9,0,44};
System.out.println(LinearSentinel(arr,70));
System.out.println(LinearSentinel(arr,45));
}

public static int LinearSentinel(int[] arr, int key){

int n = arr.length;

// Storing last element.
int last = arr[n-1];

// Adding key at end index + 1
arr[n-1] = key;

int i = 0;
for(i = 0; i<n;i++){
if(arr[i] == key) break;
}

// Restoring the last element
arr[n-1] = last;

// Returning index when found.
if((i<n-1) || arr[n-1] == key){ return i;}

// Returning -1 if not found
else return -1;
}
}
Loading