mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	* feat(go): support binary search edge and testcase * feat(go): support selection sort and testcase * feat(go): support heap sort and testcase * Update selection_sort_test.go * Update selection_sort.go * Update heap_sort.go --------- Co-authored-by: Yudong Jin <krahets@163.com>
		
			
				
	
	
		
			37 lines
		
	
	
		
			938 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			938 B
		
	
	
	
		
			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
 | 
						|
	)
 | 
						|
	// 在数组中执行二分查找
 | 
						|
	actual := binarySearch(nums, target)
 | 
						|
	fmt.Println("目标元素 6 的索引 =", actual)
 | 
						|
	if actual != expected {
 | 
						|
		t.Errorf("目标元素 6 的索引 = %d, 应该为 %d", actual, expected)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestBinarySearchEdge(t *testing.T) {
 | 
						|
	target := 6
 | 
						|
	nums := []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
 | 
						|
	// 二分查找最左一个元素
 | 
						|
	indexLeft := binarySearchLeftEdge(nums, target)
 | 
						|
	fmt.Println("数组中最左一个元素 6 的索引 = ", indexLeft)
 | 
						|
 | 
						|
	// 二分查找最右一个元素
 | 
						|
	indexRight := binarySearchRightEdge(nums, target)
 | 
						|
	fmt.Println("数组中最右一个元素 6 的索引 = ", indexRight)
 | 
						|
}
 |