Skip to content

Commit c3440d4

Browse files
SatinWukerORIGdlesnoffZoomRmc
authored
Add iterative and recursive linear searches (#30)
* Create linear_search.nim * Update linear_search.nim * Delete sorting directory * Update linear_search.nim Adding time and space complexity * Update linear_search.nim Support search in types other than int, such as char and string * Update searchings/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update searchings/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Rename linear_search.nim to linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim Adding runnableExamples * Remove comment about imports Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Remove system import Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim Shortening line 38 using type: OptNat = Option[Natural] * Update linear_search.nim * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update linear_search.nim * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update linear_search.nim * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> * Update DIRECTORY.md * Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --------- Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com>
1 parent e8c06ff commit c3440d4

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
## Strings
1010
* [Check Anagram](strings/check_anagram.nim)
11+
12+
## Searches
13+
* [Linear Search](searches/linear_search.nim)

searches/linear_search.nim

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
## Linear Search
2+
## =============
3+
## Linear search is the simplest but least efficient searching algorithm
4+
## to search for an element in an array.
5+
## It examines each element until it finds a match,
6+
## starting at the beginning of the data set toward the end.
7+
## The search ends when the element is located or when the end of the array is reached.
8+
## https://en.wikipedia.org/wiki/Linear_search
9+
##
10+
## Time Complexity: O(n) where n is the length of the array.
11+
## Space Complexity in for-loop linear search: O(1)
12+
## Space Complexity in recursive linear search: O(n)
13+
## Notice that recursive algorithms are nice to write and provide elegant implementations,
14+
## but they are impeded by call stack management. Whatever the problem we face,
15+
## there will be as much memory requirement as the number of stack frames.
16+
## Therefore the recursive linear search is less efficient than the for-loop-based one.
17+
18+
runnableExamples:
19+
var arr1 = [0, 3, 1, 4, 5, 6]
20+
doAssert linearSearch(arr1, 5) == some(Natural(4))
21+
doAssert recursiveLinearSearch(arr1, 5) == some(Natural(4))
22+
23+
var arr2 = ['0', 'c', 'a', 'u', '5', '7']
24+
doAssert linearSearch(arr2, '5') == some(Natural(4))
25+
doAssert recursiveLinearSearch(arr2, '5') == some(Natural(4))
26+
27+
var arr3 = [0, 3, 1, 4, 5, 6]
28+
doAssert linearSearch(arr3, 7) == none(Natural)
29+
doAssert recursiveLinearSearch(arr3, 7) == none(Natural)
30+
31+
32+
import std/options
33+
34+
type
35+
Nat = Natural
36+
OptNat = Option[Natural]
37+
38+
func linearSearch*[T](arr: openArray[T], key: T): OptNat =
39+
# key is the value we are searching for in the array.
40+
for i, val in arr.pairs():
41+
if val == key:
42+
return some(Natural(i))
43+
none(Natural) # `key` not found
44+
45+
func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat): OptNat=
46+
# Recursion is another method for linear search.
47+
# Recursive calls replace the for loop.
48+
49+
# `none(Natural)` is returned when the array is traversed completely
50+
# and no key is matched, or when `arr` is empty.
51+
if idx > arr.high:
52+
return none(Natural)
53+
if arr[idx] == key:
54+
return some(idx)
55+
recursiveLinearSearch(arr, key, idx + 1)
56+
57+
58+
when isMainModule:
59+
import unittest
60+
61+
template checkLinearSearch[T](arr: openArray[T], key: T, expectedIdx: OptNat): untyped =
62+
check linearSearch(arr, key) == expectedIdx
63+
check recursiveLinearSearch(arr, key) == expectedIdx
64+
65+
suite "Linear search":
66+
test "Search in an empty array":
67+
var arr: array[0, int]
68+
checkLinearSearch(arr, 5, none(Natural))
69+
70+
test "Search in an int array matching with a valid value":
71+
var arr = [0, 3, 1, 4, 5, 6]
72+
checkLinearSearch(arr, 5, some(Natural(4)))
73+
74+
test "Search in an int array for a missing value":
75+
var arr = [0, 3, 1, 4, 5, 6]
76+
checkLinearSearch(arr, 7, none(Natural))
77+
78+
test "Search in a char array matching with a char matching value":
79+
var arr = ['0', 'c', 'a', 'u', '5', '7']
80+
checkLinearSearch(arr, '5', some(Natural(4)))
81+
82+
test "Search in a string array matching with a string matching value":
83+
var arr = ["0", "c", "a", "u", "5", "7"]
84+
checkLinearSearch(arr, "5", some(Natural(4)))
85+
86+
test "Search in an int array with a valid key at the end":
87+
var arr = [1, 5, 3, 6, 5, 7]
88+
checkLinearSearch(arr, 7, some(Natural(5)))

0 commit comments

Comments
 (0)