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 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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