Skip to content

Commit

Permalink
renew project
Browse files Browse the repository at this point in the history
  • Loading branch information
linjing-lab committed Dec 27, 2022
1 parent a994e67 commit ed38bab
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ explain:
- sortingx-1.1.2 is the first stable version that has a return value and extends the iterable data types.
- sortingx-1.1.3 is the version that complete the typing of local variables and align with `sorted()` usage method.
- sortingx-1.2.0 is the end version of sorting series, which optimize the kernel of generate.
- sortingx-1.2.1 is the portable version that comparison is faster than ever, the generate is more portable.

## LICENSE

Expand Down
1 change: 1 addition & 0 deletions README_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
|v1.1.2|`pip install sortingx==1.1.2`|Support More Iterative Data Types||
|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||

</div>
2 changes: 1 addition & 1 deletion sortingx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

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

__version__ = '1.2.0'
__version__ = '1.2.1'

assert sys.version_info >= (3, 7, 0)
16 changes: 0 additions & 16 deletions sortingx/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,8 @@
# Data Generated by Mapping.
def generate(__iterable: List[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None) -> List[_T]:
compare: List[_T] = list(map(key, __iterable)) if key != None else __iterable
compare: List[_T] = ([[value] for value in compare] if (compare and not isinstance(compare[0], (list, tuple))) else compare) if key != None else __iterable
return compare

# Redefined Comparison Rules: A High-Speed State Selection Function.
def core(left: List[_T], right: List[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> bool:
if key == None:
return left < right if reverse else left > right
for index in range(0, len(left)):
if left[index] > right[index] and reverse:
return False
elif left[index] > right[index] and not reverse:
return True
elif left[index] < right[index] and reverse:
return True
elif left[index] < right[index] and not reverse:
return False
return False # Reduce the Times of Data Exchanging.

# 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)):
Expand Down
16 changes: 8 additions & 8 deletions sortingx/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ._utils import core, generate, convert
from ._utils import generate, convert
from ._typing import Iterable, Callable, Optional, _T, SupportsRichComparison, List

def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> List[_T]:
Expand All @@ -26,7 +26,7 @@ def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
for i in range(len(__iterable) - 1):
flag: bool = False # early stop by adding a bool value named flag
for j in range(len(__iterable) - i - 1):
if core(compare[j], compare[j + 1], key, reverse):
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:
Expand All @@ -50,7 +50,7 @@ def insert(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
high: int = index - 1
while low <= high: # sequence conforming to monotonicity
mid: int = (low + high) // 2
if core(keyc, compare[mid], key, reverse):
if (keyc < compare[mid] if reverse else keyc > compare[mid]):
low: int = mid + 1
else:
high: int = mid - 1
Expand Down Expand Up @@ -78,7 +78,7 @@ def shell(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCom
while gap >= 1:
for index in range(gap, length):
next: int = index
while next >= gap and core(compare[next - gap], compare[next], key, reverse):
while next >= gap and (compare[next - gap] < compare[next] if reverse else compare[next - gap] > compare[next]):
__iterable[next], __iterable[next - gap] = __iterable[next - gap], __iterable[next]
if key != None:
compare[next], compare[next - gap] = compare[next - gap], compare[next]
Expand All @@ -102,9 +102,9 @@ def build(root: int, end: int) -> None:
piv: int = root
left: int = 2 * root + 1
right: int = 2 * root + 2
if left < end and core(compare[left], compare[root], key, reverse):
if left < end and (compare[left] < compare[root] if reverse else compare[left] > compare[root]):
piv: int = left
if right < end and core(compare[right], compare[piv], key, reverse):
if right < end and (compare[right] < compare[piv] if reverse else compare[right] > compare[piv]):
piv: int = right
if piv != root:
__iterable[root], __iterable[piv] = __iterable[piv], __iterable[root]
Expand Down Expand Up @@ -147,7 +147,7 @@ def partition(l: int, r: int) -> int:
val: _T = compare[r]
index: int = l - 1
for ind in range(l, r):
if core(val, compare[ind], key, reverse):
if (val < compare[ind] if reverse else val > compare[ind]):
index += 1
__iterable[index], __iterable[ind] = __iterable[ind], __iterable[index]
if key != None:
Expand Down Expand Up @@ -182,7 +182,7 @@ def merg(low: int, mid: int, high: int) -> None:
result: List[_T] = []
store: List[_T] = []
while i < len(left) and j < len(right):
if core(rc[j], lc[i], key, reverse):
if (rc[j] < lc[i] if reverse else rc[j] > lc[i]):
result.append(left[i])
store.append(lc[i])
i += 1
Expand Down

0 comments on commit ed38bab

Please sign in to comment.