Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ecd9e7d

Browse files
committed
二分查找
1 parent 1f888e5 commit ecd9e7d

File tree

8 files changed

+150
-10
lines changed

8 files changed

+150
-10
lines changed

‎CMakeLists.txt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(datastruct C)
3-
43
set(CMAKE_C_STANDARD 99)
54

6-
add_executable(dataStruct main.c tools.c tools.h Tree/BinTree.c Tree/BinTree.h Tree/HFTree.c Tree/HFTree.h Graph/AdjGraph.c Graph/AdjGraph.h Graph/AdjLinkedList.c Graph/AdjLinkedList.h)
5+
6+
7+
add_executable(dataStruct main.c tools.c tools.h Tree/BinTree.c Tree/BinTree.h Tree/HFTree.c Tree/HFTree.h Graph/AdjGraph.c Graph/AdjGraph.h Graph/AdjLinkedList.c Graph/AdjLinkedList.h Search/LinklistSearch.c Search/LinklistSearch.h)

‎Graph/AdjLinkedList.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include "AdjLinkedList.h"
66
#include "../tools.h"
77

8-
8+
#define DATASTRUCT_ADJLINKEDLIST_C
9+
#ifndef DATASTRUCT_ADJLINKEDLIST_C
10+
#define DATASTRUCT_ADJLINKEDLIST_C
911
void CreateALGraph(ALGraph *G)
1012
{
1113
FILE *fp;
@@ -60,4 +62,6 @@ void test()
6062

6163
CreateALGraph(G);
6264

63-
}
65+
free(G);
66+
}
67+
#endif

‎Graph/AdjLinkedList.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Created by ys on 2019年4月21日.
33
//
4-
4+
#defineDATASTRUCT_ADJLINKEDLIST_H
55
#ifndef DATASTRUCT_ADJLINKEDLIST_H
66
#define DATASTRUCT_ADJLINKEDLIST_H
77

‎Search/LinklistSearch.c‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// Created by ys on 2019年4月23日.
3+
//
4+
5+
#include "LinklistSearch.h"
6+
#include "../tools.h"
7+
8+
//线性查找元素
9+
int SqListSearch(ElemType *SqList, int key, int len) {
10+
SqList[0] = key;
11+
int i = -1;
12+
for (i = len; SqList[i] != key; --i);
13+
return i;
14+
}
15+
16+
void testSqlistSearch() {
17+
const int N = 1000;
18+
ElemType *SqList = (ElemType *) malloc(sizeof(ElemType) * (N + 1));
19+
for (int i = 1; i <= N; ++i) {
20+
SqList[i] = randint(1, 10000);
21+
}
22+
23+
ElemType key = 200;
24+
int i = SqListSearch(SqList, key, N);
25+
printf("%d\n", i);
26+
for (int i = 0; i <= N; ++i) {
27+
printf("%8d", SqList[i]);
28+
if (i % 10 == 0) {
29+
printf("\n");
30+
}
31+
}
32+
free(SqList);
33+
SqList = NULL;
34+
35+
}
36+
37+
//折半查找
38+
int BinSearch(ElemType *SqList, ElemType key, int low, int high) {
39+
int mid = -1;
40+
while (low <= high) {
41+
mid = (low + high) / 2;
42+
if(SqList[mid] == key)
43+
{
44+
return mid;
45+
}
46+
if(SqList[mid] > key)
47+
{
48+
high = mid - 1;
49+
} else
50+
{
51+
low = mid + 1;
52+
}
53+
}
54+
55+
return -1;
56+
}
57+
58+
//折半查找递归算法
59+
int RBinSearch(ElemType *SqList, int key, int low, int high)
60+
{
61+
if(low > high)
62+
{
63+
return -1;
64+
}
65+
int mid = (low + high) / 2;
66+
if(SqList[mid] == key)
67+
{
68+
return mid;
69+
}
70+
71+
if(SqList[mid] > key)
72+
{
73+
RBinSearch(SqList, key, low, mid - 1);
74+
} else
75+
{
76+
RBinSearch(SqList, key, mid + 1, high);
77+
}
78+
79+
}
80+
81+
void testBSearch()
82+
{
83+
const int N = 1001;
84+
int *List = (int *)malloc(1001 * sizeof(int));
85+
for (int i = 1; i < N; ++i) {
86+
List[i] = i;
87+
}
88+
int key = 74;
89+
90+
int index = BinSearch(List, key, 1, N-1);
91+
92+
printf("查找的index为:%d\n", index);
93+
index = RBinSearch(List, key, 1, N-1);
94+
printf("递归查找的index为:%d\n", index);
95+
96+
free(List);
97+
List = NULL;
98+
}
99+
100+
101+
102+
103+
104+
105+
106+

‎Search/LinklistSearch.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Created by ys on 2019年4月23日.
3+
//
4+
5+
#ifndef DATASTRUCT_LINKLISTSEARCH_H
6+
#define DATASTRUCT_LINKLISTSEARCH_H
7+
//顺序查找法
8+
9+
#define ElemType int
10+
//线性查找
11+
int SqListSearch(ElemType *SqList, int key, int len);
12+
13+
void testSqlistSearch();
14+
15+
16+
//折半查找法
17+
18+
int BinSearch(ElemType *SqList, int key, int low, int high);
19+
20+
//折半查找递归算法
21+
int RBinSearch(ElemType *SqList, int key, int low, int high);
22+
23+
void testBSearch();
24+
25+
#endif //DATASTRUCT_LINKLISTSEARCH_H

‎main.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include <time.h>
55
#include <string.h>
66
#include "tools.h"
7-
#include "Tree/BinTree.h"
8-
#include "Tree/HFTree.h"
9-
#include "Graph/AdjLinkedList.h"
10-
#include "Graph/AdjGraph.h"
117

128

139
int main() {

‎tools.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,4 @@ void printDebug()
134134
printf("debug.........\n");
135135
}
136136

137+

‎tools.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
#include <time.h>
77
#include <string.h>
88

9+
#include "Tree/BinTree.h"
10+
#include "Tree/HFTree.h"
11+
#include "Graph/AdjLinkedList.h"
12+
#include "Graph/AdjGraph.h"
13+
14+
#include "Search/LinklistSearch.h"
15+
916

1017

1118

@@ -17,12 +24,12 @@
1724
void testFp();
1825

1926

20-
2127
#ifndef DEBUG
2228
#define DEBUG
2329

2430
void printDebug();
2531

32+
2633
#endif
2734

2835

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /