forked from Asiatik/codezilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added implementation of linear search in C,C++,Java and Python (Asiat…
- Loading branch information
1 parent
f53eb91
commit 2655cfd
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
#include <iostream> | ||
|
||
/** | ||
* Linear search algorithm | ||
* This is the exact implementation of the pseudocode written in README. | ||
* Notice that even if the array list contains multiple items, | ||
* it will return only one index. | ||
* Thus this algorithm is well suited to find if a item is present. | ||
* And not for finding the exact index if multiple items are present. | ||
* With C++ you have to be careful of integer overflows. | ||
* So we must make sure that the length of itemList is below that of int. | ||
*/ | ||
|
||
//Notice that itemList is passed by pointer. No unnecessary copy takes place. | ||
int linearSearch(int itemList[],int itemListSize,int item) | ||
{ | ||
int index=0; | ||
while (index < itemListSize) | ||
{ | ||
if (itemList[index] == item) | ||
{ | ||
return index; | ||
} | ||
index++; | ||
} | ||
return -1; | ||
|
||
} | ||
|
||
//Simple demonstration for the algorithm | ||
int main(int argc,char* argv[]) | ||
{ | ||
int arr[] = {1,2,3,4,5}; | ||
std::cout << linearSearch(arr,sizeof(arr)/sizeof(arr[0]),3) << std::endl; | ||
std::cout << linearSearch(arr,sizeof(arr)/sizeof(arr[0]),0); | ||
|
||
} |
This file contains 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,24 @@ | ||
#include "stdio.h" | ||
|
||
int linearSearch(int itemList[],int itemListSize,int item) | ||
{ | ||
int index=0; | ||
while (index < itemListSize) | ||
{ | ||
if (itemList[index] == item) | ||
{ | ||
return index; | ||
} | ||
index++; | ||
} | ||
return -1; | ||
|
||
} | ||
//Simple demonstration for the algorithm | ||
int main(int argc,char* argv[]) | ||
{ | ||
int arr[] = {1,2,3,4,5}; | ||
printf("%d\n",linearSearch(arr,sizeof(arr)/sizeof(arr[0]),3)); | ||
printf("%d",linearSearch(arr,sizeof(arr)/sizeof(arr[0]),0)); | ||
|
||
} |
This file contains 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,34 @@ | ||
public class LinearSearch{ | ||
|
||
/** | ||
* Linear search algorithm | ||
* This is the exact implementation of the pseudocode written in README. | ||
* Notice that even if the array list contains multiple items, | ||
* it will return only one index. | ||
* Thus this algorithm is well suited to find if a item is present. | ||
* And not for finding the exact index if multiple items are present. | ||
* | ||
* With java you have to be careful of integer overflows. | ||
* So we must make sure that the length of itemList is below that of int. | ||
* | ||
* @param itemList List of all the sorted items | ||
* @param item The item to search for | ||
* @return Index of the item | ||
*/ | ||
public static int linearSearch(int[] itemList,int item){ | ||
int index=0; | ||
while ( index < itemList.length ){ | ||
if ( itemList[index] == item ){ | ||
return index; | ||
} | ||
index++; | ||
} | ||
return -1; | ||
} | ||
|
||
//Simple desmonstration of the function | ||
public static void main(String args[]){ | ||
System.out.println(linearSearch(new int[]{1,2,3,4,5},6)); | ||
System.out.println(linearSearch(new int[]{1,2,3,4,5},3)); | ||
} | ||
} |
This file contains 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,16 @@ | ||
""" | ||
This is a simple Linear search algorithm for python. | ||
This is the exact implementation of the pseudocode written in README. | ||
""" | ||
def linear_search(item_list,item): | ||
index=0 | ||
while index < len(item_list): | ||
if item_list[index] == item: | ||
return index | ||
index+=1 | ||
return None | ||
|
||
#Simple demonstration of the function | ||
if __name__=='__main__': | ||
print (linear_search([1,3,5,7],3)) | ||
print (linear_search([1,2,5,7,97],0)) |
This file contains 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,26 @@ | ||
# Linear Search | ||
|
||
>Worst Case Time Complexity: O(n) | ||
>Space Complexity: O(1) | ||
[Wikipedia Entry](https://en.wikipedia.org/wiki/Linear_search) | ||
|
||
## Pseudocode | ||
|
||
A is the array, n is the size of array and T is the item | ||
|
||
|
||
function linear_search(A, n, T): | ||
i=0 | ||
while i < n: | ||
if A[i] == T: | ||
return i | ||
i=i+1 | ||
return unsuccessful | ||
|
||
Notice that even if the array list contains multiple items, it will return only one index. | ||
Thus this algorithm is well suited to find if a item is present. | ||
And not for finding the exact index if multiple items are present. | ||
|
||
This algorithm is rarely used since there are always better options like binary search or hash tables. |