Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a1f09bf
Create bubble_sort.nim
SatinWukerORIG Jun 18, 2023
3336eac
Delete bubble_sort.nim
SatinWukerORIG Jun 18, 2023
b33e5b0
Create bubble_sort.nim
SatinWukerORIG Jun 18, 2023
5e8ef79
Update bubble_sort.nim
SatinWukerORIG Jun 18, 2023
9df2f6f
Create insertion_sort.nim
SatinWukerORIG Jun 18, 2023
806dbb9
fix bug, unittesting -> unittest
SatinWukerORIG Jun 18, 2023
9b2125a
Update insertion_sort.nim
SatinWukerORIG Jun 18, 2023
b0f4779
Update insertion_sort.nim
SatinWukerORIG Jun 18, 2023
40f906e
Merge branch 'main' into main
SatinWukerORIG Jun 19, 2023
6e2a0d2
Create linear_search.nim
SatinWukerORIG Jun 19, 2023
a6a8e08
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
1728c9e
Delete sorting directory
SatinWukerORIG Jun 19, 2023
5f8e0d6
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
136dfa7
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
efddc2a
Update searchings/linear_search.nim
SatinWukerORIG Jun 19, 2023
4a416d8
Update searchings/linear_search.nim
SatinWukerORIG Jun 19, 2023
efbca42
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
e2775f7
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
f905dd1
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
f70f39f
Rename linear_search.nim to linear_search.nim
SatinWukerORIG Jun 19, 2023
6272656
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
3f5ae62
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
460a776
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
94e4737
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
b1e7d67
Update linear_search.nim
SatinWukerORIG Jun 19, 2023
5cbe4d6
Remove comment about imports
dlesnoff Jun 19, 2023
e82ce82
Remove system import
dlesnoff Jun 19, 2023
614d075
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
34f912c
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
211282c
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
8d69e3c
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
49bea37
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
1f07f1c
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
ddb0fa1
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
c883341
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
326a2b1
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
370f94c
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
5431576
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
a5a1915
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
5ebd8df
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
ad14be7
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
31590c9
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
41f383b
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
b769cfe
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
c0269ff
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
5e91290
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
02d2c82
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
1c57291
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
5f9abc1
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
209bc48
Update linear_search.nim
SatinWukerORIG Jun 20, 2023
410510e
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
e13d2d3
Update DIRECTORY.md
SatinWukerORIG Jun 20, 2023
4c7bc00
Update searches/linear_search.nim
SatinWukerORIG Jun 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

## Strings
* [Check Anagram](strings/check_anagram.nim)

## Searches
* [Linear Search](searches/linear_search.nim)
88 changes: 88 additions & 0 deletions searches/linear_search.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
## Linear Search
## =============
## Linear search is the simplest but least efficient searching algorithm
## to search for an element in an array.
## It examines each element until it finds a match,
## starting at the beginning of the data set toward the end.
## The search ends when the element is located or when the end of the array is reached.
## https://en.wikipedia.org/wiki/Linear_search
##
## Time Complexity: O(n) where n is the length of the array.
## Space Complexity in for-loop linear search: O(1)
## Space Complexity in recursive linear search: O(n)
## Notice that recursive algorithms are nice to write and provide elegant implementations,
## but they are impeded by call stack management. Whatever the problem we face,
## there will be as much memory requirement as the number of stack frames.
## Therefore the recursive linear search is less efficient than the for-loop-based one.

runnableExamples:
var arr1 = [0, 3, 1, 4, 5, 6]
doAssert linearSearch(arr1, 5) == some(Natural(4))
doAssert recursiveLinearSearch(arr1, 5) == some(Natural(4))

var arr2 = ['0', 'c', 'a', 'u', '5', '7']
doAssert linearSearch(arr2, '5') == some(Natural(4))
doAssert recursiveLinearSearch(arr2, '5') == some(Natural(4))

var arr3 = [0, 3, 1, 4, 5, 6]
doAssert linearSearch(arr3, 7) == none(Natural)
doAssert recursiveLinearSearch(arr3, 7) == none(Natural)


import std/options

type
Nat = Natural
OptNat = Option[Natural]

func linearSearch*[T](arr: openArray[T], key: T): OptNat =
# key is the value we are searching for in the array.
for i, val in arr.pairs():
if val == key:
return some(Natural(i))
none(Natural) # `key` not found

func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat): OptNat=
# Recursion is another method for linear search.
# Recursive calls replace the for loop.

# `none(Natural)` is returned when the array is traversed completely
# and no key is matched, or when `arr` is empty.
if idx > arr.high:
return none(Natural)
if arr[idx] == key:
return some(idx)
recursiveLinearSearch(arr, key, idx + 1)


when isMainModule:
import unittest

template checkLinearSearch[T](arr: openArray[T], key: T, expectedIdx: OptNat): untyped =
check linearSearch(arr, key) == expectedIdx
check recursiveLinearSearch(arr, key) == expectedIdx

suite "Linear search":
test "Search in an empty array":
var arr: array[0, int]
checkLinearSearch(arr, 5, none(Natural))

test "Search in an int array matching with a valid value":
var arr = [0, 3, 1, 4, 5, 6]
checkLinearSearch(arr, 5, some(Natural(4)))

test "Search in an int array for a missing value":
var arr = [0, 3, 1, 4, 5, 6]
checkLinearSearch(arr, 7, none(Natural))

test "Search in a char array matching with a char matching value":
var arr = ['0', 'c', 'a', 'u', '5', '7']
checkLinearSearch(arr, '5', some(Natural(4)))

test "Search in a string array matching with a string matching value":
var arr = ["0", "c", "a", "u", "5", "7"]
checkLinearSearch(arr, "5", some(Natural(4)))

test "Search in an int array with a valid key at the end":
var arr = [1, 5, 3, 6, 5, 7]
checkLinearSearch(arr, 7, some(Natural(5)))