mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 10:20:40 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
// File: binary_search_test.go
|
|
// Created Time: 2022-12-05
|
|
// Author: Slone123c (274325721@qq.com)
|
|
|
|
package chapter_searching
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestBinarySearch(t *testing.T) {
|
|
var (
|
|
target = 6
|
|
nums = []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
|
expected = 2
|
|
)
|
|
// Perform binary search in array
|
|
actual := binarySearch(nums, target)
|
|
fmt.Println("Index of target element 6 =", actual)
|
|
if actual != expected {
|
|
t.Errorf("Index of target element 6 = %d, should be %d", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestBinarySearchEdge(t *testing.T) {
|
|
// Array with duplicate elements
|
|
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
|
fmt.Println("\nArray nums = ", nums)
|
|
|
|
// Binary search left and right boundaries
|
|
for _, target := range []int{6, 7} {
|
|
index := binarySearchLeftEdge(nums, target)
|
|
fmt.Println("Leftmost element", target, " index is", index)
|
|
|
|
index = binarySearchRightEdge(nums, target)
|
|
fmt.Println("Rightmost element", target, " index is", index)
|
|
}
|
|
}
|
|
|
|
func TestBinarySearchInsertion(t *testing.T) {
|
|
// Array without duplicate elements
|
|
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
|
fmt.Println("Array nums =", nums)
|
|
|
|
// Binary search for insertion point
|
|
for _, target := range []int{6, 9} {
|
|
index := binarySearchInsertionSimple(nums, target)
|
|
fmt.Println("Element", target, " insertion point index is", index)
|
|
}
|
|
|
|
// Array with duplicate elements
|
|
nums = []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
|
|
fmt.Println("\nArray nums =", nums)
|
|
|
|
// Binary search for insertion point
|
|
for _, target := range []int{2, 6, 20} {
|
|
index := binarySearchInsertion(nums, target)
|
|
fmt.Println("Element", target, " insertion point index is", index)
|
|
}
|
|
}
|