├── .gitignore
├── c-linear-search
├── README.md
├── c-linear-search-2.c
├── c-linear-search.c
├── c-linear-search-3.c
└── c-linear-search-4.c
├── cpp-linear-search
├── README.md
├── cpp-linear-search-3.cpp
├── cpp-linear-search.cpp
└── cpp-linear-search-2.cpp
├── nim-linear-search
├── README.md
└── nim-linear-search.nim
├── php-linear-search
├── README.md
└── php-linear-search.php
├── dart-linear-search
├── README.md
└── dart-linear-search.dart
├── julia-linear-search
├── README.md
└── julia-linear-search.jl
├── lisp-linear-search
├── README.md
└── lisp-linear-search.lisp
├── ruby-linear-search
├── README.md
├── ruby-linear-search-2.rb
├── ruby-linear-search.rb
└── ruby-linear-search-3.rb
├── rust-linear-search
├── README.md
└── rust-linear-search.rs
├── scala-linear-search
├── README.md
├── scala-linear-search.scala
└── scala-linear-search2.scala
├── swift-linear-search
├── README.md
└── swift-linear-search.swift
├── golang-linear-search
├── README.md
└── golang-linear-search.go
├── groovy-linear-search
├── README.md
└── groovy-linear-search.groovy
├── haskell-linear-search
├── README.md
├── linear-search-3.hs
├── haskell-linear-search.hs
└── haskell-Linear-Search-2.hs
├── kotlin-linear-search
├── README.md
├── kotlin-linear-search.kt
└── kotlin-linear-search1.kt
├── turing-linear-search
├── README.md
└── turing-linear-search.t
├── elm-linear-search
├── .gitignore
├── elm.json
├── README.md
└── Elm_Linear_Search.elm
├── shell-script-linear-search
├── README.md
└── shell-script-linear-search.sh
├── assembly-linear-search
├── README.md
└── LIN.ASM
├── prolog-linear-search
├── README.md
└── prolog-linear-search.pl
├── python3-linear-search
├── README.md
├── python3-linear-search-3.py
├── python3-linear-search-4.py
├── python3-linear-search-5.py
├── python3-linear-search.py
├── python3-linear-search-2.py
└── python3-linear-search-6.py
├── perl-linear-search
├── README.md
└── perl-linear-search.pl
├── python-linear-search
├── README.md
├── python-linear-search.py
└── python-linear-search-2.py
├── sml-linear-search
├── README.md
└── linsearch.sml
├── CoffeeScript-Linear-Search
├── README.md
└── CoffeeScript-Linear-Search.coffee
├── batch-script-linear-search
├── README.md
└── batch-script-linear-search.bat
├── csharp-linear-search
├── README.md
├── csharp-linear-search.cs
└── csharp-linear-search-2.cs
├── lua-linear-search
├── README.md
└── lua-linear-search.lua
├── java-linear-search
├── java-linear-search-13.java
├── java-linear-search-9.java
├── JavaLinearSearch3.java
├── java-linear-search-10.java
├── JAVA-linearSearch3.java
├── README.md
├── java-linear-search-7.java
├── java-linear-search-8.java
├── Java-Linear-Search-4.java
├── java-linear-search-5.java
├── java-linear-serach-3.java
├── java-linear-search-3.java
├── java-linear-search-12.java
├── java-linear-search.java
├── java-linear-search-2.java
└── Java_Linear_Search_11.java
├── bash-Linear-Search
├── bash-linear-search.sh
├── linsearch
└── README.md
├── javascript-linear-search
├── javascript-linear-search.js
├── README.md
├── javascript-Linear-Search-2.js
└── javascript-linear-search-2.js
├── C-linear-search
├── linearSearch.c
└── linear-search-1.c
├── fsharp-linear-search
├── README.md
└── fsharp-linear-search.fsx
├── LICENSE.md
├── CONTRIBUTING.md
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
--------------------------------------------------------------------------------
/c-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # c-linear-search
2 | *Example Linear Search in C*
3 |
--------------------------------------------------------------------------------
/cpp-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # cpp-linear-search
2 | *Example Linear Search in Cpp*
3 |
--------------------------------------------------------------------------------
/nim-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # nim-linear-search
2 | *Example Linear Search in Nim*
3 |
--------------------------------------------------------------------------------
/php-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # php-linear-search
2 | *Example Linear Search in Php*
3 |
--------------------------------------------------------------------------------
/dart-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # dart-linear-search
2 | *Example Linear Search in Dart*
3 |
--------------------------------------------------------------------------------
/julia-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # julia-linear-search
2 | *Example Linear Search in julia*
3 |
--------------------------------------------------------------------------------
/lisp-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # lisp-linear-search
2 | *Example Linear Search in Lisp*
3 |
--------------------------------------------------------------------------------
/ruby-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # ruby-linear-search
2 | *Example Linear Search in Ruby*
3 |
--------------------------------------------------------------------------------
/rust-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # rust-linear-search
2 | *Example Linear Search in Rust*
3 |
--------------------------------------------------------------------------------
/scala-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # scala-linear-search
2 | *Example Linear Search in Scala*
3 |
--------------------------------------------------------------------------------
/swift-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # swift-linear-search
2 | *Example Linear Search in Swift*
3 |
--------------------------------------------------------------------------------
/golang-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # golang-linear-search
2 | *Example Linear Search in Golang*
3 |
--------------------------------------------------------------------------------
/groovy-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # groovy-linear-search
2 | *Example Linear Search in Groovy*
3 |
--------------------------------------------------------------------------------
/haskell-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # haskell-linear-search
2 | *Example Linear Search in Haskell*
3 |
--------------------------------------------------------------------------------
/kotlin-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # kotlin-linear-search
2 | *Example Linear Search in Kotlin*
3 |
--------------------------------------------------------------------------------
/turing-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # turing-linear-search
2 | *Example Linear Search in Turing*
3 |
--------------------------------------------------------------------------------
/elm-linear-search/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .idea/
3 | node_modules/
4 | elm-stuff/
5 | package-lock.json
6 |
--------------------------------------------------------------------------------
/shell-script-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # shell-script-linear-search
2 | *Example Linear Search in Shell script*
3 |
--------------------------------------------------------------------------------
/assembly-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # Assembly(8086 microprocessor) - linear-search
2 |
3 | Example Linear Search in Assembly
4 |
--------------------------------------------------------------------------------
/prolog-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # prolog-linear-search
2 | *Example Linear Search in Prolog*
3 | - gprolog
4 | - consult('prolog-linear-search.pl').
5 |
--------------------------------------------------------------------------------
/ruby-linear-search/ruby-linear-search-2.rb:
--------------------------------------------------------------------------------
1 | def linearsearch(needle, haystack)
2 | haystack.index(needle)
3 | end
4 |
5 | puts linearsearch(1, [1,2,3,4,5,6])
6 |
--------------------------------------------------------------------------------
/python3-linear-search/README.md:
--------------------------------------------------------------------------------
1 | This code is a simple interactive linear search code written in Python 3.7.
2 | User has to simply run the code and it will interact with the user and tell whether element found or not.
3 |
--------------------------------------------------------------------------------
/groovy-linear-search/groovy-linear-search.groovy:
--------------------------------------------------------------------------------
1 | def nums = [1, 2, 3, 8, 9, 12]
2 | def target = 2
3 |
4 | if (nums.indexOf(target) >= 0) {
5 | println "Successful Search!"
6 | } else {
7 | println "Not in the array."
8 | }
--------------------------------------------------------------------------------
/ruby-linear-search/ruby-linear-search.rb:
--------------------------------------------------------------------------------
1 | def linear_search (arr, num)
2 | for i in arr
3 | if(i == num)
4 | return i
5 | end
6 | end
7 | return -1
8 | end
9 | arr = [1,2,34,77]
10 | print linear_search(arr,7)
11 |
--------------------------------------------------------------------------------
/perl-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # Linear Search in perl
2 |
3 | Example linear search code in perl language.
4 |
5 | ### Run
6 |
7 | ```bash
8 | $ ./perl perl-linear-search.pl
9 | ```
10 |
11 |
12 | Pay attention to the way certain shells split words and parse input!
13 |
--------------------------------------------------------------------------------
/lisp-linear-search/lisp-linear-search.lisp:
--------------------------------------------------------------------------------
1 | ; linear search lisp program
2 |
3 | (defun linear-search (list item)
4 | (loop for pos from 0 and element in list
5 | when (eql item element)
6 | do (return pos)))
7 |
8 | (princ (linear-search '(21 72 36 94 45) 94))
9 |
--------------------------------------------------------------------------------
/python-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # python-linear-search
2 | *Example Linear Search in Python*
3 | # python-linear-search
4 | Enter the size 'n' of the list you want to search, followed by inputing the space seperated elements
5 | and then enter the element to search and the output will be the index of the element between 0 to n-1
--------------------------------------------------------------------------------
/ruby-linear-search/ruby-linear-search-3.rb:
--------------------------------------------------------------------------------
1 | def linear_search(target, array)
2 | counter = 0
3 |
4 | while counter < array.length
5 | if array[counter] == target
6 | return counter
7 | else
8 | counter += 1
9 | end
10 | end
11 | return nil
12 | end
13 |
14 | arr = [1,2,3,4]
15 | print linear_search(arr,2)
16 |
--------------------------------------------------------------------------------
/sml-linear-search/README.md:
--------------------------------------------------------------------------------
1 | SML linear search:
2 |
3 | To use this code run linsearch.sml with the command
4 | sml linsearch.sml
5 |
6 | The linsearch function takes a tuple of two arguments - The element to search for and the list of numbers in which to look.Example:
7 | linsearch (4,[2,3,6,2,4,7])
8 | Should return 5 (SML lists are 1-indexed)
9 |
--------------------------------------------------------------------------------
/CoffeeScript-Linear-Search/README.md:
--------------------------------------------------------------------------------
1 | method 1 (without using user prompt)
2 | - to run the code use command "coffee filename.coffee"
3 |
4 | method 2 (using user prompt)
5 | - coffeescript doesn't support user input from command prompt, so to get data from user input, online ides like repl.it can be used or coffeescript code can be ran by browser. Code for this is also commented in the file.
6 |
--------------------------------------------------------------------------------
/haskell-linear-search/linear-search-3.hs:
--------------------------------------------------------------------------------
1 | linearSearch :: [Int] -> Int -> Bool
2 | linearSearch [] key = False
3 | linearSearch (head:tail) key = if head == key then True
4 | else linearSearch tail key
5 |
6 | main = do
7 | let array = [1,2,3,4,6]
8 | print $ linearSearch array 5 --Return false
9 | print $ linearSearch array 6 --Return true
10 |
--------------------------------------------------------------------------------
/prolog-linear-search/prolog-linear-search.pl:
--------------------------------------------------------------------------------
1 | /*Linear Search in Prolog*/
2 | linearSearch([Key|_], Key).
3 |
4 | linearSearch([], _) :- fail.
5 | linearSearch([_|Tail], Key) :- linearSearch(Tail, Key).
6 |
7 | go :-
8 | write('Enter List : '),
9 | read(List),
10 | write('Enter Key : '),
11 | read(Key),
12 | linearSearch(List, Key),
13 | write('Element Found');write('Element not found').
14 |
15 | :-initialization(go).
16 |
--------------------------------------------------------------------------------
/sml-linear-search/linsearch.sml:
--------------------------------------------------------------------------------
1 | fun linsearch(x : int, ls : int list) =
2 | let
3 | val ctr : int ref = ref 0
4 | fun recsearch(x : int, ls : int list) =
5 | case ls of
6 | [i] => if i = x then print(Int.toString(!ctr) ^ "\n") else print("Not Found!\n")
7 | | (h::t) => (ctr := !ctr + 1; if h = x then print(Int.toString(!ctr) ^ "\n") else recsearch (x, t))
8 | in
9 | recsearch(x,ls)
10 | end
11 |
--------------------------------------------------------------------------------
/batch-script-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # batch-script-linear-search
2 | *Example Linear Search in Batch-script*
3 |
4 | ### batch-script-linear-search.bat
5 | Call batch script like below:
6 | ``` bash
7 | batch-script-linear-search.bat
8 | ```
9 | **Argument :**
10 | - **arg1**: List of strings (possible separated by space, semicolon, comma)
11 | *ex. "AA BB CC" or "AA;BB;CC"*
12 | - **arg2**: Target string
13 | *ex. AA*
--------------------------------------------------------------------------------
/elm-linear-search/elm.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "application",
3 | "source-directories": [
4 | "."
5 | ],
6 | "elm-version": "0.19.0",
7 | "dependencies": {
8 | "direct": {
9 | "elm/core": "1.0.2"
10 | },
11 | "indirect": {
12 | "elm/json": "1.1.3"
13 | }
14 | },
15 | "test-dependencies": {
16 | "direct": {},
17 | "indirect": {}
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/python3-linear-search/python3-linear-search-3.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | data_size = int(input("Enter size of data: "))
4 | data = list()
5 |
6 | for i in range(0, data_size):
7 | new_item = int(input("Adding item (%d/%d): " % (i+1, data_size)))
8 | data.append(new_item)
9 | print(data)
10 |
11 | target = int(input("Perform sequential search for: "))
12 | result = [item for item in data if item == target]
13 | print(result)
14 |
--------------------------------------------------------------------------------
/csharp-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # csharp-linear-search
2 | *Example Linear Search in Csharp*
3 | # csharp-linear-search-2
4 | *The code is pretty straight forward. Here are the steps to run the algorithm:*
5 | 1) Run the code in interactive mode
6 | 2) You will need to provide the length of array, elements of array and the value to be searched in array using Linear Search
7 | 3) You will be shown the result message if the element was found or not. That's it. Happy Coding!
8 |
--------------------------------------------------------------------------------
/python3-linear-search/python3-linear-search-4.py:
--------------------------------------------------------------------------------
1 | num = int(input("Type the number of elements: "))
2 |
3 | elements = []
4 | for i in range(num):
5 | elements.append(int(input("Type the " + str(i + 1) + "º element: ")))
6 |
7 | found = False
8 | element = int(input("Type a element to search: "))
9 | for e in elements:
10 | if element == e:
11 | found = True
12 | print("Successful search!")
13 | break
14 |
15 | if not found:
16 | print("Not Found!")
17 |
--------------------------------------------------------------------------------
/lua-linear-search/README.md:
--------------------------------------------------------------------------------
1 | ## Lua Linear Search
2 |
3 | Enter the size of the list you wish to search, followed by the numbers within the list, then the number you wish to search for.
4 |
5 | Run with `lua lua-linear-search.lua`
6 |
7 | Example:
8 | ```
9 | $ lua lua-linear-search.lua
10 | Enter number of elements:
11 | 3
12 | Enter element:
13 | 1
14 | Enter element:
15 | 2
16 | Enter element:
17 | 3
18 | Enter element to find:
19 | 4
20 | Element not found!
21 | ```
--------------------------------------------------------------------------------
/php-linear-search/php-linear-search.php:
--------------------------------------------------------------------------------
1 |
21 |
--------------------------------------------------------------------------------
/python3-linear-search/python3-linear-search-5.py:
--------------------------------------------------------------------------------
1 | n=int(input("Enter size of array : "))
2 | arr=[]
3 | flag=0
4 | print('Enter array elements : ')
5 | for i in range(n):
6 | n=arr.append(int(input()))
7 | key=int(input("Enter element to be searched for : "))
8 | for x in arr:
9 | if(x==key):
10 | print('Found at index : ',arr.index(x))
11 | flag=1
12 | break
13 | else:
14 | flag=0
15 | if(flag==0):
16 | print('Not Found!')
17 |
--------------------------------------------------------------------------------
/scala-linear-search/scala-linear-search.scala:
--------------------------------------------------------------------------------
1 | import util.control.Breaks._
2 |
3 | object LinearSearch {
4 | def main(args: Array[String]) {
5 | var a = List(1,2,3,8,9,12)
6 | val elementToFind = 2
7 |
8 | breakable {
9 | for (i <- a){
10 | if (i == elementToFind) {
11 | println("Successful Search!")
12 | break
13 | }
14 | }
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/haskell-linear-search/haskell-linear-search.hs:
--------------------------------------------------------------------------------
1 | -- Type main in ghci,
2 | -- or call search function with valid inputs
3 |
4 | main = do
5 | let array = [2, 4, 6, 8, 10, 3, 5]
6 | print $ search array 6 -- Returns true
7 |
8 | -- This function recursively checks list
9 | search :: Eq a => [a] -> a -> Bool
10 | search [] _ = False
11 | search (h:t) num = h == num || search t num
12 |
13 | search :: Eq a => [a] -> a -> [Int]
14 | search xs y = [ i | (i,x) <- zip [0..] xs, x==y]
--------------------------------------------------------------------------------
/cpp-linear-search/cpp-linear-search-3.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | void lsearch(int a[],int s,int n)
4 | {
5 | for(int i=0;i>n;
19 | int a[n];
20 | for(i=0;i>a[i];
22 | cout<<"Search element: ";
23 | cin>>s;
24 | lsearch(a,s,n);
25 | }
26 |
--------------------------------------------------------------------------------
/CoffeeScript-Linear-Search/CoffeeScript-Linear-Search.coffee:
--------------------------------------------------------------------------------
1 | #n = prompt 'Enter number of elements'
2 | n = 4 #number of elements in array
3 | a = [1,2,3,4] #elements in array
4 | x = 0
5 | ###
6 | while x < n
7 | a[x] = prompt 'Enter element'
8 | x++
9 | ###
10 | #f = prompt 'Enter element to search'
11 | f = 4 #element to search
12 | x = 0
13 | while x < n
14 | if a[x]==f then break
15 | x++
16 | if x Int {
2 | while true {
3 | if let str = readLine() {
4 | if let num = Int(str) {
5 | return num
6 | } else {
7 | print("\(str) is not a number.")
8 | }
9 | }
10 | }
11 | }
12 |
13 | func linearSearch(in haystack: [Int], for needle: Int) -> Int {
14 | for i in 0 ..< haystack.count {
15 | if haystack[i] == needle {
16 | return i
17 | }
18 | }
19 |
20 | return -1
21 | }
22 |
23 | let haystack = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
24 |
25 | let index = linearSearch(in: haystack, for: 2)
26 | print("2 was found at index \(index)")
--------------------------------------------------------------------------------
/lua-linear-search/lua-linear-search.lua:
--------------------------------------------------------------------------------
1 | elements = {}
2 | print("Enter number of elements:")
3 | number_of_elements = io.read("*number")
4 | for i = 1, number_of_elements do
5 | print("Enter element: ")
6 | element = io.read("*number")
7 | table.insert(elements, element)
8 | end
9 |
10 | print("Enter element to find:")
11 | element_to_find = io.read("*number")
12 | for i = 1, number_of_elements do
13 | if elements[i] == element_to_find then
14 | print("Element found!")
15 | break
16 | end
17 | if i == number_of_elements then
18 | print("Element not found!")
19 | end
20 | end
21 |
--------------------------------------------------------------------------------
/rust-linear-search/rust-linear-search.rs:
--------------------------------------------------------------------------------
1 | enum SearchResult {
2 | Found(i32),
3 | NotFound
4 | }
5 |
6 | use SearchResult::*;
7 |
8 | fn linear_search(items: &[i32], target: i32) -> SearchResult {
9 | for (idx, item) in items.iter().enumerate() {
10 | if *item == target {
11 | return Found(idx as i32);
12 | }
13 | }
14 | NotFound
15 | }
16 |
17 | fn main() {
18 | let items = [1, 3, 5, 7, 12, 454];
19 | if let SearchResult::Found(n) = linear_search(&items, 12) {
20 | println!("Found at {} index :)!", n);
21 | }
22 | else {
23 | println!("Not found :(");
24 | }
25 | }
--------------------------------------------------------------------------------
/assembly-linear-search/LIN.ASM:
--------------------------------------------------------------------------------
1 | DATA SEGMENT
2 | STRING1 DB 11H,22H,33H,44H,55H
3 | MSG1 DB "FOUND$"
4 | MSG2 DB "NOT FOUND$"
5 | SE DB 55H
6 | DATA ENDS
7 |
8 | PRINT MACRO MSG
9 | MOV AH, 09H
10 | LEA DX, MSG
11 | INT 21H
12 | ENDM
13 |
14 | CODE SEGMENT
15 | ASSUME CS:CODE, DS:DATA
16 | START:
17 | MOV AX, DATA
18 | MOV DS, AX
19 | MOV AL, SE
20 | LEA SI, STRING1
21 | MOV CX, 05H
22 |
23 | UP:
24 | MOV BL,[SI]
25 | CMP AL, BL
26 | JZ FO
27 | INC SI
28 | DEC CX
29 | JNZ UP
30 | PRINT MSG2
31 | JMP END1
32 |
33 | FO:
34 | PRINT MSG1
35 |
36 | END1:
37 | MOV AH,4CH
38 | INT 21H
39 | CODE ENDS
40 | END START
41 |
--------------------------------------------------------------------------------
/golang-linear-search/golang-linear-search.go:
--------------------------------------------------------------------------------
1 | package main
2 | import "fmt"
3 | func main() {
4 | var n int;
5 | var fg bool;
6 | fg=false;
7 | fmt.Print("Enter the number of element: ")
8 | fmt.Scanf("%d",&n);
9 | data := make([]int, n, 2*n);
10 | for i :=0; i, key: Int) : Boolean {
6 |
7 | for(x in searchList){
8 | if(x == key){
9 | return true
10 | }
11 | }
12 |
13 | return false
14 | }
15 |
16 | fun main(args : Array) {
17 |
18 | var searchArr: Array = arrayOf(1,3,5,7,9,11)
19 | var searchKey = 3
20 |
21 | var isKeyPresent = linearSearch(searchArr, searchKey);
22 |
23 | when (isKeyPresent){
24 |
25 | true -> println("key found in list")
26 | false -> println("key not found in list")
27 | }
28 | }
29 |
30 |
31 |
--------------------------------------------------------------------------------
/c-linear-search/c-linear-search-2.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int arr[100], i, num, n, c=0, pos;
6 | printf("Enter the array size : ");
7 | scanf("%d", &n);
8 | printf("Enter the Array Elements : ");
9 | for(i=0; i
2 | using namespace std;
3 |
4 | void linearSearch(const int myList[], int key,int num)
5 | {
6 | for (int i = 0; i < num; i++)
7 | {
8 | if (key == myList[i])
9 | {
10 | cout << "Element found" << endl;
11 | return;
12 | }
13 | }
14 | cout<<"Element not found";
15 | }
16 | int main()
17 | {
18 | int num;
19 | cout << "Enter Number of Elements : ";
20 | cin >> num;
21 | int myList[num];
22 | for(int i = 0 ; i < num ;i++)
23 | cin >> myList[i];
24 | cout << "Enter Element to Search : ";
25 | int key;
26 | cin >> key;
27 | linearSearch(myList, key,num);
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/turing-linear-search/turing-linear-search.t:
--------------------------------------------------------------------------------
1 | function linear_search(var arr : array 0 .. * of int, var key : int) : boolean
2 | for i : 0 .. upper(arr)-1
3 | if arr(i) = key then
4 | result true
5 | end if
6 | end for
7 | result false
8 | end linear_search
9 |
10 | var n : int
11 | put "Please enter the number of elements: " ..
12 | get n
13 | var arr : array 0 .. n of int
14 | put "Please enter the array elements: " ..
15 | for i : 0 .. n-1
16 | get arr(i)
17 | end for
18 | put "Enter the key to search for: "..
19 | var key : int
20 | get key
21 | if linear_search(arr, key) then
22 | put "Successful Search"
23 | else
24 | put "Unsuccessful Search"
25 | end if
26 |
--------------------------------------------------------------------------------
/java-linear-search/java-linear-search-13.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class LinearSearch {
4 | public static void main(String[] args) {
5 | Scanner input = new Scanner(System.in);
6 | int n = input.nextInt();
7 | int value = input.nextInt();
8 | int[] array = new int[n];
9 | for (int i = 0; i < n; i++) {
10 | array[i] = input.nextInt();
11 | }
12 | linearSearch(n, array, value);
13 | }
14 |
15 | private static void linearSearch(int n, int[] array, int value) {
16 | for (int i = 0; i < n; i++) {
17 | if (array[i] == value) {
18 | System.out.println(i);
19 | return;
20 | }
21 | }
22 | System.out.println("Not Found!");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/java-linear-search/java-linear-search-9.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class LinearSearch {
4 | public static void main(String[] args) {
5 | Scanner input = new Scanner(System.in);
6 | int n = input.nextInt();
7 | int value = input.nextInt();
8 | int[] array = new int[n];
9 | for (int i = 0; i < n; i++) {
10 | array[i] = input.nextInt();
11 | }
12 | linearSearch(n, array, value);
13 | }
14 |
15 | private static void linearSearch(int n, int[] array, int value) {
16 | for (int i = 0; i < n; i++) {
17 | if (array[i] == value) {
18 | System.out.println(i);
19 | return;
20 | }
21 | }
22 | System.out.println("Not Found!");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/java-linear-search/JavaLinearSearch3.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.List;
3 | import java.util.Optional;
4 |
5 | public class JavaLinearSearch3 {
6 | public Integer linearSearch(List numbers, Integer key ) {
7 | Integer result = null;
8 | Optional number = numbers.stream().filter(n -> n.equals(key)).findFirst();
9 | if (number.isPresent()) {
10 | result = number.get();
11 | }
12 | return result;
13 | }
14 |
15 | public static void main(String[] args) {
16 | JavaLinearSearch3 javaLinearSearch3 = new JavaLinearSearch3();
17 | System.out.println(javaLinearSearch3.linearSearch(Arrays.asList(1,2,3,4,5), 3));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/bash-Linear-Search/bash-linear-search.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | echo -n "Enter size of data: "
4 | read data_size
5 |
6 | for ((i = 0; i < $data_size; i++)); do
7 | printf "Adding item (%d/%d): " $((i+1)) $data_size
8 | read array[$i]
9 | done
10 |
11 | for ((i = 0; i < ${#array[@]}; i++)); do
12 | echo -n ${array[$i]}
13 | if [ $i -lt $((data_size - 1)) ]; then
14 | echo -n ", "
15 | else
16 | echo ""
17 | fi
18 | done
19 |
20 | echo -n "Perform sequential search for: "
21 | read target
22 |
23 | for ((i = 0; i < ${#array[@]}; i++)); do
24 | if [ ${array[$i]} -eq $target ]; then
25 | printf "Item found at: %d\n" $i
26 | exit
27 | fi
28 | done
29 |
30 | echo "Item not found."
31 |
--------------------------------------------------------------------------------
/elm-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # elm-linear-search
2 | *Example Linear Search in ELM*
3 |
4 | ## How to install ELM and its dependencies
5 |
6 | Follow this link for installation of [ELM](https://guide.elm-lang.org/install/)
7 | OR
8 | Follow following steps for Linux
9 | ```bash
10 | wget -O elm https://github.com/elm/compiler/releases/download/0.19.0/binary-for-linux-64-bit.gz
11 | chmod +x elm
12 | mv elm /usr/local/bin/
13 | ```
14 |
15 | We also need a dependency of `run-elm`
16 | ```bash
17 | npm install -g @kachkaev/run-elm
18 | ```
19 |
20 | ## How to execute file
21 |
22 | ```bash
23 | run-elm Elm_Linear_Search.elm one two there two
24 | ```
25 |
26 | > Note: I could not have file name with hyphen in it, so using underscore
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/kotlin-linear-search/kotlin-linear-search1.kt:
--------------------------------------------------------------------------------
1 | package linearSearch;
2 |
3 |
4 | class LinearSearch (searchList: Array) {
5 | var searchList = searchList
6 | fun exists (key: T) : Boolean {
7 |
8 | for(x in this.searchList){
9 | if(x == key){
10 | return true
11 | }
12 | }
13 |
14 | return false
15 | }
16 | }
17 |
18 | fun main(args : Array) {
19 |
20 | var searchArr: Array = arrayOf(1,3,5,7,9,11)
21 | var searchKey = 3
22 |
23 | var isKeyPresent = LinearSearch(searchArr).exists(searchKey);
24 |
25 | when (isKeyPresent){
26 |
27 | true -> println("key found in list")
28 | false -> println("key not found in list")
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/c-linear-search/c-linear-search.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int n,i,x,flag=0;
5 | int a[100100];
6 | scanf("%d",&n); //how many numbers in which you want to search
7 | for(i=0;i );
4 | print "Enter the values: ";
5 | for ( $i =0; $i <$size; ++$i){
6 | my $num = ;
7 | chomp $num;
8 | push @array, $num;
9 | }
10 |
11 | # prompt the user for a search key
12 | print "Enter an integer search key: ";
13 | chomp ( $searchKey = );
14 |
15 | $found = 0; # $found is initially false
16 |
17 | for ( $i = 0; $i < @array && !$found; ++$i ) {
18 |
19 | if ( $array[ $i ] == $searchKey ) {
20 | $index = $i;
21 | $found = 1;
22 | }
23 | }
24 |
25 | if ( $found ) { # $found == 1
26 | print "Found $searchKey at subscript $index \n";
27 | }
28 | else { # $found == 0
29 | print "$searchKey not found \n";
30 | }
31 |
--------------------------------------------------------------------------------
/bash-Linear-Search/README.md:
--------------------------------------------------------------------------------
1 | # Linear Search in bash
2 |
3 | Ignore the `set` line, and the definitions of `log` and `die`; they are fairly
4 | standard boilerplate lines.
5 |
6 | The search itself happens in `search`: we iterate through the array's indices,
7 | stopping when we find the key. If we don't, we return a sentinel value.
8 |
9 | NB This is not likely to be of true use; idiomatic bash very rarely demands
10 | specific indices.
11 |
12 | `main` then sets up the parameters (making sure they were given), calls our
13 | search function, and handles output.
14 |
15 | ### Run
16 |
17 | ```bash
18 | $ ./bash-Linear-Search/linsearch {key} {array}
19 | ```
20 |
21 | - `key` should be a single shell-word
22 | - `array` should be a (possibly empty) list of shell-words
23 |
24 | Pay attention to the way certain shells split words and parse input!
25 |
--------------------------------------------------------------------------------
/c-linear-search/c-linear-search-3.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int array[100], search, c, n;
6 |
7 | printf("Enter the number of elements in array\n");
8 | scanf("%d", &n); /* Taking input for no of elements in array*/
9 |
10 | printf("Enter %d integer(s) :\n", n);
11 |
12 | for (c = 0; c < n; c++)
13 | scanf("%d", &array[c]); /* Taking input for values of array at each index*/
14 |
15 | printf("Enter a number to search\n");
16 | scanf("%d", &search); /* Taking input for element to search*/
17 |
18 | for (c = 0; c < n; c++)
19 | {
20 | if (array[c] == search) /* If element is found */
21 | {
22 | printf("%d is found at location %d.\n", search, c+1);
23 | break;
24 | }
25 | }
26 | if (c == n)
27 | printf("%d isn't found in the array.\n", search);
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/javascript-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # javascript-linear-search
2 |
3 | Make a linear search through an array using javascript and write to the current webpage if the element being searched for was found or not. Prompts are made to the user via the prompt command. User will first define how large of an array to create. User will then define the array to search through and the element to search for. Position found is output in a 0-based index format (i.e. position 3 is the 4th element). Note: this will overwrite all HTML on the page. To run the code simply set the command to an HTML button click command or in the console copy the javascript and type `linear();`
4 |
5 | Example:
6 |
7 | ```javascript
8 | // user will normally enter array data via prompt. This is just to demonstrate.
9 | const array = [0,4,2,6,11,8,3,9,5];
10 | const searchEl = 11;
11 | // output: Element 11 found at position 3
12 | ```
13 |
--------------------------------------------------------------------------------
/C-linear-search/linearSearch.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | int main()
5 | {
6 | int array[100], search, c, n;
7 |
8 | printf("Enter the number of elements in array\n");
9 | scanf("%d", &n); /* Taking input for no of elements in array*/
10 |
11 | printf("Enter %d integer(s) :\n", n);
12 |
13 | for (c = 0; c < n; c++)
14 | scanf("%d", &array[c]); /* Taking input for values of array at each index*/
15 |
16 | printf("Enter a number to search\n");
17 | scanf("%d", &search); /* Taking input for element to search*/
18 |
19 | for (c = 0; c < n; c++)
20 | {
21 | if (array[c] == search) /* If element is found */
22 | {
23 | printf("%d is found at location %d.\n", search, c+1);
24 | break;
25 | }
26 | }
27 | if (c == n)
28 | printf("%d isn't found in the array.\n", search);
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/haskell-linear-search/haskell-Linear-Search-2.hs:
--------------------------------------------------------------------------------
1 | -- tested with GHC 8.4.3
2 |
3 | -- linearSearch takes an array of items, and a key of the same type.
4 | -- If the key is in the array, it will return the key itself.
5 | linearSearch :: Eq a => [a] -> a -> Maybe a
6 | linearSearch [] _ = Nothing
7 | linearSearch (x:xs) key
8 | | x == key = Just x
9 | | otherwise = linearSearch xs key
10 |
11 | -- linearSearch' takes an array of items, and a key of the same type.
12 | -- If the key is in the array, it will return the index of the key.
13 | linearSearch' :: Eq a => [a] -> a -> Maybe Int
14 | linearSearch' (x:xs) key = accSearch (x:xs) key 0
15 | where accSearch :: Eq a => [a] -> a -> Int -> Maybe Int
16 | accSearch [] _ _ = Nothing
17 | accSearch (x:xs) key i
18 | | x == key = Just i
19 | | otherwise = accSearch xs key (i+1)
20 |
21 |
--------------------------------------------------------------------------------
/javascript-linear-search/javascript-Linear-Search-2.js:
--------------------------------------------------------------------------------
1 | function linear () {
2 | let n = parseInt(prompt('Enter the size of an array'))
3 | let a = []
4 | for (let i = 0; i < n; i++) {
5 | a[i] = parseInt(prompt('Current array:\t' + a + '\nEnter array elements'))
6 | }
7 | let k = parseInt(prompt('Current array:\t' + a + '\nEnter the key element to search: '))
8 | for (let i = 0; i < a.length; i++) {
9 | if (k === a[i]) {
10 | // document.body.innerText('');
11 | document.writeln('Element ' + a[i] + ' Found at Position:' + i)
12 | break
13 | } else if (i === (a.length - 1) && k !== a[i]) {
14 | // document.body.innerText('');
15 | document.writeln('Element Not Found')
16 | }
17 | }
18 | }
19 | const nums = [1, 2, 3, 8, 9, 12]
20 | const target = 2
21 | for (const num of nums) {
22 | if (num === target) {
23 | console.log('Successful Search!')
24 | break
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/javascript-linear-search/javascript-linear-search-2.js:
--------------------------------------------------------------------------------
1 | function linear () {
2 | let n = parseInt(prompt('Enter the size of an array'))
3 | let a = []
4 | for (let i = 0; i < n; i++) {
5 | a[i] = parseInt(prompt('Current array:\t' + a + '\nEnter array elements'))
6 | }
7 | let k = parseInt(prompt('Current array:\t' + a + '\nEnter the key element to search: '))
8 | for (let i = 0; i < a.length; i++) {
9 | if (k === a[i]) {
10 | // document.body.innerText('');
11 | document.writeln('Element ' + a[i] + ' Found at Position:' + i)
12 | break
13 | } else if (i === (a.length - 1) && k !== a[i]) {
14 | // document.body.innerText('');
15 | document.writeln('Element Not Found')
16 | }
17 | }
18 | }
19 | const nums = [1, 2, 3, 8, 9, 12]
20 | const target = 2
21 | for (const num of nums) {
22 | if (num === target) {
23 | console.log('Successful Search!')
24 | break
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/fsharp-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # fsharp-linear-search
2 | *Example Linear Search in F#*
3 |
4 | 1) Locate your fsi executable - on Windows, something like `C:\Program Files (x86)\Microsoft SDKs\F#\10.1\Framework\v4.0\fsi.exe`
5 | 2) Run that executable from the command line with the its argument being the location of this fsharp-linear-search.fsx file, the second being the value that you are searching for, and the rest of your list of elements as the rest of the arguments.
6 |
7 | For example:
8 | ```
9 | & 'C:\Program Files (x86)\Microsoft SDKs\F#\10.1\Framework\v4.0\fsi.exe' .\fsharp-l
10 | inear-search.fsx 10 0 1 2 3 4
11 | ```
12 | will produce the result `Not Found!` because 10 is not in the list of 1 2 3 4 5 6, but
13 |
14 | ```
15 | & 'C:\Program Files (x86)\Microsoft SDKs\F#\10.1\Framework\v4.0\fsi.exe' .\fsharp-l
16 | inear-search.fsx 2 0 1 2 3 4
17 | ```
18 |
19 | will produce the result `2 found at index 2` because it is in the list
--------------------------------------------------------------------------------
/cpp-linear-search/cpp-linear-search-2.cpp:
--------------------------------------------------------------------------------
1 | /* STL IMPLEMENTATION OF LINEAR SEARCH IN C++
2 | ~By Rahul Suresh (http://github.com/icy-meteor)~*/
3 |
4 | //Header Files
5 | #include
6 | #include
7 | using namespace std;
8 |
9 | //Main Function
10 | int main()
11 | {
12 | int a,n,key;
13 | vector vec;
14 | vector::iterator it;
15 | cout<<"Enter number of elements : ";
16 | cin>>n;
17 | cout<<"Enter element to search : ";
18 | cin>>key;
19 | for(int i=0;i>a;
22 | vec.push_back(a);
23 | }
24 | for(it=vec.begin();it!=vec.end();it++) //Comparing each element with key
25 | {
26 | if((*it)==key)
27 | {
28 |
29 | cout<<"Element "<
7 | REM // in command prompt type as below:
8 | REM // >LinearSearch.bat "AA BB CC DD EE" "DD"
9 |
10 | REM // Acqusition variable argument1 as list
11 | SET arg1=%~1
12 | REM // Acqusition variable argument2 as target search
13 | SET arg2=%~2
14 | REM // Definition counter variable
15 | SET /a count = 0
16 | REM // Definition flag whenever found or not
17 | SET found=0
18 |
19 | REM // For Loop List (Linear Search)
20 | FOR %%s IN (%arg1%) DO (
21 | SET /a count += 1
22 | IF %%s==%arg2% (
23 | SET found=1
24 | @ECHO "Found for : %%s, Index : !count!"
25 | )
26 | )
27 |
28 | REM // If not found, print not found
29 | IF %found%==0 (
30 | @ECHO "Not found for : %arg2%, Total : %count%"
31 | )
--------------------------------------------------------------------------------
/elm-linear-search/Elm_Linear_Search.elm:
--------------------------------------------------------------------------------
1 | module Elm_Linear_Search exposing (output)
2 |
3 | import List exposing (head, length, drop, take)
4 |
5 | search : String -> List String -> Int -> String
6 | search query list index =
7 | case head list of
8 | Nothing -> "Not Found!"
9 | Just headElement ->
10 | if headElement == query then
11 | String.fromInt index
12 | else
13 | search query (drop 1 list) <| index + 1
14 |
15 | output : List String -> String
16 | output args =
17 | case length args of
18 | 0 -> "Enter list to search in args, with last args as search query"
19 | 1 -> "Enter list to search in args, with last args as search query. Having only one argument is invalid"
20 | _ ->
21 | let
22 | query = Maybe.withDefault "" <| head <| drop (length args - 1) args
23 | list = take ( length args - 1 ) args
24 | in
25 | search query list 0
26 |
--------------------------------------------------------------------------------
/C-linear-search/linear-search-1.c:
--------------------------------------------------------------------------------
1 | #include
2 | int ls(int arr[], int l, int r, int x)
3 | {
4 | if (r < l)
5 | {
6 | return -1;
7 | }
8 | if (arr[l] == x)
9 | {
10 | return l;
11 |
12 | }
13 | if (arr[r] == x)
14 | {
15 | return r;
16 | }
17 | else
18 | {
19 | return ls(arr, l+1, r-1, x);
20 | }
21 |
22 | }
23 |
24 | int main()
25 | {
26 | int n;
27 | // int arr[] = {12, 34, 54, 2, 3};
28 | printf("Enter the size of array\n");
29 | scanf("%d",&n);
30 | int arr[n];
31 | // scanf("%d",&n);
32 | printf("Enter the element in the array\n");
33 | for(int i=0;i list;
16 |
17 | for(var i = 0; i < element; i++){
18 | print("index: $i value: ");
19 | var inputVal = stdin.readLineSync();
20 | var value2 = int.parse(inputVal);
21 | list.add(value2);
22 | }
23 |
24 | print("which number would you like to search: ");
25 | var userVal = stdin.readLineSync();
26 | var userValue = int.parse(userVal);
27 | print("searching...");
28 | print(linearSearch(list, userValue));*/
29 |
30 | }
31 |
32 | String linearSearch(List list, int x){
33 | for (var i = 0; i < list.length; i++) {
34 | if(x == list[i]){
35 | return "Found! index is: $i";
36 | }
37 | }
38 |
39 | return "Not found!";
40 | }
--------------------------------------------------------------------------------
/nim-linear-search/nim-linear-search.nim:
--------------------------------------------------------------------------------
1 | import strutils
2 |
3 | proc linear_search[T](collection: seq[T], item: T): T =
4 | for i in 0..(collection.len() - 1):
5 | if collection[i] == item:
6 | return i
7 | return -1
8 |
9 | proc main() =
10 | echo "Enter size of collection:"
11 | let sizeOfCollection = readLine(stdin).parseInt()
12 |
13 | # input elements of array
14 | echo "Enter ",sizeOfCollection, " numbers, with each number followed by enter"
15 | var collection = newSeq[int]()
16 | for i in 0 .. (sizeOfCollection-1):
17 | collection.add(readLine(stdin).parseInt())
18 |
19 | # input no. to be serached
20 | echo "Enter no. to be searched:"
21 | let noToSearched = readLine(stdin).parseInt()
22 |
23 | # output index of searched no.
24 | var indexOfSearchedNo = linear_search(collection, noToSearched)
25 | if indexOfSearchedNo == -1:
26 | echo "No. not found!"
27 | else:
28 | echo "Postion of searched no. : ", (indexOfSearchedNo+1)
29 |
30 | main()
--------------------------------------------------------------------------------
/java-linear-search/java-linear-search-10.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | import java.util.Optional;
3 | import java.util.Scanner;
4 |
5 | public class JavaLinearSearchUsingStreams {
6 | public static void main(String[] args) {
7 | System.out.println("How many numbers do you want to enter?");
8 | Scanner scanner = new Scanner(System.in);
9 | int numbers = scanner.nextInt();
10 | ArrayList array = new ArrayList<>();
11 |
12 | for (int i = 0; i < numbers; i++) {
13 | System.out.println("Please enter the number for index " + i);
14 | array.add(scanner.nextInt());
15 | }
16 |
17 | System.out.println("Enter the number you want to search for");
18 | int numberToFind = scanner.nextInt();
19 | Optional isFound = array.stream().filter(i -> i == numberToFind).findFirst();
20 | if(isFound.isPresent()) {
21 | System.out.println("Found number at index" + array.indexOf(isFound));
22 | } else System.out.println("Number not present");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/c-linear-search/c-linear-search-4.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(int argc, char* args[]) {
4 | int data_size;
5 |
6 | printf("Size of data: ");
7 | scanf("%d", &data_size);
8 |
9 | int data[data_size];
10 |
11 | for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
12 | printf("Adding item (%d/%d): ", i+1, (int) (sizeof(data) / sizeof(data[0])));
13 | scanf("%d", &data[i]);
14 | }
15 |
16 | for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
17 | printf("%d", data[i]);
18 | if (i < sizeof(data) / sizeof(data[0]) - 1) {
19 | printf(", ");
20 | } else {
21 | printf("\n");
22 | }
23 | }
24 |
25 | printf("Perform sequential search for: ");
26 | int target;
27 | scanf("%d", &target);
28 |
29 | for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
30 | if (target == data[i]) {
31 | printf("Found item at index: %d\n", i);
32 | return 0;
33 | }
34 | }
35 | printf("Item not found.\n");
36 |
37 | return 1;
38 | }
39 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Ayan Banerjee
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/java-linear-search/JAVA-linearSearch3.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | import java.io.*;
3 | import java.lang.*;
4 |
5 | public class LinearSearch
6 | {
7 | public static void main(String args[])
8 | {
9 | Scanner in=new Scanner(System.in);
10 | System.out.println("Enter the no. of input integers:");
11 | int n=in.nextInt();
12 | int arr[] = new int[n];
13 |
14 |
15 | System.out.println("Enter Array Elements : ");
16 | for(int i=0; i printfn "%s found at index %d" v i
7 | exit 0
8 | | _ -> ()
9 |
10 | // search function, takes in value and list
11 | // curries value into the findin function and iterates through each element
12 | // if it gets through the whole list, it prints not found, because otherwise the code would have exited
13 | let search value list =
14 | Seq.iteri (fun i x -> findin i x value |> ignore) list
15 | printfn "Not Found!"
16 |
17 | // Parse the command line arguments such that the first argument is the desired value and the rest are the list of elements
18 | let args = Array.toSeq fsi.CommandLineArgs
19 | let s = Seq.tail args
20 | let search_value = Seq.head s
21 | let elements = Seq.tail s
22 |
23 | // call the search function with the value we are looking for and the list of elements
24 | search search_value elements
25 |
--------------------------------------------------------------------------------
/java-linear-search/README.md:
--------------------------------------------------------------------------------
1 | # java-linear-search
2 | *Example Linear Search in Java*
3 |
4 | # java-linear-search-2
5 | **Added features in version 2:**
6 |
7 | - Added JavaDoc
8 | - Only one class
9 | - Better code structure
10 | - Better testing
11 |
12 |
13 | # java-linear-search-3
14 | Easily understandable implementation
15 |
16 | # java-linear-search-11
17 | This is a simple method based single classed Java program to implement linear search.
18 | Inputs required in the program :
19 | - An integer that will be the size of the Array
20 | - An array with above-mentioned size
21 | - An integer that is to be found
22 | Outputs:
23 | - if found, the index of the array.
24 | - or else, not found.
25 |
26 | # java-linear-search-12
27 | A simple single class implementation of linear search in Java.
28 | The user will be prompted to input the number of elements in a list, they'd like to make.
29 | The user then will choose a target to be searched in the list.
30 | The linear search algorithm will be automatically performed, and the index at which the
31 | target was found will be returned or the code will notify that it could not find the target.
32 | In either case, the created list will be returned as well.
33 |
--------------------------------------------------------------------------------
/java-linear-search/java-linear-search-7.java:
--------------------------------------------------------------------------------
1 | /**
2 | * @author Andrew Joshua Loria
3 | * @version October 1, 2018
4 | */
5 | public class LinearSearch {
6 |
7 | /**
8 | * The main method of the program.
9 | * This contains a specific case of hardcoded values.
10 | *
11 | * @param args command line arguments.
12 | */
13 | public static void main(String[] args) {
14 | int [] theArray = {1,2,3,4,5,6}; // an arbitrary array of numbers
15 | int theNum = 5; // value to search for
16 | System.out.println("Output is: " + doLinearSearch(theArray, theNum));
17 | }
18 |
19 | /**
20 | * This method contains the linear search algorithm.
21 | *
22 | * @param arr the array of numbers to search.
23 | * @param target the number to look for.
24 | * @return if number present in array, its index is returned. If not, return -1.
25 | */
26 | private static int doLinearSearch(int arr[], int target) {
27 | int value = -1; //return value initialized to not found
28 | for (int index = 0; index < arr.length; index++) {
29 | if (arr[index] == target){
30 | value = index;
31 | }
32 | }
33 | return value;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/java-linear-search/java-linear-search-8.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class JavaLinearSearch8 {
4 |
5 | private static final Scanner SC = new Scanner(System.in);
6 |
7 | public static void main(String[] args) {
8 | Integer[] data;
9 |
10 | System.out.print("Size of data: ");
11 | data = new Integer[SC.nextInt()];
12 |
13 | for (int i = 0; i < data.length; i++) {
14 | System.out.print(String.format("Adding item (%d/%d): ", i+1, data.length));
15 | data[i] = SC.nextInt();
16 | }
17 |
18 | for (int i = 0; i < data.length; i++) {
19 | System.out.print(data[i]);
20 | if (i < data.length - 1) {
21 | System.out.print(", ");
22 | } else {
23 | System.out.println();
24 | }
25 | }
26 |
27 | System.out.print("Perform sequential search for: ");
28 | Integer target = SC.nextInt();
29 |
30 | for (int i = 0; i < data.length; i++) {
31 | if (target.equals(data[i])) {
32 | System.out.println("Found item at index: " + i);
33 | return;
34 | }
35 | }
36 | System.out.println("Item not found.");
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/java-linear-search/Java-Linear-Search-4.java:
--------------------------------------------------------------------------------
1 | /**
2 | * @author Ana Carolina Ferreira (github.com/anacdf)
3 | * @version October 5, 2018
4 | */
5 |
6 | import java.util.*;
7 |
8 | import static jdk.nashorn.internal.objects.NativeMath.random;
9 |
10 | public class JavaLinearSearch {
11 | public static void main(String args[]){
12 | Scanner in=new Scanner(System.in);
13 | Random random = new Random();
14 |
15 | System.out.println("Enter the limit of your array (from 1 to 1000): ");
16 | int size = in.nextInt();
17 | ArrayList list = new ArrayList(size);
18 |
19 | System.out.println("Great! Now Java is selecting some random numbers for you...");
20 | for(int i=0; i<=size; i++) {
21 | list.add(i, random.nextInt(size+1));
22 | }
23 |
24 | System.out.print("Please, enter the number to be searched, from 0 to 99: ");
25 | int search = in.nextInt();
26 |
27 | int index = -1;
28 | index = list.indexOf(search);
29 |
30 | if(index==-1){
31 | System.out.print("Number Not Found:");
32 | System.out.print(" "+list);
33 | } else {
34 | System.out.print("Number found at position " + index + "!");
35 | }
36 | }
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/csharp-linear-search/csharp-linear-search.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace forgetCode
4 | {
5 | class Program
6 | {
7 | public static void Main()
8 | {
9 | int[] a = new int[100];
10 | Console.WriteLine("Enter number of elements you want to hold in the array ?");
11 | string s = Console.ReadLine();
12 | int x = Int32.Parse(s);
13 |
14 | Console.WriteLine("\n Enter array elements \n");
15 | for (int i = 0; i < x; i++)
16 | {
17 | string s1 = Console.ReadLine();
18 | a[i] = Int32.Parse(s1);
19 | }
20 |
21 | Console.WriteLine("Enter Search element\n");
22 | string s3 = Console.ReadLine();
23 | int x2 = Int32.Parse(s3);
24 | for (int i = 0; i < x; i++)
25 | {
26 | if (a[i] == x2)
27 | {
28 |
29 | Console.WriteLine("Search successful");
30 | Console.WriteLine("Element {0} found at location {1}\n", x2, i + 1);
31 | return;
32 | }
33 | }
34 | Console.WriteLine("Search unsuccessful");
35 | }
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | You can contribute in 2 ways -
2 | 1. **By providing an algorithm of linear search in any programming language:**
3 | Your code should be able to take inputs (no. of elements, the elements, the element to search) and if that elememt is found it should print the index else print "Not Found!".
4 |
5 | Inside the main directory add a folder named `-Linear-Search`, for example `python3-Linear-Search`.Inside the folder there should be 2 files, a `README.md` briefly explaining how to run the code and the code file. Name of the code file should be same as the folder name.
6 | Note: If a code in that language is already present, do not create a new folder, instead append a number to the file name inside the folder. For example if `python3-Linear-Search` exists then inside that folder add `python3-Linear-Search-2.py`(your file name). Add related instructions to the README file.
7 |
8 | 2. You can also contribute by structuring the present code in the aforementioned way.
9 |
10 | Don't forget to write your name in the following structure in README under contributors sections: `||[your_name](https://github.com/your_github_handle]| | | |`.
11 |
12 | Note that, the `Your Institution Name>` field is optional and you may leave it blank if you wish.
13 |
14 |
15 | **NOTE: You can also edit others code if it can be improved.**
16 |
--------------------------------------------------------------------------------
/java-linear-search/java-linear-search-5.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 |
4 |
5 | class LinearSearch{
6 | int linearSearch(int arr[], int searchNumber){
7 | int len = arr.length;
8 | int found = 0; //Assuming that the number doesn't exist
9 | for(int i =0;i numbers = new ArrayList<>();
37 | System.out.println("How many number are you going to enter? ");
38 | int n = scan.nextInt();
39 | System.out.println("Enter the list of numbers: ");
40 | for (int i = 0; i < n; i++) {
41 | int num = scan.nextInt();
42 | numbers.add(num);
43 | }
44 |
45 | System.out.println("Enter the target: ");
46 | int target = scan.nextInt();
47 |
48 | if (numbers.indexOf(target) >= 0) {
49 | System.out.println("Successful Search!");
50 | } else {
51 | System.out.println("Unsuccessful Search!");
52 | }
53 | scan.close();
54 | }
55 | }
--------------------------------------------------------------------------------
/csharp-linear-search/csharp-linear-search-2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | class Program
4 | {
5 | static void Main()
6 | {
7 | //Declaring variables
8 | string inp;
9 | int len, val, res;
10 | int[] arr;
11 |
12 | Console.Write("Enter the length of array: ");
13 | inp = Console.ReadLine();
14 | len = Convert.ToInt32(inp);
15 |
16 | arr = new int[len];
17 |
18 | //taking array elements as input one by one
19 | for (int i=0; i= 0) {
38 | System.out.println("Successful Search! The index of your target is: " + indexOfTarget);
39 | } else {
40 | System.out.println("The array of numbers does not contain your desired number!");
41 | }
42 |
43 | //Close the scanner
44 | scanner.close();
45 | }
46 |
47 | /**
48 | * Implements the linear search algorithm.
49 | *
50 | * @param numbers the array of numbers to search.
51 | * @param target the number to look for.
52 | * @return the index of the number in the array, if the array contains the given number, otherwise -1.
53 | */
54 | private static int linearSearch(int[] numbers, int target) {
55 | for (int i = 0; i < numbers.length; i++) {
56 | if (numbers[i] == target) {
57 | return i;
58 | }
59 | }
60 | return -1;
61 | }
62 | }
--------------------------------------------------------------------------------
/java-linear-search/Java_Linear_Search_11.java:
--------------------------------------------------------------------------------
1 | package LinearSearch;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Java_Linear_Search_11 {
6 | /**
7 | *
8 | * @param numToFind is the number that we have to find in the Array
9 | * @param arrayOfIntegers is the array of integers entered by the user
10 | * @return if the number is not found in the array we return -1
11 | * which is also the default value
12 | * else we return 1
13 | */
14 | private static final int linearSearch(int numToFind, int[] arrayOfIntegers){
15 | for (int i = 0; i < arrayOfIntegers.length; i++) {
16 | if (arrayOfIntegers[i] == numToFind){
17 | return (i + 1);
18 | }
19 | }
20 | return -1;
21 | }
22 | public static void main(String[] args) {
23 |
24 | //declaring vars to measure time elapsed
25 | long endTime, timeElapsed, startTime;
26 | startTime = System.nanoTime();
27 |
28 | //declaring required Integer Variables
29 | int arrayLength = 0;
30 | int numToFind = 0;
31 |
32 | //initializing Scanner Class
33 | Scanner scanner = new Scanner(System.in);
34 |
35 | // input number of elements in the array
36 | System.out.println("Enter the number of Integers in array >");
37 | arrayLength = scanner.nextInt();
38 |
39 | //declaring array with the num of elements
40 | int[] arrayOfIntegers = new int[arrayLength];
41 |
42 | //getting array elements from the user
43 | System.out.println("Enter array of Integers > ");
44 | for (int i = 0; i < arrayLength; i++){
45 | System.out.println("Enter element " + (i +1));
46 | arrayOfIntegers[i] = scanner.nextInt();
47 | }
48 |
49 | //getting number to find
50 | System.out.println("Enter number to find > ");
51 | numToFind = scanner.nextInt();
52 |
53 | int numPresentInArrayAt = -1;
54 | numPresentInArrayAt = linearSearch(numToFind, arrayOfIntegers);
55 |
56 | if (numPresentInArrayAt == -1){
57 | System.out.println(numToFind + " is not found in the entered array...");
58 | }else {
59 | System.out.println(numToFind + " is present at position " + numPresentInArrayAt);
60 | }
61 |
62 | scanner.close();
63 | endTime = System.nanoTime();
64 |
65 | //calculating time elapsed
66 | timeElapsed = endTime - startTime;
67 | System.out.println("Time elapsed in nanoSeconds is " + timeElapsed);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Linear-Search
2 | Add a Linear Search Code in any language of your choice! Take a look at the [CONTRIBUTING.md](./CONTRIBUTING.md) before opening a pull request.
3 |
4 | ## About Linear Search
5 | Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
6 |
7 | 
8 |
9 |
10 | ## Contributors
11 |
12 | |Sl. No.| Name | Institution | Country | Language Added |
13 | | ----- | ---- | ----------- | ------- | -------------- |
14 | |1| [Ayan Banerjee](https://github.com/ayan-b) | | | |
15 | |2| [Craxy](https://github.com/CraxyTM) | | | |
16 | |3| [Bhrigu Kansra](https://github.com/kinetickansra) | | | |
17 | |4| [Rahul Suresh](https://github.com/icy-meteor) | | | |
18 | |5| [Derek Rogers](https://github.com/derek-rogers) | | | |
19 | |6| [Astha Awasthi](https://github.com/asaw4) | | | |
20 | |7| [Nattaaek Wattanuyan](https://github.com/nattaaek) | | | |
21 | |8| [Sahil Nishal](https://github.com/snishal) | | | |
22 | |9| [Saksham Garg](https://github.com/sak6e) | | | |
23 | |10| [D. Ben Knoble](https://github.com/benknoble) | | | |
24 | |11| [Priyanka Rajput](https://github.com/Priyankarajput1) | | | |
25 | |12| [Amartya Kalapahar](https://github.com/amartya-k) | N.M.A.M. Institute Of Technology, Nitte | India | Assembly |
26 | |13| [Owais Ali](https://github.com/ows-ali) |Lahore University of Management Sciences | Pakistan | C# |
27 | |14| [Vinayak](https://github.com/vinayak42) | Indraprastha Institute of Information Technology, Delhi | India | Python-3 |
28 | |15| [Daniel Del Rio](https://github.com/daniddelrio) | Ateneo de Manila University | Philippines | `issue#2` |
29 | |16| [Andrew Joshua Loria](https://github.com/ajloria) | University of Washington | United States | Java-7 |
30 | |17| [Jirayu Saengwannakool](https://github.com/bankzxcv) || Thailand | Javascript,Golang |
31 | |18| [Ryan Michalec](https://github.com/a3qz) || United States | F# |
32 | |19| [Pulkit Jatav](https://github.com/paradoxpj) | | India | Python |
33 | |20| [Marco Wang](https://github.com/aesophor) | University of Taipei | Taiwan | Java, C, Bash, Python-3 |
34 | |21| [Grzegorz Wcisło](https://github.com/grzegorz-wcislo) | | Poland | |
35 | |22| [Ivan Dyominov](https://github.com/dyominov) | | Ukraine | Scala |
36 | |23| [Jacob Fjerbaek Olsen](https://github.com/fjerbaek) | Aarhus University | Denmark | SML (Standard ML) |
37 | |24| [Ana Carolina Ferreira](https://github.com/anacdf) | PUCRS | Brazil | Java |
38 | |25| [Akash Thakrar](https://github.com/akashthakrar) | | India | CoffeeScript |
39 | |26| [Piyush Pant](https://github.com/PiyushP17) | DIT University, Dehradun | India | Python 3 |
40 | |27| [Søren Eriksen](https://github.com/soer7022) | Aarhus University | Denmark | Java |
41 | |28| [Akshay Khanna](https://github.com/akshaykhanna) | Thoughtworks | India | Nim |
42 | |29| [Aditya Jyoti Paul](https://github.com/phreakyphoenix) | SRM IST | India | Cpp |
43 | |30| [Tejas Agarwal](https://github.com/tjzs69) | Manipal University Jaipur | India | Java |
44 | |31| [Chandan Taneja](https://github.com/chandantaneja) | Manav Rachna University | India | Java
45 | |32| [Prateek Jena](https://github.com/prateikjena) | Bhadrak Autonomous College | India | Perl |
46 | |33| [Lalit Umbarkar](https://github.com/MrL1605) | | India | ELM |
47 | |34| [Joseph Egan](https://github.com/eganjs) | | | |
48 | |35| [Mohit Mohan](https://github.com/mohitmo)| IITK | India | |
49 | |36| [Aman Kumar](https://github.com/aman-ku)|IIIT,Vadodara|India|C|
50 | |37| [Ryan Smolik](https://github.com/ryansmolik03) | | |Java|
51 | |38| [Ciaran Evans](https://github.com/ciaranevans) | | UK | Lua |
52 | |39| [Balakumaran](https://github.com/webbalakumaran) | | India | Julia
53 | |40| [Janitha](https://github.com/janitha133) | | Sri Lanka | Java
54 |
--------------------------------------------------------------------------------