From a1f09bf9dea14c4b38f7199198d1bc83efa9822b Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 18:54:57 -0700 Subject: [PATCH 01/52] Create bubble_sort.nim --- bubble_sort.nim | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 bubble_sort.nim diff --git a/bubble_sort.nim b/bubble_sort.nim new file mode 100644 index 00000000..f25bd2c8 --- /dev/null +++ b/bubble_sort.nim @@ -0,0 +1,2 @@ +## Bubble Sort Implementation in Nim +## From 3336eac37e19b11b114f194b62d4f757aead0e71 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 19:22:39 -0700 Subject: [PATCH 02/52] Delete bubble_sort.nim --- bubble_sort.nim | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 bubble_sort.nim diff --git a/bubble_sort.nim b/bubble_sort.nim deleted file mode 100644 index f25bd2c8..00000000 --- a/bubble_sort.nim +++ /dev/null @@ -1,2 +0,0 @@ -## Bubble Sort Implementation in Nim -## From b33e5b030fd29f1b7c6c036d36774aa1698a570a Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 19:23:15 -0700 Subject: [PATCH 03/52] Create bubble_sort.nim --- sorting/bubble_sort.nim | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 sorting/bubble_sort.nim diff --git a/sorting/bubble_sort.nim b/sorting/bubble_sort.nim new file mode 100644 index 00000000..ed056c45 --- /dev/null +++ b/sorting/bubble_sort.nim @@ -0,0 +1,31 @@ +## Bubble Sort +## +## Define function, bubble_sort() +## Pass in array "arr" as parameter +proc bubble_sort(arr: var openarray[int]) = + ## Optimization: if array is already sorted, + ## it doesn't need to do this process + var swapped = false + ## Iterate through arr + for i in 0 .. high(arr) - 1: + ## high(arr) also work but outer loop will + ## repeat 1 time more. + ## Last i elements are already in place + for j in 0 .. high(arr) - i - 1: + ## Go through array from 0 to length of arr - i - 1 + ## Swap if the element found is greater + ## than the next element + if arr[j] > arr[j + 1]: + swap arr[j], arr[j + 1] + swapped = true + + if not swapped: + ## if no need to make a single swap, just exit. + return + + +var arr = @[5, 6, 2, 1, 3] + +bubble_sort(arr) + +echo repr(arr) From 5e8ef798570103f437526aec16505d21e724bf75 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 19:52:02 -0700 Subject: [PATCH 04/52] Update bubble_sort.nim --- sorting/bubble_sort.nim | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/sorting/bubble_sort.nim b/sorting/bubble_sort.nim index ed056c45..051e76ab 100644 --- a/sorting/bubble_sort.nim +++ b/sorting/bubble_sort.nim @@ -1,8 +1,15 @@ ## Bubble Sort ## +## https://en.wikipedia.org/wiki/Bubble_sort +## Time complexity: O(n^2) +## Auxiliary Space: O(1). + +## Import unit testing for testing purposes +import unittesting + ## Define function, bubble_sort() ## Pass in array "arr" as parameter -proc bubble_sort(arr: var openarray[int]) = +proc bubble_sort(arr: var openarray[int])= ## Optimization: if array is already sorted, ## it doesn't need to do this process var swapped = false @@ -23,9 +30,23 @@ proc bubble_sort(arr: var openarray[int]) = ## if no need to make a single swap, just exit. return +## Test +test "Empty array": + var arr: seq[int] + bubble_sort(arr) + echo repr(arr) + check arr == [] + -var arr = @[5, 6, 2, 1, 3] +test "one element array": + var arr = @[1] + bubble_sort(arr) + echo repr(arr) + check arr == [1] -bubble_sort(arr) -echo repr(arr) +test "complete array": + var arr = @[5, 6, 2, 1, 3] + bubble_sort(arr) + echo repr(arr) + check arr == @[1, 2, 3, 5, 6] From 9df2f6f3bd3cd6e745d9bad57f33abc2b3275e57 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 19:56:46 -0700 Subject: [PATCH 05/52] Create insertion_sort.nim --- sorting/insertion_sort.nim | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 sorting/insertion_sort.nim diff --git a/sorting/insertion_sort.nim b/sorting/insertion_sort.nim new file mode 100644 index 00000000..9e039b1d --- /dev/null +++ b/sorting/insertion_sort.nim @@ -0,0 +1,33 @@ +## Insertion Sort Algorithm Implementation +## +## https://en.wikipedia.org/wiki/Insertion_sort +## Worst Time Complexity: O(n^2) +## Best Time Complexity: O(n) +## Worst Space Complexity: O(n) + +## Import unit testing for testing purposes +import unittesting + +proc insertion_sort() + + +## Test +test "Empty array": + var arr: seq[int] + insertion_sort(arr) + echo repr(arr) + check arr == [] + + +test "one element array": + var arr = @[1] + insertion_sort(arr) + echo repr(arr) + check arr == [1] + + +test "complete array": + var arr = @[5, 6, 2, 1, 3] + insertion_sort(arr) + echo repr(arr) + check arr == @[1, 2, 3, 5, 6] From 806dbb9ab97df994c150418e938b5af90688fc21 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 20:06:28 -0700 Subject: [PATCH 06/52] fix bug, unittesting -> unittest --- sorting/bubble_sort.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/bubble_sort.nim b/sorting/bubble_sort.nim index 051e76ab..39837e38 100644 --- a/sorting/bubble_sort.nim +++ b/sorting/bubble_sort.nim @@ -5,7 +5,7 @@ ## Auxiliary Space: O(1). ## Import unit testing for testing purposes -import unittesting +import unittest ## Define function, bubble_sort() ## Pass in array "arr" as parameter From 9b2125a98bed00123993f01b34906947c41074c8 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sat, 17 Jun 2023 20:19:36 -0700 Subject: [PATCH 07/52] Update insertion_sort.nim --- sorting/insertion_sort.nim | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/sorting/insertion_sort.nim b/sorting/insertion_sort.nim index 9e039b1d..e0cc9503 100644 --- a/sorting/insertion_sort.nim +++ b/sorting/insertion_sort.nim @@ -6,10 +6,32 @@ ## Worst Space Complexity: O(n) ## Import unit testing for testing purposes -import unittesting +import unittest -proc insertion_sort() +## Define the function +proc insertion_sort(arr: var openarray[int]) = + ## Length is the length of arr + var length = high(arr) + + if length <= 1: + ## If length is or is less than one, no execution + return + + ## Iterate through array + for i in 1..high(arr): + + ## You can treat "key" as a temporary variable + var key = arr[i] + + ## Move elements of arr[0..i-1], that are + ## greater than key, to one position ahead + ## of their current position + var j = i - 1 + while j >= 0 and key < arr[j] : + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key ## Test test "Empty array": From b0f4779091f55f4da023663547accab9537db0e9 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sun, 18 Jun 2023 04:21:02 -0700 Subject: [PATCH 08/52] Update insertion_sort.nim adding "case" to comments --- sorting/insertion_sort.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorting/insertion_sort.nim b/sorting/insertion_sort.nim index e0cc9503..2e6b9aa5 100644 --- a/sorting/insertion_sort.nim +++ b/sorting/insertion_sort.nim @@ -1,9 +1,9 @@ ## Insertion Sort Algorithm Implementation ## ## https://en.wikipedia.org/wiki/Insertion_sort -## Worst Time Complexity: O(n^2) -## Best Time Complexity: O(n) -## Worst Space Complexity: O(n) +## Worst Case Time Complexity: O(n^2) +## Best Case Time Complexity: O(n) +## Worst Case Space Complexity: O(n) ## Import unit testing for testing purposes import unittest From 6e2a0d2b093aeac04902ff1ea551b4c71d732b19 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Sun, 18 Jun 2023 19:38:26 -0700 Subject: [PATCH 09/52] Create linear_search.nim --- searchings/linear_search.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 searchings/linear_search.nim diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim new file mode 100644 index 00000000..99cda92c --- /dev/null +++ b/searchings/linear_search.nim @@ -0,0 +1,19 @@ +## Linear Search in Pure Nim +## ============= +#[ +Linear search is the simplest but least efficient algorithm +to search for an element in a data set. +It examines each element until it finds a match, +starting at the beginning of the data set, until the end. +The search is finished and terminated once the target element is located. +# https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm +]# + +## openArray[T] is a proc parameter type that accept arrays and seqs +# value is the value for matching in the array +func linearSearch(arr: var openArray[T], value): int = + for i in 1..arr.len: + if arr[i] == value: + return i +when isMainModule: + import unittest From a6a8e087d5a7558640ffd1973d478dcdd8696aec Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 01:36:04 -0700 Subject: [PATCH 10/52] Update linear_search.nim --- searchings/linear_search.nim | 39 +++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index 99cda92c..468dff76 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -11,9 +11,42 @@ The search is finished and terminated once the target element is located. ## openArray[T] is a proc parameter type that accept arrays and seqs # value is the value for matching in the array -func linearSearch(arr: var openArray[T], value): int = - for i in 1..arr.len: - if arr[i] == value: +func linearSearch(arr: var openArray[int], value:int): int = + for i in 0..arr.len - 1: # len - 1 to make sure no index out of bound + if arr[i] == value: return i + return -1 # -1 is the default index for unfound element + +## recursion is another method for linear search +## we can just replace the for loop with recursion. +## recursion traverse from the end of the array to the front. +func recursiveLinearSearch(arr: var openArray[int], idx:int, value:int): int = + ## return -1 would be invoked when the array is traversed completely + ## and no value is matched, or when array is empty and has a length of 0 + if idx == -1: + return -1 + if arr[idx] == value: + return idx + return recursiveLinearSearch(arr, idx - 1, value) + when isMainModule: import unittest + +suite "Linear search": + test "Search in empty array": + var arr: array[0, int] + + check linearSearch(arr, 5) == -1 + check recursiveLinearSearch(arr, arr.len - 1, 5) == -1 + + test "Search in int array with valid matching value": + var arr = @[0, 3, 1, 4, 5, 6] + + check linearSearch(arr, 5) == 4 + check recursiveLinearSearch(arr, arr.len - 1, 5) == 4 + + test "Search in int array with invalid matching value": + var arr = @[0, 3, 1, 4, 5, 6] + + check linearSearch(arr, 7) == -1 + check recursiveLinearSearch(arr, arr.len - 1, 7) == -1 From 1728c9e525e619b246a8d640b48a4e3d93a195b7 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 01:36:26 -0700 Subject: [PATCH 11/52] Delete sorting directory --- sorting/bubble_sort.nim | 52 ----------------------------------- sorting/insertion_sort.nim | 55 -------------------------------------- 2 files changed, 107 deletions(-) delete mode 100644 sorting/bubble_sort.nim delete mode 100644 sorting/insertion_sort.nim diff --git a/sorting/bubble_sort.nim b/sorting/bubble_sort.nim deleted file mode 100644 index 39837e38..00000000 --- a/sorting/bubble_sort.nim +++ /dev/null @@ -1,52 +0,0 @@ -## Bubble Sort -## -## https://en.wikipedia.org/wiki/Bubble_sort -## Time complexity: O(n^2) -## Auxiliary Space: O(1). - -## Import unit testing for testing purposes -import unittest - -## Define function, bubble_sort() -## Pass in array "arr" as parameter -proc bubble_sort(arr: var openarray[int])= - ## Optimization: if array is already sorted, - ## it doesn't need to do this process - var swapped = false - ## Iterate through arr - for i in 0 .. high(arr) - 1: - ## high(arr) also work but outer loop will - ## repeat 1 time more. - ## Last i elements are already in place - for j in 0 .. high(arr) - i - 1: - ## Go through array from 0 to length of arr - i - 1 - ## Swap if the element found is greater - ## than the next element - if arr[j] > arr[j + 1]: - swap arr[j], arr[j + 1] - swapped = true - - if not swapped: - ## if no need to make a single swap, just exit. - return - -## Test -test "Empty array": - var arr: seq[int] - bubble_sort(arr) - echo repr(arr) - check arr == [] - - -test "one element array": - var arr = @[1] - bubble_sort(arr) - echo repr(arr) - check arr == [1] - - -test "complete array": - var arr = @[5, 6, 2, 1, 3] - bubble_sort(arr) - echo repr(arr) - check arr == @[1, 2, 3, 5, 6] diff --git a/sorting/insertion_sort.nim b/sorting/insertion_sort.nim deleted file mode 100644 index 2e6b9aa5..00000000 --- a/sorting/insertion_sort.nim +++ /dev/null @@ -1,55 +0,0 @@ -## Insertion Sort Algorithm Implementation -## -## https://en.wikipedia.org/wiki/Insertion_sort -## Worst Case Time Complexity: O(n^2) -## Best Case Time Complexity: O(n) -## Worst Case Space Complexity: O(n) - -## Import unit testing for testing purposes -import unittest - -## Define the function -proc insertion_sort(arr: var openarray[int]) = - - ## Length is the length of arr - var length = high(arr) - - if length <= 1: - ## If length is or is less than one, no execution - return - - ## Iterate through array - for i in 1..high(arr): - - ## You can treat "key" as a temporary variable - var key = arr[i] - - ## Move elements of arr[0..i-1], that are - ## greater than key, to one position ahead - ## of their current position - var j = i - 1 - while j >= 0 and key < arr[j] : - arr[j + 1] = arr[j] - j -= 1 - arr[j + 1] = key - -## Test -test "Empty array": - var arr: seq[int] - insertion_sort(arr) - echo repr(arr) - check arr == [] - - -test "one element array": - var arr = @[1] - insertion_sort(arr) - echo repr(arr) - check arr == [1] - - -test "complete array": - var arr = @[5, 6, 2, 1, 3] - insertion_sort(arr) - echo repr(arr) - check arr == @[1, 2, 3, 5, 6] From 5f8e0d6495df39c8bea2b56b6b701ccff9c5d8a1 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 01:44:46 -0700 Subject: [PATCH 12/52] Update linear_search.nim Adding time and space complexity --- searchings/linear_search.nim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index 468dff76..ec6cf77f 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -8,6 +8,10 @@ starting at the beginning of the data set, until the end. The search is finished and terminated once the target element is located. # https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm ]# +## Worst case time complexity: O(N) +## Average case time complexity: O(N) +## Best case time complexity: O(1) +## Space complexity: O(1) ## openArray[T] is a proc parameter type that accept arrays and seqs # value is the value for matching in the array From 136dfa7a92f52f6af443e13e817b72315aad4288 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 02:05:28 -0700 Subject: [PATCH 13/52] Update linear_search.nim Support search in types other than int, such as char and string --- searchings/linear_search.nim | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index ec6cf77f..2c2c6ff1 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -13,9 +13,9 @@ The search is finished and terminated once the target element is located. ## Best case time complexity: O(1) ## Space complexity: O(1) -## openArray[T] is a proc parameter type that accept arrays and seqs +## openArray[T] is a func parameter type that accept arrays and seqs in any type # value is the value for matching in the array -func linearSearch(arr: var openArray[int], value:int): int = +func linearSearch[T](arr: openArray[T], value:T): int = for i in 0..arr.len - 1: # len - 1 to make sure no index out of bound if arr[i] == value: return i @@ -24,7 +24,7 @@ func linearSearch(arr: var openArray[int], value:int): int = ## recursion is another method for linear search ## we can just replace the for loop with recursion. ## recursion traverse from the end of the array to the front. -func recursiveLinearSearch(arr: var openArray[int], idx:int, value:int): int = +func recursiveLinearSearch[T](arr: openArray[T], idx:int, value:T): int = ## return -1 would be invoked when the array is traversed completely ## and no value is matched, or when array is empty and has a length of 0 if idx == -1: @@ -36,21 +36,33 @@ func recursiveLinearSearch(arr: var openArray[int], idx:int, value:int): int = when isMainModule: import unittest -suite "Linear search": + suite "Linear search": test "Search in empty array": var arr: array[0, int] check linearSearch(arr, 5) == -1 check recursiveLinearSearch(arr, arr.len - 1, 5) == -1 - test "Search in int array with valid matching value": + test "Search in int array matching with valid value": var arr = @[0, 3, 1, 4, 5, 6] check linearSearch(arr, 5) == 4 check recursiveLinearSearch(arr, arr.len - 1, 5) == 4 - test "Search in int array with invalid matching value": + test "Search in int array matching with invalid value": var arr = @[0, 3, 1, 4, 5, 6] check linearSearch(arr, 7) == -1 check recursiveLinearSearch(arr, arr.len - 1, 7) == -1 + + test "Search in char array matching with char matching value": + var arr = @['0', 'c', 'a', 'u', '5', '7'] + + check linearSearch(arr, '5') == 4 + check recursiveLinearSearch(arr, arr.len - 1, '5') == 4 + + test "Search in string array matching with string matching value": + var arr = @["0", "c", "a", "u", "5", "7"] + + check linearSearch(arr, "5") == 4 + check recursiveLinearSearch(arr, arr.len - 1, "5") == 4 From efddc2aa5681e1a3d60754914ad4c0b8bf49350d Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 03:01:50 -0700 Subject: [PATCH 14/52] Update searchings/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searchings/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index 2c2c6ff1..c0413728 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -1,4 +1,4 @@ -## Linear Search in Pure Nim +## Linear Search ## ============= #[ Linear search is the simplest but least efficient algorithm From 4a416d8ecd51e0ddc2772f91c4c908dca66afad4 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 03:02:19 -0700 Subject: [PATCH 15/52] Update searchings/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searchings/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index c0413728..3f560280 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -15,7 +15,7 @@ The search is finished and terminated once the target element is located. ## openArray[T] is a func parameter type that accept arrays and seqs in any type # value is the value for matching in the array -func linearSearch[T](arr: openArray[T], value:T): int = +func linearSearch[T](arr: openArray[T], value: T): int = for i in 0..arr.len - 1: # len - 1 to make sure no index out of bound if arr[i] == value: return i From efbca4298691d267c07b3c97c83b2c14ff9d8633 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 03:16:42 -0700 Subject: [PATCH 16/52] Update linear_search.nim --- searchings/linear_search.nim | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index 3f560280..2af6b5a2 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -8,10 +8,12 @@ starting at the beginning of the data set, until the end. The search is finished and terminated once the target element is located. # https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm ]# -## Worst case time complexity: O(N) -## Average case time complexity: O(N) -## Best case time complexity: O(1) -## Space complexity: O(1) +## Time Complexity: O(N) +## Space Complexity in for-loop linear search: O(1) +## Space Complexity in recursive linear search: O(n) +## This is because of the call stack operation, +## and there is no CPS automatic optimization in Nim +## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style ## openArray[T] is a func parameter type that accept arrays and seqs in any type # value is the value for matching in the array From e2775f755c34b1ccdadbb3f374179e4ccb1e0af3 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 03:19:18 -0700 Subject: [PATCH 17/52] Update linear_search.nim --- searchings/linear_search.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index 2af6b5a2..0715e968 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -15,10 +15,10 @@ The search is finished and terminated once the target element is located. ## and there is no CPS automatic optimization in Nim ## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style -## openArray[T] is a func parameter type that accept arrays and seqs in any type +# openArray[T] is a func parameter type that accept arrays and seqs in any type # value is the value for matching in the array func linearSearch[T](arr: openArray[T], value: T): int = - for i in 0..arr.len - 1: # len - 1 to make sure no index out of bound + for i in arr.low .. arr.high: if arr[i] == value: return i return -1 # -1 is the default index for unfound element From f905dd10a1c24c8c1c0ff3518c66699169f02a44 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 06:14:12 -0700 Subject: [PATCH 18/52] Update linear_search.nim --- searchings/linear_search.nim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/searchings/linear_search.nim b/searchings/linear_search.nim index 0715e968..d8bc7e35 100644 --- a/searchings/linear_search.nim +++ b/searchings/linear_search.nim @@ -23,10 +23,10 @@ func linearSearch[T](arr: openArray[T], value: T): int = return i return -1 # -1 is the default index for unfound element -## recursion is another method for linear search +## Recursion is another method for linear search ## we can just replace the for loop with recursion. -## recursion traverse from the end of the array to the front. -func recursiveLinearSearch[T](arr: openArray[T], idx:int, value:T): int = +## recursion traverses from the end of the array to the front. +func recursiveLinearSearch[T](arr: openArray[T], idx: int, value: T): int = ## return -1 would be invoked when the array is traversed completely ## and no value is matched, or when array is empty and has a length of 0 if idx == -1: @@ -39,31 +39,31 @@ when isMainModule: import unittest suite "Linear search": - test "Search in empty array": + test "Search in an empty array": var arr: array[0, int] check linearSearch(arr, 5) == -1 check recursiveLinearSearch(arr, arr.len - 1, 5) == -1 - test "Search in int array matching with valid value": + test "Search in an int array matching with a valid value": var arr = @[0, 3, 1, 4, 5, 6] check linearSearch(arr, 5) == 4 check recursiveLinearSearch(arr, arr.len - 1, 5) == 4 - test "Search in int array matching with invalid value": + test "Search in an int array matching with an invalid value": var arr = @[0, 3, 1, 4, 5, 6] check linearSearch(arr, 7) == -1 check recursiveLinearSearch(arr, arr.len - 1, 7) == -1 - test "Search in char array matching with char matching value": + test "Search in a char array matching with a char matching value": var arr = @['0', 'c', 'a', 'u', '5', '7'] check linearSearch(arr, '5') == 4 check recursiveLinearSearch(arr, arr.len - 1, '5') == 4 - test "Search in string array matching with string matching value": + test "Search in a string array matching with a string matching value": var arr = @["0", "c", "a", "u", "5", "7"] check linearSearch(arr, "5") == 4 From f70f39f58cb21aec86e479488f800b7c7242b887 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 06:15:14 -0700 Subject: [PATCH 19/52] Rename linear_search.nim to linear_search.nim --- {searchings => searches}/linear_search.nim | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {searchings => searches}/linear_search.nim (100%) diff --git a/searchings/linear_search.nim b/searches/linear_search.nim similarity index 100% rename from searchings/linear_search.nim rename to searches/linear_search.nim From 62726561282550cc20b1d6561eb86f84b2cba4c0 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 07:41:20 -0700 Subject: [PATCH 20/52] Update linear_search.nim --- searches/linear_search.nim | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index d8bc7e35..38881ed2 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -1,13 +1,12 @@ ## Linear Search ## ============= -#[ -Linear search is the simplest but least efficient algorithm -to search for an element in a data set. -It examines each element until it finds a match, -starting at the beginning of the data set, until the end. -The search is finished and terminated once the target element is located. -# https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm -]# +## Linear search is the simplest but least efficient algorithm +## to search for an element in a data set. +## It examines each element until it finds a match, +## starting at the beginning of the data set, until the end. +## The search is finished and terminated once the target element is located. +## https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm +## ## Time Complexity: O(N) ## Space Complexity in for-loop linear search: O(1) ## Space Complexity in recursive linear search: O(n) @@ -15,25 +14,25 @@ The search is finished and terminated once the target element is located. ## and there is no CPS automatic optimization in Nim ## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style -# openArray[T] is a func parameter type that accept arrays and seqs in any type -# value is the value for matching in the array -func linearSearch[T](arr: openArray[T], value: T): int = +func linearSearch[T](arr: openArray[T], key: T): int = + ## key is the value for matching in the array for i in arr.low .. arr.high: - if arr[i] == value: + if arr[i] == key: return i - return -1 # -1 is the default index for unfound element + return -1 # -1 is the default index for the unfound element -## Recursion is another method for linear search -## we can just replace the for loop with recursion. -## recursion traverses from the end of the array to the front. func recursiveLinearSearch[T](arr: openArray[T], idx: int, value: T): int = + ## Recursion is another method for linear search + ## we can just replace the for loop with recursion. + ## recursion traverses from the end of the array to the front. + ## return -1 would be invoked when the array is traversed completely - ## and no value is matched, or when array is empty and has a length of 0 + ## and no value is matched, or when the array is empty and has a length of 0 if idx == -1: return -1 if arr[idx] == value: return idx - return recursiveLinearSearch(arr, idx - 1, value) + recursiveLinearSearch(arr, idx - 1, value) when isMainModule: import unittest From 3f5ae62e74f1f8c14e1865846f7e9d51b0713779 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 07:54:17 -0700 Subject: [PATCH 21/52] Update linear_search.nim --- searches/linear_search.nim | 49 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 38881ed2..e7ecdcfd 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -14,24 +14,29 @@ ## and there is no CPS automatic optimization in Nim ## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style -func linearSearch[T](arr: openArray[T], key: T): int = +import std/options +import system + +func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = ## key is the value for matching in the array - for i in arr.low .. arr.high: + var i: Natural = 0 + while i < arr.len - 1: if arr[i] == key: - return i - return -1 # -1 is the default index for the unfound element + return some(i) + inc i + return none(Natural) # -1 is the default index for unfound element -func recursiveLinearSearch[T](arr: openArray[T], idx: int, value: T): int = +func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option[Natural] = ## Recursion is another method for linear search ## we can just replace the for loop with recursion. ## recursion traverses from the end of the array to the front. ## return -1 would be invoked when the array is traversed completely - ## and no value is matched, or when the array is empty and has a length of 0 + ## and no value is matched, or when array is empty and has a length of 0 if idx == -1: - return -1 + return none(Natural) if arr[idx] == value: - return idx + return some(idx) recursiveLinearSearch(arr, idx - 1, value) when isMainModule: @@ -41,29 +46,29 @@ when isMainModule: test "Search in an empty array": var arr: array[0, int] - check linearSearch(arr, 5) == -1 - check recursiveLinearSearch(arr, arr.len - 1, 5) == -1 + check linearSearch(arr, 5) == none(int) + check recursiveLinearSearch(arr, arr.len - 1, 5) == none(Natural) test "Search in an int array matching with a valid value": - var arr = @[0, 3, 1, 4, 5, 6] + var arr = [0, 3, 1, 4, 5, 6] - check linearSearch(arr, 5) == 4 - check recursiveLinearSearch(arr, arr.len - 1, 5) == 4 + check linearSearch(arr, 5) == some(4) + check recursiveLinearSearch(arr, arr.len - 1, 5) == some(4) test "Search in an int array matching with an invalid value": - var arr = @[0, 3, 1, 4, 5, 6] + var arr = [0, 3, 1, 4, 5, 6] - check linearSearch(arr, 7) == -1 - check recursiveLinearSearch(arr, arr.len - 1, 7) == -1 + check linearSearch(arr, 7) == none(int) + check recursiveLinearSearch(arr, arr.len - 1, 7) == none(Natural) test "Search in a char array matching with a char matching value": - var arr = @['0', 'c', 'a', 'u', '5', '7'] + var arr = ['0', 'c', 'a', 'u', '5', '7'] - check linearSearch(arr, '5') == 4 - check recursiveLinearSearch(arr, arr.len - 1, '5') == 4 + check linearSearch(arr, '5') == some(4) + check recursiveLinearSearch(arr, arr.len - 1, '5') == some(4) test "Search in a string array matching with a string matching value": - var arr = @["0", "c", "a", "u", "5", "7"] + var arr = ["0", "c", "a", "u", "5", "7"] - check linearSearch(arr, "5") == 4 - check recursiveLinearSearch(arr, arr.len - 1, "5") == 4 + check linearSearch(arr, "5") == some(4) + check recursiveLinearSearch(arr, arr.len - 1, "5") == some(4) From 460a7764c79e101f796fe9a65df7af3360eee046 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:15:04 -0700 Subject: [PATCH 22/52] Update linear_search.nim --- searches/linear_search.nim | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index e7ecdcfd..9cc304bb 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -19,11 +19,10 @@ import system func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = ## key is the value for matching in the array - var i: Natural = 0 - while i < arr.len - 1: + for i in arr.low .. arr.high: if arr[i] == key: - return some(i) - inc i + return some(Natural(i)) + return none(Natural) # -1 is the default index for unfound element func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option[Natural] = @@ -33,7 +32,7 @@ func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option ## return -1 would be invoked when the array is traversed completely ## and no value is matched, or when array is empty and has a length of 0 - if idx == -1: + if idx <= 0: return none(Natural) if arr[idx] == value: return some(idx) @@ -46,29 +45,29 @@ when isMainModule: test "Search in an empty array": var arr: array[0, int] - check linearSearch(arr, 5) == none(int) - check recursiveLinearSearch(arr, arr.len - 1, 5) == none(Natural) + check linearSearch(arr, 5) == none(Natural) + check recursiveLinearSearch(arr, arr.high, 5) == none(Natural) test "Search in an int array matching with a valid value": var arr = [0, 3, 1, 4, 5, 6] - check linearSearch(arr, 5) == some(4) - check recursiveLinearSearch(arr, arr.len - 1, 5) == some(4) + check linearSearch(arr, 5) == some(Natural(4)) + check recursiveLinearSearch(arr, arr.high, 5) == some(Natural(4)) test "Search in an int array matching with an invalid value": var arr = [0, 3, 1, 4, 5, 6] - check linearSearch(arr, 7) == none(int) - check recursiveLinearSearch(arr, arr.len - 1, 7) == none(Natural) + check linearSearch(arr, 7) == none(Natural) + check recursiveLinearSearch(arr, arr.high, 7) == none(Natural) test "Search in a char array matching with a char matching value": var arr = ['0', 'c', 'a', 'u', '5', '7'] - check linearSearch(arr, '5') == some(4) - check recursiveLinearSearch(arr, arr.len - 1, '5') == some(4) + check linearSearch(arr, '5') == some(Natural(4)) + check recursiveLinearSearch(arr, arr.high, '5') == some(Natural(4)) test "Search in a string array matching with a string matching value": var arr = ["0", "c", "a", "u", "5", "7"] - check linearSearch(arr, "5") == some(4) - check recursiveLinearSearch(arr, arr.len - 1, "5") == some(4) + check linearSearch(arr, "5") == some(Natural(4)) + check recursiveLinearSearch(arr, arr.high, "5") == some(Natural(4)) From 94e4737ec8f6dbf8015b47cdee56d86cf7581364 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:19:49 -0700 Subject: [PATCH 23/52] Update linear_search.nim --- searches/linear_search.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 9cc304bb..ec3ad78d 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -14,6 +14,7 @@ ## and there is no CPS automatic optimization in Nim ## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style +## importing options and system for type Option and Natural import std/options import system From b1e7d671134626e05907c5ecd2b29ebdd5859bb4 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:29:41 -0700 Subject: [PATCH 24/52] Update linear_search.nim Adding runnableExamples --- searches/linear_search.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index ec3ad78d..13cef778 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -14,6 +14,16 @@ ## and there is no CPS automatic optimization in Nim ## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style +runnableExamples: + var arr1 = [0, 3, 1, 4, 5, 6] + doAssert linearSearch(arr1, 5) == some(Natural(4)) + doAssert recursiveLinearSearch(arr1, arr1.high, 5) == some(Natural(4)) + + var arr2 = ['0', 'c', 'a', 'u', '5', '7'] + doAssert linearSearch(arr2, '5') == some(Natural(4)) + doAssert recursiveLinearSearch(arr2, arr2.high, '5') == some(Natural(4)) + + ## importing options and system for type Option and Natural import std/options import system @@ -39,6 +49,7 @@ func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option return some(idx) recursiveLinearSearch(arr, idx - 1, value) + when isMainModule: import unittest From 5cbe4d6ed3d5c75f7e0d5aa4996d1ffaffd39c5a Mon Sep 17 00:00:00 2001 From: dlesnoff <54949944+dlesnoff@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:11:35 +0200 Subject: [PATCH 25/52] Remove comment about imports Co-authored-by: Zoom --- searches/linear_search.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 13cef778..b7d6f931 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -24,7 +24,6 @@ runnableExamples: doAssert recursiveLinearSearch(arr2, arr2.high, '5') == some(Natural(4)) -## importing options and system for type Option and Natural import std/options import system From e82ce82557766eb75e8abbb8bff3564f8ab1732e Mon Sep 17 00:00:00 2001 From: dlesnoff <54949944+dlesnoff@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:12:46 +0200 Subject: [PATCH 26/52] Remove system import Co-authored-by: Zoom --- searches/linear_search.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index b7d6f931..56ef433e 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -25,7 +25,6 @@ runnableExamples: import std/options -import system func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = ## key is the value for matching in the array From 614d0756d98e7e5b30aa918c9d58e8df3ea8971d Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:02:35 +0800 Subject: [PATCH 27/52] Update searches/linear_search.nim Co-authored-by: Zoom --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 56ef433e..2236238a 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -39,7 +39,7 @@ func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option ## we can just replace the for loop with recursion. ## recursion traverses from the end of the array to the front. - ## return -1 would be invoked when the array is traversed completely + ## `none(Natural)` is returned when the array is traversed completely ## and no value is matched, or when array is empty and has a length of 0 if idx <= 0: return none(Natural) From 34f912c4953fdffba6f9bff768aa6e51a228ff15 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:02:43 +0800 Subject: [PATCH 28/52] Update searches/linear_search.nim Co-authored-by: Zoom --- searches/linear_search.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 2236238a..065ed3ab 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -3,7 +3,8 @@ ## Linear search is the simplest but least efficient algorithm ## to search for an element in a data set. ## It examines each element until it finds a match, -## starting at the beginning of the data set, until the end. +## starting at the beginning of the data set for the iterative version, +## or in the opposite direction for the recursive version, as implemented in this module. ## The search is finished and terminated once the target element is located. ## https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm ## From 211282c76d3a41bbc2d5c52297af5f3686ce028c Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:03:39 +0800 Subject: [PATCH 29/52] Update searches/linear_search.nim Co-authored-by: Zoom --- searches/linear_search.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 065ed3ab..2361bd1a 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -32,8 +32,7 @@ func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = for i in arr.low .. arr.high: if arr[i] == key: return some(Natural(i)) - - return none(Natural) # -1 is the default index for unfound element + none(Natural) # `key` not found func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option[Natural] = ## Recursion is another method for linear search From 8d69e3c098d7f27617bec6055b49567faf7b59a9 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:04:21 +0800 Subject: [PATCH 30/52] Update searches/linear_search.nim Co-authored-by: Zoom --- searches/linear_search.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 2361bd1a..77cd2e81 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -29,8 +29,8 @@ import std/options func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = ## key is the value for matching in the array - for i in arr.low .. arr.high: - if arr[i] == key: + for i, val in arr.pairs(): + if val == key: return some(Natural(i)) none(Natural) # `key` not found From 49bea37f6076c39dfba4ab297840e4553db45078 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:04:44 +0800 Subject: [PATCH 31/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 77cd2e81..69c75d22 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -40,7 +40,7 @@ func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option ## recursion traverses from the end of the array to the front. ## `none(Natural)` is returned when the array is traversed completely - ## and no value is matched, or when array is empty and has a length of 0 + ## and no value is matched, or when `arr` is empty and has a length of 0 if idx <= 0: return none(Natural) if arr[idx] == value: From 1f07f1ca1c4dadf018ec32defbab1a5cd92a087b Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:05:29 +0800 Subject: [PATCH 32/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 69c75d22..91b94c81 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -51,33 +51,32 @@ func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option when isMainModule: import unittest + template checkLinearSearch[T](arr: openArray[T], value: T, expectedIndex: Option[Natural]) = + check linearSearch(arr, value) == expectedIndex + check recursiveLinearSearch(arr, arr.high, value) == expectedIndex + suite "Linear search": test "Search in an empty array": var arr: array[0, int] - check linearSearch(arr, 5) == none(Natural) - check recursiveLinearSearch(arr, arr.high, 5) == none(Natural) + checkLinearSearch(arr, 5, none(Natural)) test "Search in an int array matching with a valid value": var arr = [0, 3, 1, 4, 5, 6] - check linearSearch(arr, 5) == some(Natural(4)) - check recursiveLinearSearch(arr, arr.high, 5) == some(Natural(4)) + checkLinearSearch(arr, 5, some(Natural(4))) test "Search in an int array matching with an invalid value": var arr = [0, 3, 1, 4, 5, 6] - check linearSearch(arr, 7) == none(Natural) - check recursiveLinearSearch(arr, arr.high, 7) == none(Natural) + 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'] - check linearSearch(arr, '5') == some(Natural(4)) - check recursiveLinearSearch(arr, arr.high, '5') == some(Natural(4)) + 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"] - check linearSearch(arr, "5") == some(Natural(4)) - check recursiveLinearSearch(arr, arr.high, "5") == some(Natural(4)) + checkLinearSearch(arr, "5", some(Natural(4))) From ddb0fa1cc27c08dd6b0790f4c095fd473f4827ee Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 18:50:42 -0700 Subject: [PATCH 33/52] Update linear_search.nim --- searches/linear_search.nim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 91b94c81..747d74cf 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -37,15 +37,14 @@ func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = func recursiveLinearSearch[T](arr: openArray[T], idx: Natural, value: T): Option[Natural] = ## Recursion is another method for linear search ## we can just replace the for loop with recursion. - ## recursion traverses from the end of the array to the front. ## `none(Natural)` is returned when the array is traversed completely ## and no value is matched, or when `arr` is empty and has a length of 0 - if idx <= 0: + if idx >= arr.high: return none(Natural) if arr[idx] == value: return some(idx) - recursiveLinearSearch(arr, idx - 1, value) + recursiveLinearSearch(arr, idx + 1, value) when isMainModule: @@ -53,7 +52,7 @@ when isMainModule: template checkLinearSearch[T](arr: openArray[T], value: T, expectedIndex: Option[Natural]) = check linearSearch(arr, value) == expectedIndex - check recursiveLinearSearch(arr, arr.high, value) == expectedIndex + check recursiveLinearSearch(arr, arr.low, value) == expectedIndex suite "Linear search": test "Search in an empty array": From c883341c715b7e6643077de562c6eb9d25700750 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:03:35 -0700 Subject: [PATCH 34/52] Update linear_search.nim --- searches/linear_search.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 747d74cf..bd95c378 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -27,14 +27,14 @@ runnableExamples: import std/options -func linearSearch[T](arr: openArray[T], key: T): Option[Natural] = +func linearSearch*[T](arr: openArray[T], key: T): Option[Natural] = ## key is the value for matching 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], idx: Natural, value: T): Option[Natural] = +func recursiveLinearSearch*[T](arr: openArray[T], idx: Natural, value: T): Option[Natural] = ## Recursion is another method for linear search ## we can just replace the for loop with recursion. From 326a2b1db54c1754c2021237eac6a5bfa071b492 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:14:49 -0700 Subject: [PATCH 35/52] Update linear_search.nim --- searches/linear_search.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index bd95c378..205d0e66 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -34,7 +34,7 @@ func linearSearch*[T](arr: openArray[T], key: T): Option[Natural] = return some(Natural(i)) none(Natural) # `key` not found -func recursiveLinearSearch*[T](arr: openArray[T], idx: Natural, value: T): Option[Natural] = +func recursiveLinearSearch*[T](arr: openArray[T], value: T, idx: Natural = arr.low.Natural): Option[Natural] = ## Recursion is another method for linear search ## we can just replace the for loop with recursion. @@ -44,7 +44,7 @@ func recursiveLinearSearch*[T](arr: openArray[T], idx: Natural, value: T): Optio return none(Natural) if arr[idx] == value: return some(idx) - recursiveLinearSearch(arr, idx + 1, value) + recursiveLinearSearch(arr, value, idx + 1) when isMainModule: @@ -52,7 +52,7 @@ when isMainModule: template checkLinearSearch[T](arr: openArray[T], value: T, expectedIndex: Option[Natural]) = check linearSearch(arr, value) == expectedIndex - check recursiveLinearSearch(arr, arr.low, value) == expectedIndex + check recursiveLinearSearch(arr, value) == expectedIndex suite "Linear search": test "Search in an empty array": From 370f94c33f5561a901cb950219099377f63490ba Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:18:04 -0700 Subject: [PATCH 36/52] Update linear_search.nim --- searches/linear_search.nim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 205d0e66..13db29f9 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -28,18 +28,18 @@ runnableExamples: import std/options func linearSearch*[T](arr: openArray[T], key: T): Option[Natural] = - ## key is the value for matching in the array + # key is the value for matching 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], value: T, idx: Natural = arr.low.Natural): Option[Natural] = - ## Recursion is another method for linear search - ## we can just replace the for loop with recursion. + # Recursion is another method for linear search + # we can just replace the for loop with recursion. - ## `none(Natural)` is returned when the array is traversed completely - ## and no value is matched, or when `arr` is empty and has a length of 0 + # `none(Natural)` is returned when the array is traversed completely + # and no value is matched, or when `arr` is empty and has a length of 0 if idx >= arr.high: return none(Natural) if arr[idx] == value: From 54315765080a49c5f7399530442033d073d09691 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:21:52 -0700 Subject: [PATCH 37/52] Update linear_search.nim --- searches/linear_search.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 13db29f9..6e66604a 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -18,11 +18,11 @@ runnableExamples: var arr1 = [0, 3, 1, 4, 5, 6] doAssert linearSearch(arr1, 5) == some(Natural(4)) - doAssert recursiveLinearSearch(arr1, arr1.high, 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, arr2.high, '5') == some(Natural(4)) + doAssert recursiveLinearSearch(arr2, '5') == some(Natural(4)) import std/options From a5a1915becf08d9cda8fd9be2cad444ff46b8f16 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:39:35 -0700 Subject: [PATCH 38/52] Update linear_search.nim Shortening line 38 using type: OptNat = Option[Natural] --- searches/linear_search.nim | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 6e66604a..67f67c6e 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -27,32 +27,36 @@ runnableExamples: import std/options -func linearSearch*[T](arr: openArray[T], key: T): Option[Natural] = +type + Nat = Natural + OptNat = Option[Natural] + +func linearSearch*[T](arr: openArray[T], key: T): OptNat = # key is the value for matching 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], value: T, idx: Natural = arr.low.Natural): Option[Natural] = +func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat): OptNat= # Recursion is another method for linear search # we can just replace the for loop with recursion. # `none(Natural)` is returned when the array is traversed completely - # and no value is matched, or when `arr` is empty and has a length of 0 + # and no key is matched, or when `arr` is empty and has a length of 0 if idx >= arr.high: return none(Natural) - if arr[idx] == value: + if arr[idx] == key: return some(idx) - recursiveLinearSearch(arr, value, idx + 1) + recursiveLinearSearch(arr, key, idx + 1) when isMainModule: import unittest - template checkLinearSearch[T](arr: openArray[T], value: T, expectedIndex: Option[Natural]) = - check linearSearch(arr, value) == expectedIndex - check recursiveLinearSearch(arr, value) == expectedIndex + template checkLinearSearch[T](arr: openArray[T], key: T, expectedIndex: Option[Natural]) = + check linearSearch(arr, key) == expectedIndex + check recursiveLinearSearch(arr, key) == expectedIndex suite "Linear search": test "Search in an empty array": From 5ebd8df9a1120fc3c7c586771d01af638dd87f3c Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:25:45 -0700 Subject: [PATCH 39/52] Update linear_search.nim --- searches/linear_search.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 67f67c6e..e96a8bf7 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -54,9 +54,9 @@ func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat when isMainModule: import unittest - template checkLinearSearch[T](arr: openArray[T], key: T, expectedIndex: Option[Natural]) = - check linearSearch(arr, key) == expectedIndex - check recursiveLinearSearch(arr, key) == expectedIndex + template checkLinearSearch[T](arr: openArray[T], key: T, expectedIdx: OptNat = + check linearSearch(arr, key) == expectedIdx + check recursiveLinearSearch(arr, key) == expectedIdx suite "Linear search": test "Search in an empty array": From ad14be76ebd38a948b06e232b05dea7f1059f70a Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 13:48:39 +0800 Subject: [PATCH 40/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index e96a8bf7..68958949 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -44,7 +44,7 @@ func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat # `none(Natural)` is returned when the array is traversed completely # and no key is matched, or when `arr` is empty and has a length of 0 - if idx >= arr.high: + if idx > arr.high: return none(Natural) if arr[idx] == key: return some(idx) From 31590c9b94b0cecdda9a848af232f7bf8b9f9512 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 13:48:58 +0800 Subject: [PATCH 41/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 68958949..35ac6870 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -54,7 +54,7 @@ func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat when isMainModule: import unittest - template checkLinearSearch[T](arr: openArray[T], key: T, expectedIdx: OptNat = + template checkLinearSearch[T](arr: openArray[T], key: T, expectedIdx: OptNat): untyped = check linearSearch(arr, key) == expectedIdx check recursiveLinearSearch(arr, key) == expectedIdx From 41f383b398533e755b2584d197f63ce46034b188 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 01:07:17 -0700 Subject: [PATCH 42/52] Update linear_search.nim --- searches/linear_search.nim | 5 ----- 1 file changed, 5 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 35ac6870..27f887db 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -61,25 +61,20 @@ when isMainModule: 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 matching with an invalid 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))) From b769cfe99d47a470dde5deec639947afe36ae218 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 02:40:17 -0700 Subject: [PATCH 43/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 27f887db..e331a7e0 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -67,7 +67,7 @@ when isMainModule: var arr = [0, 3, 1, 4, 5, 6] checkLinearSearch(arr, 5, some(Natural(4))) - test "Search in an int array matching with an invalid value": + test "Search in an int array for a missing value": var arr = [0, 3, 1, 4, 5, 6] checkLinearSearch(arr, 7, none(Natural)) From c0269ff076fd73b7827d4e216647d9b535d9f83d Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:08:38 -0700 Subject: [PATCH 44/52] Update linear_search.nim --- searches/linear_search.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index e331a7e0..4e029f8c 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -1,6 +1,6 @@ ## Linear Search ## ============= -## Linear search is the simplest but least efficient algorithm +## Linear search is the simplest but least efficient searching algorithm ## to search for an element in a data set. ## It examines each element until it finds a match, ## starting at the beginning of the data set for the iterative version, @@ -78,3 +78,7 @@ when isMainModule: 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))) From 5e912907a5db1680d1722fea0a400b7341f68b7a Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:13:38 -0700 Subject: [PATCH 45/52] Update linear_search.nim --- searches/linear_search.nim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 4e029f8c..e9fac2d1 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -3,9 +3,8 @@ ## Linear search is the simplest but least efficient searching algorithm ## to search for an element in a data set. ## It examines each element until it finds a match, -## starting at the beginning of the data set for the iterative version, -## or in the opposite direction for the recursive version, as implemented in this module. -## The search is finished and terminated once the target element is located. +## 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://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm ## ## Time Complexity: O(N) From 02d2c827b24159b554c9a51a3f3207195cd49933 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:14:16 -0700 Subject: [PATCH 46/52] Update linear_search.nim --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index e9fac2d1..6c1b2f21 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -7,7 +7,7 @@ ## The search ends when the element is located or when the end of the array is reached. ## https://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm ## -## Time Complexity: O(N) +## Time Complexity: O(n) ## Space Complexity in for-loop linear search: O(1) ## Space Complexity in recursive linear search: O(n) ## This is because of the call stack operation, From 1c572911fb26cd3c5bcccd459f30f92f068c844d Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:17:49 -0700 Subject: [PATCH 47/52] Update linear_search.nim --- searches/linear_search.nim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 6c1b2f21..d7eeb518 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -10,9 +10,10 @@ ## Time Complexity: O(n) ## Space Complexity in for-loop linear search: O(1) ## Space Complexity in recursive linear search: O(n) -## This is because of the call stack operation, -## and there is no CPS automatic optimization in Nim -## CPS: https://en.wikipedia.org/wiki/Continuation-passing_style +## 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] From 5f9abc155ecd4251dee3ac750fc98572062af8ad Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:24:39 -0700 Subject: [PATCH 48/52] Update linear_search.nim --- searches/linear_search.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index d7eeb518..83256238 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -5,7 +5,7 @@ ## 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://www.simplilearn.com/tutorials/data-structure-tutorial/linear-search-algorithm +## https://en.wikipedia.org/wiki/Linear_search ## ## Time Complexity: O(n) ## Space Complexity in for-loop linear search: O(1) @@ -24,6 +24,10 @@ runnableExamples: 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 From 209bc48281745800cb1cb3d86b899b8e7fcef18c Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:28:12 -0700 Subject: [PATCH 49/52] Update linear_search.nim --- searches/linear_search.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 83256238..8ec6c61b 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -36,18 +36,18 @@ type OptNat = Option[Natural] func linearSearch*[T](arr: openArray[T], key: T): OptNat = - # key is the value for matching in the array + # 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 - # we can just replace the for loop with recursion. + # 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 and has a length of 0 + # and no key is matched, or when `arr` is empty. if idx > arr.high: return none(Natural) if arr[idx] == key: From 410510e26dcefe0b176fd5ff56d9f911192edc08 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:42:44 -0700 Subject: [PATCH 50/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 8ec6c61b..6e7149f7 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -7,7 +7,7 @@ ## 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) +## 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, From e13d2d3bf39937a25c0965e8493f0ee9e7a29b62 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:44:51 -0700 Subject: [PATCH 51/52] Update DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6f296bd6..3f37dde1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -8,3 +8,6 @@ ## Strings * [Check Anagram](strings/check_anagram.nim) + +## Searches + * [Linear Search](searches/linear_search.nim) From 4c7bc001461f10ce832efbee47ea041df50cdbd6 Mon Sep 17 00:00:00 2001 From: Satin Wuker <74630829+SatinWuker@users.noreply.github.com> Date: Tue, 20 Jun 2023 03:45:15 -0700 Subject: [PATCH 52/52] Update searches/linear_search.nim Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- searches/linear_search.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.nim b/searches/linear_search.nim index 6e7149f7..ae2a7484 100644 --- a/searches/linear_search.nim +++ b/searches/linear_search.nim @@ -1,7 +1,7 @@ ## Linear Search ## ============= ## Linear search is the simplest but least efficient searching algorithm -## to search for an element in a data set. +## 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.