Skip to content

Commit

Permalink
Added implementation of linear search in C,C++,Java and Python (Asiat…
Browse files Browse the repository at this point in the history
  • Loading branch information
GnikDroy authored and tstreamDOTh committed Sep 30, 2018
1 parent f53eb91 commit 2655cfd
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Searching/Linear_Search/C++/LinearSearch.cpp
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);

}
24 changes: 24 additions & 0 deletions Searching/Linear_Search/C/LinearSearch.c
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));

}
34 changes: 34 additions & 0 deletions Searching/Linear_Search/Java/LinearSearch.java
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));
}
}
16 changes: 16 additions & 0 deletions Searching/Linear_Search/Python/LinearSearch.py
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))
26 changes: 26 additions & 0 deletions Searching/Linear_Search/README.md
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.

0 comments on commit 2655cfd

Please sign in to comment.