https://github.com/ibnaleem/sort
an array sorting algorithm in ascending order implemented in Python
https://github.com/ibnaleem/sort
array array-manipulations array-sorting arrays python sort-algorithms sorting sorting-algorithm sorting-algorithms sorting-algorithms-implemented
Last synced: about 1 month ago
JSON representation
an array sorting algorithm in ascending order implemented in Python
- Host: GitHub
- URL: https://github.com/ibnaleem/sort
- Owner: ibnaleem
- Created: 2024-04-16T03:57:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-16T04:08:14.000Z (over 1 year ago)
- Last Synced: 2025-04-02T22:23:54.250Z (6 months ago)
- Topics: array, array-manipulations, array-sorting, arrays, python, sort-algorithms, sorting, sorting-algorithm, sorting-algorithms, sorting-algorithms-implemented
- Language: Python
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sort
an array sorting algorithm in ascending order implemented in Python
```py
def my_sort(arr: list) -> list:
if len(arr) <= 1:
return arr
```
Empty and single element arrays cannot be sorted. The array requires at least two elements.
```py
pivot = arr[0]
```
Variable `pivot` stores the value of the first element. This algorithm must *pivot* the given array to the left (less than) or right (greater than) of the first element. For example, if `pivot = 5`, then all elements less than 5 must be pivoted to the left, else, they must be pivoted to the right.
```py
left = [i for i in arr[1:] if i <= pivot]
right = [i for i in arr[1:] if pivot < i]
```
All elements less than the first element are *pivoted* to the left, else, they are *pivoted* to the right.
```py
return my_sort(left) + [pivot] + my_sort(right)
```
Recursively sort the `left` and `right` arrays until they cannot be sorted, keep the `pivot` sandwiched between them (since all values to the `right` are greater than `pivot`, and all values to the `left` are less than or equal to `pivot`)