-
-
Notifications
You must be signed in to change notification settings - Fork 28
Add iterative and recursive linear searches #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 3336eac
Delete bubble_sort.nim
SatinWukerORIG b33e5b0
Create bubble_sort.nim
SatinWukerORIG 5e8ef79
Update bubble_sort.nim
SatinWukerORIG 9df2f6f
Create insertion_sort.nim
SatinWukerORIG 806dbb9
fix bug, unittesting -> unittest
SatinWukerORIG 9b2125a
Update insertion_sort.nim
SatinWukerORIG b0f4779
Update insertion_sort.nim
SatinWukerORIG 40f906e
Merge branch 'main' into main
SatinWukerORIG 6e2a0d2
Create linear_search.nim
SatinWukerORIG a6a8e08
Update linear_search.nim
SatinWukerORIG 1728c9e
Delete sorting directory
SatinWukerORIG 5f8e0d6
Update linear_search.nim
SatinWukerORIG 136dfa7
Update linear_search.nim
SatinWukerORIG efddc2a
Update searchings/linear_search.nim
SatinWukerORIG 4a416d8
Update searchings/linear_search.nim
SatinWukerORIG efbca42
Update linear_search.nim
SatinWukerORIG e2775f7
Update linear_search.nim
SatinWukerORIG f905dd1
Update linear_search.nim
SatinWukerORIG f70f39f
Rename linear_search.nim to linear_search.nim
SatinWukerORIG 6272656
Update linear_search.nim
SatinWukerORIG 3f5ae62
Update linear_search.nim
SatinWukerORIG 460a776
Update linear_search.nim
SatinWukerORIG 94e4737
Update linear_search.nim
SatinWukerORIG b1e7d67
Update linear_search.nim
SatinWukerORIG 5cbe4d6
Remove comment about imports
dlesnoff e82ce82
Remove system import
dlesnoff 614d075
Update searches/linear_search.nim
SatinWukerORIG 34f912c
Update searches/linear_search.nim
SatinWukerORIG 211282c
Update searches/linear_search.nim
SatinWukerORIG 8d69e3c
Update searches/linear_search.nim
SatinWukerORIG 49bea37
Update searches/linear_search.nim
SatinWukerORIG 1f07f1c
Update searches/linear_search.nim
SatinWukerORIG ddb0fa1
Update linear_search.nim
SatinWukerORIG c883341
Update linear_search.nim
SatinWukerORIG 326a2b1
Update linear_search.nim
SatinWukerORIG 370f94c
Update linear_search.nim
SatinWukerORIG 5431576
Update linear_search.nim
SatinWukerORIG a5a1915
Update linear_search.nim
SatinWukerORIG 5ebd8df
Update linear_search.nim
SatinWukerORIG ad14be7
Update searches/linear_search.nim
SatinWukerORIG 31590c9
Update searches/linear_search.nim
SatinWukerORIG 41f383b
Update linear_search.nim
SatinWukerORIG b769cfe
Update searches/linear_search.nim
SatinWukerORIG c0269ff
Update linear_search.nim
SatinWukerORIG 5e91290
Update linear_search.nim
SatinWukerORIG 02d2c82
Update linear_search.nim
SatinWukerORIG 1c57291
Update linear_search.nim
SatinWukerORIG 5f9abc1
Update linear_search.nim
SatinWukerORIG 209bc48
Update linear_search.nim
SatinWukerORIG 410510e
Update searches/linear_search.nim
SatinWukerORIG e13d2d3
Update DIRECTORY.md
SatinWukerORIG 4c7bc00
Update searches/linear_search.nim
SatinWukerORIG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,6 @@ | |
|
|
||
| ## Strings | ||
| * [Check Anagram](strings/check_anagram.nim) | ||
|
|
||
| ## Searches | ||
| * [Linear Search](searches/linear_search.nim) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
SatinWukerORIG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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))) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.