Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson16-diy1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a4297c12
authored
Apr 23, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
b250abfe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
126 additions
and
2 deletions
binary_search.py
defin.py
hexin.py
t.py
test.py
binary_search.py
View file @
a4297c12
# 使用二分查找法,找出9和20在列表里面的索引
num_list
=
[
1
,
3
,
5
,
8
,
11
,
15
,
17
,
18
,
20
,
21
]
\ No newline at end of file
num_list
=
[
1
,
3
,
5
,
8
,
11
,
15
,
17
,
18
,
20
,
21
]
#要查找的数
goal1
=
9
goal2
=
20
low
=
0
high
=
len
(
num_list
)
-
1
while
low
<=
high
:
mid
=
(
low
+
high
)
//
2
num
=
num_list
[
mid
]
if
num
==
goal1
:
print
(
"索引:"
,
mid
)
break
elif
num
<
goal1
:
low
=
mid
+
1
else
:
high
=
mid
-
1
defin.py
0 → 100644
View file @
a4297c12
# 使用二分查找法,找出9和20在列表里面的索引
num_list
=
[
1
,
3
,
5
,
8
,
11
,
15
,
17
,
18
,
20
,
21
]
'''
#要查找的数
goal1 = 9
goal2 = 20
low = 0
high = len(num_list)-1
while low<=high:
mid = (low+high)//2
num = num_list[mid]
if num == goal1:
print("索引:",mid)
break
elif num<goal1:
low = mid+1
else:
high = mid-1
'''
def
erfen
(
alist
,
number
):
low
=
0
high
=
len
(
alist
)
-
1
while
low
<=
high
:
mid
=
(
low
+
high
)
//
2
num
=
alist
[
mid
]
if
num
==
number
:
print
(
"索引:"
,
mid
)
return
mid
#break
elif
num
<
number
:
low
=
mid
+
1
else
:
high
=
mid
-
1
return
None
res
=
erfen
(
num_list
,
2
)
print
(
res
)
hexin.py
0 → 100644
View file @
a4297c12
#主要逻辑
mid
=
(
low
+
high
)
//
2
if
中间值
==
查找对象
:
找到了
elif
中间值
<
查找对象
:
low
=
mid
+
1
else
:
high
=
mid
-
1
\ No newline at end of file
t.py
0 → 100644
View file @
a4297c12
'''
#主要逻辑
mid = (low+high)//2
if 中间值==查找对象:
找到了
elif 中间值<查找对象:
low = mid+1
else:
high = mid-1
'''
# 使用二分查找法,找出9和20在列表里面的索引
num_list
=
[
1
,
3
,
5
,
8
,
11
,
15
,
17
,
18
,
20
,
21
]
goal
=
20
low
=
0
high
=
len
(
num_list
)
-
1
while
low
<=
high
:
mid
=
(
low
+
high
)
//
2
num
=
num_list
[
mid
]
if
num
==
goal
:
print
(
"索引"
,
mid
)
break
elif
num
<
goal
:
low
=
mid
+
1
else
:
high
=
mid
-
1
test.py
0 → 100644
View file @
a4297c12
'''
题目:
有一个无序的序列list_c = [37, 99, 73, 48, 47, 40, 40, 25, 99, 51], 请先排序并打印输出。
分别尝试插入[20,40,41,100]到序列中合适的位置,保证其有序。
'''
list_c
=
[
37
,
99
,
73
,
48
,
47
,
40
,
40
,
25
,
99
,
51
,
99
]
list_s
=
[
20
,
40
,
41
,
100
]
#先排序
order_list
=
sorted
(
list_c
)
print
(
order_list
)
#浅复制,保留原来数据
ret
=
order_list
[:]
def
insert_sort
(
ret
,
num
):
low
=
0
#索引起点
high
=
len
(
ret
)
#索引终点
while
low
<
high
:
# 起点索引大于或等于列表最后一个元素的索引,条件无法进入。
mid
=
(
low
+
high
)
//
2
# 取列表中间点的索引
if
num
<
ret
[
mid
]:
# 条件判断,如果待插入列表的值小于有序列表对应mid索引的值,就向左查询
high
=
mid
-
1
# 同时确认这个中间点mid索引就是ret列表的high最高索引
else
:
# 如果待插入列表的值大于有序列表对应mid索引的值,就向右查询,求中点
low
=
mid
+
1
# 同时确认这个中间点mid索引就是ret列表的开始索引low的值。
return
low
# 这个开始索引low就是最终计算出来的待插入索引的位置low,所以返回low
for
num
in
list_s
:
# 循环去取待插入列表的元素。
index
=
insert_sort
(
ret
,
num
)
# 调用上面的函数取出插入元素索引值
ret
.
insert
(
index
,
num
)
# 就地插入对应索引的值到列表ret中.
print
(
ret
)
# 打印插入元素后的列表
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment