Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson6-5-1
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
9236e539
authored
Aug 01, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
0db28b07
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
2 deletions
diy.py
diy1.py
diy2.py
diy.py
View file @
9236e539
...
@@ -4,8 +4,10 @@ name='刘强'
...
@@ -4,8 +4,10 @@ name='刘强'
power
=
66
power
=
66
hero
=
[
'赵一'
,
30
,
'丁二'
,
37
,
'孙五'
,
52
,
'王猛'
,
89
,
'周亮'
,
98
]
hero
=
[
'赵一'
,
30
,
'丁二'
,
37
,
'孙五'
,
52
,
'王猛'
,
89
,
'周亮'
,
98
]
# 自动排序
# 自动排序
for
i
in
range
(
len
(
hero
)
):
for
i
in
range
(
1
,
len
(
hero
),
2
):
if
i
%
2
==
1
and
hero
[
i
]
>=
power
:
if
hero
[
i
]
>=
power
:
hero
.
insert
(
i
-
1
,
name
)
hero
.
insert
(
i
-
1
,
name
)
hero
.
insert
(
i
,
power
)
hero
.
insert
(
i
,
power
)
break
print
(
hero
)
print
(
hero
)
print
(
hero
[
-
6
::
2
])
\ No newline at end of file
diy1.py
0 → 100644
View file @
9236e539
s1
=
list
(
input
(
'字符串'
))
for
i
in
range
(
len
(
s1
)):
for
j
in
range
(
i
,
len
(
s1
)):
if
s1
[
i
]
>
s1
[
j
]:
s1
[
i
],
s1
[
j
]
=
s1
[
j
],
s1
[
i
]
print
(
s1
)
low
=
0
high
=
len
(
s1
)
-
1
num
=
ord
(
's'
)
while
True
:
mid
=
low
+
(
high
-
low
)
*
(
num
-
ord
(
s1
[
low
]))
//
(
ord
(
s1
[
high
])
-
ord
(
s1
[
low
]))
if
ord
(
s1
[
mid
])
==
num
:
print
(
mid
)
break
elif
ord
(
s1
[
mid
])
>
num
:
high
=
mid
-
1
elif
low
>
high
:
print
(
'no'
)
break
else
:
low
=
mid
+
1
diy2.py
0 → 100644
View file @
9236e539
#1.创建分治函数
def
MergeSort
(
alist
):
#alist代表的是一个列表
'''
我们需要将alist不断的通过递归分成两个列表left , right 长度小于等于1的时候停止
'''
#设置基线条件 列表的长度小于2
if
len
(
alist
)
<
2
:
#将列表返回
return
alist
#设置num 是切片分割成left 和 right 的分割点 ,num是列表的长度整除2
num
=
len
(
alist
)
//
2
#对左边函数不断的进行递归赋值给列表left
left
=
MergeSort
(
alist
[:
num
])
# 对右边边函数不断的进行递归赋值给列表right
right
=
MergeSort
(
alist
[
num
:])
#对left和right进行归并
return
Merge
(
left
,
right
)
#2 创建归并的函数
def
Merge
(
left
,
right
):
#设置两个指针 指向数据的起始位置 (索引)
l
=
0
r
=
0
#创建一个列表(空间) 用来容纳 left 和right
result
=
[]
#当某一边的指针超过了列表的长度就结束
while
l
<
len
(
left
)
and
r
<
len
(
right
):
#如果左边的列表数据小于右边的列表中的数据 ,就将较小的值添加到result列表中,并且指针要右边移动
if
left
[
l
]
<=
right
[
r
]:
result
.
append
(
left
[
l
])
l
+=
1
else
:
result
.
append
(
right
[
r
])
r
+=
1
#while循环结束就代表了有一方的指针大于等于列表的长度将没有添加完成的数据直接添加到列表的末尾
result
+=
left
[
l
:]
result
+=
right
[
r
:]
return
result
alist
=
[
8
,
4
,
5
,
7
,
1
,
3
,
6
,
2
]
print
(
MergeSort
(
alist
))
\ No newline at end of file
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