From 90e23a6790f1f0a0e3704314a08be02114bcdf11 Mon Sep 17 00:00:00 2001 From: Coskun Deniz Date: Sat, 21 Aug 2021 19:44:58 +0100 Subject: [PATCH 1/5] declare 'index' as variable --- Insertion Sort/InsertionSort.swift | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Insertion Sort/InsertionSort.swift b/Insertion Sort/InsertionSort.swift index e24ede905..5f0b6c2b4 100644 --- a/Insertion Sort/InsertionSort.swift +++ b/Insertion Sort/InsertionSort.swift @@ -28,14 +28,13 @@ func insertionSort(_ array: [T]) -> [T] { guard array.count > 1 else { return array } var sortedArray = array - for index in 1.. 0, temp < sortedArray[currentIndex - 1] { - sortedArray[currentIndex] = sortedArray[currentIndex - 1] - currentIndex -= 1 + for var index in 1.. 0, temp < sortedArray[index - 1] { + sortedArray[index] = sortedArray[index - 1] + index -= 1 } - sortedArray[currentIndex] = temp + sortedArray[index] = temp } return sortedArray } From 5ad2c68e359c6676df6b3b95315fe8d2b8d70d81 Mon Sep 17 00:00:00 2001 From: Aaron Watkins Date: Fri, 26 Nov 2021 16:35:21 -0500 Subject: [PATCH 2/5] legacy variables names correct legacy variable name `x` to be `index` so reader isnt wasting time scratching their head and can focus more on understanding the insertion sort algorithm. --- Insertion Sort/README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Insertion Sort/README.markdown b/Insertion Sort/README.markdown index 1c41aa91d..f37bfeaef 100644 --- a/Insertion Sort/README.markdown +++ b/Insertion Sort/README.markdown @@ -116,9 +116,9 @@ Here is how the code works. 1. Make a copy of the array. This is necessary because we cannot modify the contents of the `array` parameter directly. Like Swift's own `sorted()`, the `insertionSort()` function will return a sorted *copy* of the original array. -2. There are two loops inside this function. The outer loop looks at each of the elements in the array in turn; this is what picks the top-most number from the pile. The variable `x` is the index of where the sorted portion ends and the pile begins (the position of the `|` bar). Remember, at any given moment the beginning of the array -- from index 0 up to `x` -- is always sorted. The rest, from index `x` until the last element, is the unsorted pile. +2. There are two loops inside this function. The outer loop looks at each of the elements in the array in turn; this is what picks the top-most number from the pile. The variable `index` is the index of where the sorted portion ends and the pile begins (the position of the `|` bar). Remember, at any given moment the beginning of the array -- from index 0 up to `index` -- is always sorted. The rest, from index `index` until the last element, is the unsorted pile. -3. The inner loop looks at the element at position `x`. This is the number at the top of the pile, and it may be smaller than any of the previous elements. The inner loop steps backwards through the sorted array; every time it finds a previous number that is larger, it swaps them. When the inner loop completes, the beginning of the array is sorted again, and the sorted portion has grown by one element. +3. The inner loop looks at the element at position `index`. This is the number at the top of the pile, and it may be smaller than any of the previous elements. The inner loop steps backwards through the sorted array; every time it finds a previous number that is larger, it swaps them. When the inner loop completes, the beginning of the array is sorted again, and the sorted portion has grown by one element. > **Note:** The outer loop starts at index 1, not 0. Moving the very first element from the pile to the sorted portion doesn't actually change anything, so we might as well skip it. From eb4e151b55ff846c28be5d2357b7ab2f776a9dce Mon Sep 17 00:00:00 2001 From: Aaron Watkins Date: Fri, 26 Nov 2021 16:45:21 -0500 Subject: [PATCH 3/5] correct index to currentIndex --- Insertion Sort/README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Insertion Sort/README.markdown b/Insertion Sort/README.markdown index f37bfeaef..f7b933b92 100644 --- a/Insertion Sort/README.markdown +++ b/Insertion Sort/README.markdown @@ -116,9 +116,9 @@ Here is how the code works. 1. Make a copy of the array. This is necessary because we cannot modify the contents of the `array` parameter directly. Like Swift's own `sorted()`, the `insertionSort()` function will return a sorted *copy* of the original array. -2. There are two loops inside this function. The outer loop looks at each of the elements in the array in turn; this is what picks the top-most number from the pile. The variable `index` is the index of where the sorted portion ends and the pile begins (the position of the `|` bar). Remember, at any given moment the beginning of the array -- from index 0 up to `index` -- is always sorted. The rest, from index `index` until the last element, is the unsorted pile. +2. There are two loops inside this function. The outer loop looks at each of the elements in the array in turn; this is what picks the top-most number from the pile. The variable `currentIndex` is the index of where the sorted portion ends and the pile begins (the position of the `|` bar). Remember, at any given moment the beginning of the array -- from index 0 up to `currentIndex` -- is always sorted. The rest, from index `currentIndex` until the last element, is the unsorted pile. -3. The inner loop looks at the element at position `index`. This is the number at the top of the pile, and it may be smaller than any of the previous elements. The inner loop steps backwards through the sorted array; every time it finds a previous number that is larger, it swaps them. When the inner loop completes, the beginning of the array is sorted again, and the sorted portion has grown by one element. +3. The inner loop looks at the element at position `currentIndex`. This is the number at the top of the pile, and it may be smaller than any of the previous elements. The inner loop steps backwards through the sorted array; every time it finds a previous number that is larger, it swaps them. When the inner loop completes, the beginning of the array is sorted again, and the sorted portion has grown by one element. > **Note:** The outer loop starts at index 1, not 0. Moving the very first element from the pile to the sorted portion doesn't actually change anything, so we might as well skip it. From 6af57a839d59db84ded9e95315d4a9aeff8a9d64 Mon Sep 17 00:00:00 2001 From: Namratha2604 Date: Thu, 26 Sep 2024 14:25:05 +0530 Subject: [PATCH 4/5] #986-corrected the comment --- Bloom Filter/BloomFilter.playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bloom Filter/BloomFilter.playground/Contents.swift b/Bloom Filter/BloomFilter.playground/Contents.swift index 7a3719d57..8ed808a06 100644 --- a/Bloom Filter/BloomFilter.playground/Contents.swift +++ b/Bloom Filter/BloomFilter.playground/Contents.swift @@ -79,4 +79,4 @@ bloom.insert("Bloom Filterz") print(bloom.array) bloom.query("Bloom Filterz") // true -bloom.query("Hello WORLD") // true +bloom.query("Hello WORLD") // false or true: It may return true due to a false positive, but it's not guaranteed. From 5e8c02913c04d86d3b41ebf47210b9f974ad7d26 Mon Sep 17 00:00:00 2001 From: Nitesh J Date: Mon, 7 Oct 2024 14:05:17 +0530 Subject: [PATCH 5/5] issue resolved #997 and #998 Correction of the README.markdown and Selection Sampling code improvement #998 --- Selection Sampling/README.markdown | 2 +- Selection Sampling/SelectionSampling.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Selection Sampling/README.markdown b/Selection Sampling/README.markdown index 741f592f4..5c2ff5e25 100644 --- a/Selection Sampling/README.markdown +++ b/Selection Sampling/README.markdown @@ -12,7 +12,7 @@ func select(from a: [T], count k: Int) -> [T] { for i in 0..(from a: [T], count k: Int) -> [T] { for i in 0..