forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of find kth element in array (TheAlgorithms#472) (…
…TheAlgorithms#473) * feat: implementation of find kth element in array (TheAlgorithms#472) * feat: implementation of find kth element in array * feat: implementation of find kth element in array * Updated Documentation in README.md Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Rak Laptudirm <[email protected]>
- Loading branch information
1 parent
4f29592
commit b875ba7
Showing
4 changed files
with
161 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,43 @@ | ||
package math | ||
|
||
import ( | ||
"github.com/TheAlgorithms/Go/search" | ||
"github.com/TheAlgorithms/Go/sort" | ||
) | ||
|
||
// FindKthMax returns the kth large element given an integer slice | ||
// with nil `error` if found and returns -1 with `error` `search.ErrNotFound` | ||
// if not found. NOTE: The `nums` slice gets mutated in the process. | ||
func FindKthMax(nums []int, k int) (int, error) { | ||
index := len(nums) - k | ||
return kthNumber(nums, index) | ||
} | ||
|
||
// FindKthMin returns kth small element given an integer slice | ||
// with nil `error` if found and returns -1 with `error` `search.ErrNotFound` | ||
// if not found. NOTE: The `nums` slice gets mutated in the process. | ||
func FindKthMin(nums []int, k int) (int, error) { | ||
index := k - 1 | ||
return kthNumber(nums, index) | ||
} | ||
|
||
// kthNumber use the selection algorithm (based on the partition method - the same one as used in quicksort). | ||
func kthNumber(nums []int, k int) (int, error) { | ||
if k < 0 || k >= len(nums) { | ||
return -1, search.ErrNotFound | ||
} | ||
start := 0 | ||
end := len(nums) - 1 | ||
for start <= end { | ||
pivot := sort.Partition(nums, start, end) | ||
if k == pivot { | ||
return nums[pivot], nil | ||
} | ||
if k > pivot { | ||
start = pivot + 1 | ||
continue | ||
} | ||
end = pivot - 1 | ||
} | ||
return -1, search.ErrNotFound | ||
} |
This file contains 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,104 @@ | ||
package math | ||
|
||
import ( | ||
"github.com/TheAlgorithms/Go/search" | ||
"testing" | ||
) | ||
|
||
func TestFindKthMax(t *testing.T) { | ||
sortTests := []struct { | ||
input []int | ||
k int | ||
expected int | ||
err error | ||
name string | ||
}{ | ||
{ | ||
input: []int{6, 7, 0, -1, 10, 70, 8, 22, 3, 9}, | ||
k: 3, | ||
expected: 10, | ||
name: "3th largest number", | ||
}, | ||
{ | ||
input: []int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, | ||
k: 3, | ||
expected: -1, | ||
name: "3th largest number", | ||
}, | ||
{ | ||
input: []int{-1, -1, -1, -1, -1, -1}, | ||
k: 7, | ||
expected: -1, | ||
err: search.ErrNotFound, | ||
name: "This should be an error", | ||
}, | ||
{ | ||
input: []int{}, | ||
k: 1, | ||
expected: -1, | ||
err: search.ErrNotFound, | ||
name: "This should be an error", | ||
}, | ||
} | ||
for _, test := range sortTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
actual, err := FindKthMax(test.input, test.k) | ||
if err != test.err { | ||
t.Errorf("name:%v FindKthMax() = %v, want err: %v", test.name, err, test.err) | ||
} | ||
if actual != test.expected { | ||
t.Errorf("test %s failed", test.name) | ||
t.Errorf("actual %v expected %v", actual, test.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFindKthMin(t *testing.T) { | ||
sortTests := []struct { | ||
input []int | ||
k int | ||
expected int | ||
err error | ||
name string | ||
}{ | ||
{ | ||
input: []int{6, 7, 0, -1, 10, 70, 8, 22, 3, 9}, | ||
k: 3, | ||
expected: 3, | ||
name: "3th smallest number", | ||
}, | ||
{ | ||
input: []int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, | ||
k: 3, | ||
expected: -1, | ||
name: "3th smallest number", | ||
}, | ||
{ | ||
input: []int{-1, -1, -1, -1, -1, -1}, | ||
k: 7, | ||
expected: -1, | ||
err: search.ErrNotFound, | ||
name: "This should be an error", | ||
}, | ||
{ | ||
input: []int{}, | ||
k: 1, | ||
expected: -1, | ||
err: search.ErrNotFound, | ||
name: "This should be an error", | ||
}, | ||
} | ||
for _, test := range sortTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
actual, err := FindKthMin(test.input, test.k) | ||
if err != test.err { | ||
t.Errorf("name:%v FindKthMin() = %v, want err: %v", test.name, err, test.err) | ||
} | ||
if actual != test.expected { | ||
t.Errorf("test %s failed", test.name) | ||
t.Errorf("actual %v expected %v", actual, test.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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