-
Notifications
You must be signed in to change notification settings - Fork 20k
Linear Search Algorithm in Java #6277
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
Closed
farheen-shaikh530
wants to merge
3
commits into
TheAlgorithms:master
from
farheen-shaikh530:add-linear-search
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* LinearSearch.java | ||
* This program implements the Linear Search algorithm. | ||
* Time Complexity: O(n) | ||
*/ | ||
|
||
public class LinearSearch { | ||
|
||
public static int linearSearch(int[] arr, int target) { | ||
// Traverse each element in the array | ||
for (int i = 0; i < arr.length; i++) { | ||
if (arr[i] == target) { | ||
return i; // Found the target at index i | ||
} | ||
} | ||
return -1; // Target not found | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] numbers = {5, 8, 12, 20, 35}; | ||
int target = 20; | ||
|
||
int index = linearSearch(numbers, target); | ||
if (index != -1) { | ||
System.out.println("Target " + target + " found at index: " + index); | ||
} else { | ||
System.out.println("Target " + target + " not found in the array."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* BubbleSort.java | ||
* This program implements the Bubble Sort algorithm. | ||
* Time Complexity: O(n^2) | ||
*/ | ||
|
||
public class BubbleSort { | ||
|
||
public static void bubbleSort(int[] arr) { | ||
int n = arr.length; | ||
boolean swapped; | ||
for (int i = 0; i < n - 1; i++) { | ||
swapped = false; | ||
|
||
for (int j = 0; j < n - i - 1; j++) { | ||
if (arr[j] > arr[j + 1]) { | ||
// swap arr[j] and arr[j+1] | ||
int temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
|
||
swapped = true; | ||
} | ||
} | ||
|
||
// If no two elements were swapped, array is sorted | ||
if (!swapped) | ||
break; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr = {64, 25, 12, 22, 11}; | ||
bubbleSort(arr); | ||
System.out.println("Sorted array:"); | ||
for (int num : arr) { | ||
System.out.print(num + " "); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* InsertionSort.java | ||
* This program implements the Insertion Sort algorithm. | ||
* Time Complexity: O(n^2) in worst case, O(n) in best case (already sorted). | ||
*/ | ||
|
||
public class InsertionSort { | ||
|
||
public static void insertionSort(int[] arr) { | ||
// Loop from the second element to the end | ||
for (int i = 1; i < arr.length; i++) { | ||
int key = arr[i]; // Store the current element to be inserted | ||
int j = i - 1; | ||
|
||
// Move elements of arr[0..i-1], that are greater than key, | ||
// to one position ahead of their current position | ||
while (j >= 0 && arr[j] > key) { | ||
arr[j + 1] = arr[j]; // Shift element to the right | ||
j = j - 1; | ||
} | ||
arr[j + 1] = key; // Insert the key into its correct position | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr = {29, 10, 14, 37, 13}; | ||
insertionSort(arr); | ||
System.out.println("Sorted array:"); | ||
for (int num : arr) { | ||
System.out.print(num + " "); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already implemented in https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LinearSearch.java