Skip to content

Commit f11d32a

Browse files
committed
renew keyword-sorting
1 parent 9c9483c commit f11d32a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ I test the performance of the sorting algorithm after adding the keyword sorting
6464

6565
The design of reverse sorting of all methods is completely correct, and the design of keyword sorting based on bubble sorting optimized is feasible, which is consistent with the usage of *sorted* parameter officially released by Python.
6666

67+
The examples of keyword sorting are underlying:
68+
69+
```python
70+
data = [('Alex', 100, 90, 98, 95), ('Jack', 97, 89, 92, 99), ('Peter', 92, 95, 92, 96), ('Li', 97, 88, 98, 92)]
71+
bubble_sort(data, key=lambda x:(x[1], x[2]), reverse=True)
72+
print(data)
73+
74+
'''
75+
reverse=False:
76+
[('Peter', 92, 95, 92, 96), ('Li', 97, 88, 98, 92), ('Jack', 97, 89, 92, 99), ('Alex', 100, 90, 98, 95)]
77+
78+
reverse=True:
79+
[('Alex', 100, 90, 98, 95), ('Jack', 97, 89, 92, 99), ('Li', 97, 88, 98, 92), ('Peter', 92, 95, 92, 96)]
80+
'''
81+
```
82+
83+
you can see more examples in [*test_key*](./test_key.py) file.
84+
6785
## LICENSE
6886

6987
[MIT LICENSE](./LICENSE)

test_key.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import List
21
from utils import cmp
32

4-
def bubble_sort(array: List, key=None, reverse: bool=False) -> None:
3+
def bubble_sort(array: list, key=None, reverse: bool=False) -> None:
54
'''
65
array: 同一数据维度下,支持数值型数据,如整型与浮点型混合;支持全为字符串类型的数据;不支持字符串型与数值型混合。
76
key: lambda函数, 仅含一个参量,用于关键字排序, 例如: key=lambda x: x[1], key=lambda x: (x[0], x[1])

0 commit comments

Comments
 (0)