Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhi18av/f27fdc69a9d7ed8a65cb45f785aeff63 to your computer and use it in GitHub Desktop.
Save abhi18av/f27fdc69a9d7ed8a65cb45f785aeff63 to your computer and use it in GitHub Desktop.
Linear Search and algorithmic complexity
  • Haskell
import Data.List
 
haystack=["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
needles = ["Bush"]

map (\x -> (x, elemIndex x haystack)) needles
  • Clojure
(let [haystack ["Zig" "Zag" "Wally" "Ronald" "Bush" "Krusty" "Charlie" "Bush" "Bozo"]]
  (let [idx (.indexOf haystack "Bush")]
    (if (neg? idx) 
      (println "NOT Found")
      (println "Found"))))
  • Go
package main

import "fmt"

func linearsearch(datalist []string, key string) bool {
	for _, item := range datalist {
		if item == key {
			return true
		}
	}
	return false
}

func main() {
	items := []string{"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo"}
	fmt.Println(linearsearch(items, "Bush"))
}
  • JavaScript
var haystack = ['Zig', 'Zag', 'Wally', 'Ronald', 'Bush', 'Krusty', 'Charlie', 'Bush', 'Bozo']
var needles = ['Bush']
 
for (let i in needles) {
    var found = false;
    for (var j in haystack) {
        if (haystack[j] == needles[i]) {
            found = true;
            break;
        }
    }
    if (found)
        console.log("Found")
    else
    
        console.log("NOT Found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment