Skip to content

Commit

Permalink
upgrade project
Browse files Browse the repository at this point in the history
  • Loading branch information
linjing-lab committed Feb 16, 2023
1 parent 2dc232f commit f8a933e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ explain:

By the way, I didn't complete all the iterative data types, in order to develop a more targeted scenario. If you are **interested** in other iterative data types, please add them in the `convert` function of the `_utils.py` file, for example: bytes, bytearray, range, zip. If you need to deal with `dict_keys`, `dict_values`, `dict_items`, please use `list()` to convert the variables of these data types before using any method of sortingx.

- [sortingx-1.2.2](https://github.com/linjing-lab/sorting-algorithms/tree/v1.2.2) is the package that support `range`, `zip`, `dict_keys`, `dict_values`, `dict_items` additionally, you can choose what suitable data you want to input.

## LICENSE

[Apache 2.0](./LICENSE)
1 change: 1 addition & 0 deletions README_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
|v1.1.3|`pip install sortingx==1.1.3`|Typing Check with More Local Variables||
|v1.2.0|`pip install sortingx==1.2.0`|Optimize Generate Function's Kernel||
|v1.2.1|`pip install sortingx==1.2.1`|Delete Core Function to Make Builtin First||
|v1.2.2|`pip install sortingx==1.2.2`|Change Convert Function to Make it More Robust||

</div>
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'License :: OSI Approved :: Apache Software License',
'Topic :: Software Development',
'Topic :: Software Development :: Libraries',
Expand Down
4 changes: 2 additions & 2 deletions sortingx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import sys

from .sorting import bubble, insert, shell, heap, quick, merge
from .sorting import bubble, insert, shell, heap, quick, merge, __all__

__version__ = '1.2.1'
__version__ = '1.2.2'

assert sys.version_info >= (3, 7, 0)
17 changes: 14 additions & 3 deletions sortingx/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@

# Data Generated by Mapping.
def generate(__iterable: List[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None) -> List[_T]:
'''
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:return: data generated by map.
'''
compare: List[_T] = list(map(key, __iterable)) if key != None else __iterable
return compare

# Uniformly Convert `Iterable` into `List`: Facilitate the Execution of Sorting Algorithms.
def convert(__iterable: Iterable[_T]) -> List[_T]:
if isinstance(__iterable, (tuple, str, set, dict)): # more iterable data: bytes, bytearray, range, zip, not support dict_keys, dict_values, dict_items.
return list(__iterable)
return __iterable
'''
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:return: converted result in a list.
'''
if isinstance(__iterable, list): # represents unifying data as list, not refer to allowing some input that is not suitable for sorting.
return __iterable
return list(__iterable) # iterable data (except list) are convert to list.
50 changes: 31 additions & 19 deletions sortingx/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
from ._utils import generate, convert
from ._typing import Iterable, Callable, Optional, _T, SupportsRichComparison, List

__all__ = ["bubble", "insert", "shell", "heap", "quick", "merge"]

def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
'''
:param __iterable: iterable data.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:param reverse: whether to use descending order. The default is ascending order.
:return: bubble's sorted result in a list.
'''
__iterable: List[_T] = convert(__iterable)
compare: List[_T] = generate(__iterable, key)
Expand All @@ -28,18 +32,20 @@ def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
for j in range(len(__iterable) - i - 1):
if (compare[j] < compare[j + 1] if reverse else compare[j] > compare[j+1]):
__iterable[j], __iterable[j + 1] = __iterable[j + 1], __iterable[j]
flag: bool = True
if key != None:
compare[j], compare[j + 1] = compare[j + 1], compare[j]
flag: bool = True
if not flag:
break
return __iterable

def insert(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
'''
:param __iterable: iterable data.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:param reverse: whether to use descending order. The default is ascending order.
:return: insert's sorted result in a list.
'''
__iterable: List[_T] = convert(__iterable)
compare: List[_T] = generate(__iterable, key)
Expand All @@ -65,9 +71,11 @@ def insert(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo

def shell(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
'''
:param __iterable: iterable data.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:param reverse: whether to use descending order. The default is ascending order.
:return: shell's sorted result in a list.
'''
__iterable: List[_T] = convert(__iterable)
compare: List[_T] = generate(__iterable, key)
Expand All @@ -88,9 +96,11 @@ def shell(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCom

def heap(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
'''
:param __iterable: iterable data.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:param reverse: whether to use descending order. The default is ascending order.
:return: heap's sorted result in a list.
'''
__iterable: List[_T] = convert(__iterable)
compare: List[_T] = generate(__iterable, key)
Expand Down Expand Up @@ -124,9 +134,11 @@ def build(root: int, end: int) -> None:

def quick(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
'''
:param __iterable: iterable data.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:param reverse: whether to use descending order. The default is ascending order.
:return: quick's sorted result in a list.
'''
__iterable: List[_T] = convert(__iterable)
compare: List[_T] = generate(__iterable, key)
Expand Down Expand Up @@ -161,17 +173,19 @@ def partition(l: int, r: int) -> int:

def merge(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
'''
:param __iterable: iterable data.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
:param __iterable: iterable data, mainly refers to `list`, `tuple`, `set`, `dict`, `str`, `zip`, `range`.
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]), key=str.lower.
:param reverse: whether to use descending order. The default is ascending order.
:return: merge's sorted result in a list.
'''
__iterable: List[_T] = convert(__iterable)
compare: List[_T] = generate(__iterable, key)
def merg(low: int, mid: int, high: int) -> None:
'''
:param low: The low-side cursor of __iterable (int).
:param mid: The middle-side cursor of __iterable (int).
:param high: The high-side cursor of __iterable (int).
:param low: The low cursor of __iterable (int).
:param mid: The middle cursor of __iterable (int).
:param high: The high cursor of __iterable (int).
'''
left: List[_T] = __iterable[low: mid]
lc: List[_T] = compare[low: mid]
Expand Down Expand Up @@ -212,6 +226,4 @@ def solve() -> None:
low += 2 * i
i *= 2
solve()
return __iterable

__all__ = [bubble, insert, shell, heap, quick, merge]
return __iterable

0 comments on commit f8a933e

Please sign in to comment.