Skip to content

Commit 24d72d7

Browse files
committed
2017.4.25
Add Search Algorithm
1 parent 624ef90 commit 24d72d7

File tree

11 files changed

+272
-3
lines changed

11 files changed

+272
-3
lines changed

Algorithm/Search/Binary_Search/binary_search.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package Binary_Search
88

9+
import "fmt"
10+
911
// 二分查找首先要数组有序
1012
func Binary_Search(nums []int, key int) int {
1113
left, right := 0, len(nums)-1
@@ -16,8 +18,10 @@ func Binary_Search(nums []int, key int) int {
1618
} else if key > nums[middle] {
1719
left = middle + 1
1820
} else {
21+
fmt.Println("Find", key, "in", middle)
1922
return middle
2023
}
2124
}
25+
fmt.Println("Not Find")
2226
return -1
2327
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Author: Juntaran
3+
* Email: Jacinthmail@gmail.com
4+
* Date: 2017/4/25 14:13
5+
*/
6+
7+
package Binary_Tree_Search
8+
9+
import (
10+
"Golang_Algorithm/Data_Structure/Binary_Tree"
11+
"fmt"
12+
)
13+
14+
/*
15+
递归查找二叉排序树T中是否存在key,指针f指向T的双亲,其初始调用值为NULL
16+
若查找成功,则指针p指向该数据元素结点,并返回1
17+
否则指针p指向查找路径上访问的最后一个结点并返回0
18+
*/
19+
20+
func Binary_Tree_Search(nums []int, key int) bool {
21+
bst := Binary_Tree.NewTree(nil)
22+
for i := 0; i < len(nums); i++ {
23+
bst.Insert(nums[i])
24+
}
25+
//bst.LevelTraverse()
26+
_, err := bst.Search(key)
27+
if err != nil {
28+
fmt.Println("Not Find")
29+
return false
30+
} else {
31+
fmt.Println("Find key")
32+
return true
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Author: Juntaran
3+
* Email: Jacinthmail@gmail.com
4+
* Date: 2017/4/25 14:16
5+
*/
6+
7+
package main
8+
9+
import "Golang_Algorithm/Algorithm/Search/Binary_Tree_Search"
10+
11+
func main() {
12+
var nums = []int{7, 3, 2, 8, 10, 3, 2, 5, 6, 7}
13+
Binary_Tree_Search.Binary_Tree_Search(nums, 6)
14+
}

Algorithm/Search/Fibonacci_Search/fibonacci_search.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package Fibonacci_Search
88

9+
import "fmt"
10+
911
// 斐波那契查找
1012
func Fibonacci_Search(nums []int, key int) int {
1113
// 斐波那契数列
@@ -22,7 +24,7 @@ func Fibonacci_Search(nums []int, key int) int {
2224
k ++
2325
}
2426
// 扩展nums到Fibonacci[k]-1的长度
25-
var temp = new([Fibonacci[k]-1]int)
27+
var temp = make([]int, Fibonacci[k]-1)
2628
for i := 0; i < len(nums); i++ {
2729
temp[i] = nums[i]
2830
}
@@ -42,11 +44,14 @@ func Fibonacci_Search(nums []int, key int) int {
4244
k = k - 2
4345
} else {
4446
if middle < len(nums) {
47+
fmt.Println("Find", key, "in", middle)
4548
return middle
4649
} else {
50+
fmt.Println("Find", key, "in", len(nums)-1)
4751
return len(nums) - 1
4852
}
4953
}
5054
}
55+
fmt.Println("Not Find")
5156
return -1
5257
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Author: Juntaran
3+
* Email: Jacinthmail@gmail.com
4+
* Date: 2017/4/25 14:32
5+
*/
6+
7+
package Hash_Search
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
const numkey int = -32768
14+
15+
type hashTable struct {
16+
element []int // 数据元素存储基址,动态分配切片
17+
count int // 当前数据元素个数
18+
}
19+
20+
// 创建一个哈希表
21+
func newTable() *hashTable {
22+
newHashTable := &hashTable {
23+
element: make([]int, 5),
24+
count: 0,
25+
}
26+
return newHashTable
27+
}
28+
29+
// 初始化哈希表
30+
func initHashTable(table *hashTable, size int) {
31+
table.count = size
32+
table.element = make([]int, size)
33+
for i := 0; i < size; i++ {
34+
table.element[i] = numkey
35+
}
36+
}
37+
38+
// 哈希函数
39+
func hash(key int, size int) int {
40+
// 除留余数法
41+
return key % size
42+
}
43+
44+
// 插入数据
45+
func insertHash(table *hashTable, key int) {
46+
// 获取哈希地址
47+
addr := hash(key, table.count)
48+
// 如果不为空,则冲突
49+
for table.element[addr] != numkey {
50+
// 开放定址法的线性探测
51+
addr = (addr + 1) % table.count
52+
}
53+
// 找到空位后插入
54+
table.element[addr] = key
55+
}
56+
57+
// 在哈希表中查找
58+
func search(table *hashTable, key int) (bool, int) {
59+
// 求哈希地址
60+
addr := hash(key, table.count)
61+
// 冲突
62+
for table.element[addr] != key {
63+
// 线性探测
64+
addr = (addr + 1) % table.count
65+
if table.element[addr] == numkey || addr == hash(key, table.count) {
66+
return false, -1
67+
}
68+
}
69+
return true, addr
70+
}
71+
72+
func Hash_Search(nums []int, key int) {
73+
table := newTable()
74+
initHashTable(table, len(nums))
75+
for i := 0; i < len(nums); i++ {
76+
insertHash(table, nums[i])
77+
}
78+
ret, addr := search(table, key)
79+
if ret == false {
80+
fmt.Println("Not Find")
81+
} else {
82+
fmt.Println("Find", key, "in", addr)
83+
}
84+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Author: Juntaran
3+
* Email: Jacinthmail@gmail.com
4+
* Date: 2017/4/25 15:07
5+
*/
6+
7+
package main
8+
9+
import "Golang_Algorithm/Algorithm/Search/Hash_Search"
10+
11+
func main() {
12+
var nums = []int{7, 3, 2, 8, 10, 3, 2, 5, 6, 7}
13+
Hash_Search.Hash_Search(nums, 3)
14+
}

Algorithm/Search/Insert_Search/insert_search.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
package Insert_Search
88

9+
import "fmt"
10+
911
// 插值查找
1012
func Insert_Search(nums []int, key int) int {
11-
left, right := 0, len(nums)
13+
left, right := 0, len(nums)-1
1214
for left <= right {
1315
// 差值查找只在middle的取值和二分不同
1416
middle := left + (right-left) * (key-nums[left]) / (nums[right] - nums[left])
@@ -17,8 +19,10 @@ func Insert_Search(nums []int, key int) int {
1719
} else if key > nums[middle] {
1820
left = middle + 1
1921
} else {
22+
fmt.Println("Find", key, "in", middle)
2023
return middle
2124
}
2225
}
26+
fmt.Println("Not Find")
2327
return -1
2428
}

Algorithm/Search/Sequence_Search/sequence_search.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
package Sequence_Search
88

9+
import "fmt"
10+
911
func Sequence_Search(nums []int, key int) int {
1012
for i := 0; i < len(nums); i++ {
1113
if nums[i] == key {
14+
fmt.Println("Find", key, "in", i)
1215
return i
1316
}
1417
}
18+
fmt.Println("Not Find")
1519
return -1
1620
}

Algorithm/Search/Sequence_Search/sequence_search_2.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66

77
package Sequence_Search
88

9+
import "fmt"
10+
911
// 哨兵顺序查找
1012
func Sequence_Search_2(nums []int, key int) int {
1113
if nums[0] == key {
14+
fmt.Println("Find", key, "in 0")
1215
return 0
1316
}
14-
i := len(nums)
17+
i := len(nums)-1
1518
for nums[i] != key && i != 0 {
1619
i --
1720
}
1821
if i == 0 {
22+
fmt.Println("Not Find")
1923
return -1
2024
} else {
25+
fmt.Println("Find", key, "in", i)
2126
return i
2227
}
2328
}

Algorithm/Search/Test/main.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Author: Juntaran
3+
* Email: Jacinthmail@gmail.com
4+
* Date: 2017/4/25 15:19
5+
*/
6+
7+
package main
8+
9+
import (
10+
"time"
11+
"math/rand"
12+
"fmt"
13+
"Golang_Algorithm/Algorithm/Sort/Quick_Sort"
14+
"Golang_Algorithm/Algorithm/Search/Sequence_Search"
15+
"Golang_Algorithm/Algorithm/Search/Binary_Search"
16+
"Golang_Algorithm/Algorithm/Search/Insert_Search"
17+
"Golang_Algorithm/Algorithm/Search/Fibonacci_Search"
18+
"Golang_Algorithm/Algorithm/Search/Binary_Tree_Search"
19+
"Golang_Algorithm/Algorithm/Search/Hash_Search"
20+
)
21+
22+
const Max = 50000 // 随机数生成范围
23+
const Num = 50000 // 随机数生成个数
24+
25+
var start_time time.Time
26+
var during_time time.Duration
27+
28+
func resetNums(initial, nums []int) []int {
29+
for i := 0; i < Num; i++ {
30+
nums[i] = initial[i]
31+
}
32+
return nums
33+
}
34+
35+
func main() {
36+
// initial是无序数组
37+
var initial = make([]int, Num)
38+
rand.Seed(time.Now().UnixNano())
39+
for i := 0; i < Num; i++ {
40+
initial[i] = rand.Intn(Max)
41+
}
42+
fmt.Printf("Initial:\n%v\n", initial)
43+
// nums是有序数组
44+
var nums = make([]int, Num)
45+
nums = resetNums(initial, nums)
46+
Quick_Sort.Quick_Sort_Recurse(nums)
47+
fmt.Printf("Nums:\n%v\n", nums)
48+
49+
var key = initial[Num / 2]
50+
51+
// 顺序查找
52+
fmt.Println("Sequence Search:")
53+
start_time = time.Now()
54+
Sequence_Search.Sequence_Search(initial, key)
55+
during_time = time.Since(start_time)
56+
fmt.Println("Sequence Search Time:", during_time)
57+
58+
// 哨兵顺序查找
59+
fmt.Println("Sequence Search2:")
60+
start_time = time.Now()
61+
Sequence_Search.Sequence_Search_2(initial, key)
62+
during_time = time.Since(start_time)
63+
fmt.Println("Sequence Search2 Time:", during_time)
64+
65+
// 二分查找
66+
fmt.Println("Binary Search:")
67+
start_time = time.Now()
68+
Binary_Search.Binary_Search(nums, key)
69+
during_time = time.Since(start_time)
70+
fmt.Println("Binary Search Time:", during_time)
71+
72+
// 插值查找
73+
fmt.Println("Insert Search:")
74+
start_time = time.Now()
75+
Insert_Search.Insert_Search(nums, key)
76+
during_time = time.Since(start_time)
77+
fmt.Println("Insert Search Time:", during_time)
78+
79+
// 斐波那契查找
80+
fmt.Println("Fibonacci Search:")
81+
start_time = time.Now()
82+
Fibonacci_Search.Fibonacci_Search(nums, key)
83+
during_time = time.Since(start_time)
84+
fmt.Println("Fibonacci Search Time:", during_time)
85+
86+
// 二叉搜索树
87+
fmt.Println("Binary Tree Search")
88+
start_time = time.Now()
89+
Binary_Tree_Search.Binary_Tree_Search(nums, key)
90+
during_time = time.Since(start_time)
91+
fmt.Println("Binary Tree Search Time:", during_time)
92+
93+
// 哈希表查找
94+
fmt.Println("Hash Table Search")
95+
start_time = time.Now()
96+
Hash_Search.Hash_Search(nums, key)
97+
during_time = time.Since(start_time)
98+
fmt.Println("Hash Table Search Time:", during_time)
99+
}

0 commit comments

Comments
 (0)