Skip to content

Commit bd23e92

Browse files
committed
added docs and test
1 parent ade7d9b commit bd23e92

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

docs/sorting/tree-sort.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Tree Sort
2+
3+
A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree (in-order) so that the elements come out in sorted order. It has two phases:
4+
1. Frist is creating a binary search tree using the given array elements.
5+
2. Second phase is traversing the given binary search tree in inorder, thus resulting in a sorted array.
6+
7+
**Performance**
8+
9+
The average number of comparisions for this method is O(nlogn). But in worst case, number of comparisions is reduced by O(n^2), a case which arrives when the tree is skewed.
10+
11+
12+
13+
## Install
14+
15+
```
16+
pip install allalgorithms
17+
```
18+
19+
## Usage
20+
21+
```py
22+
from allalgorithms.sorting import tree_sort
23+
24+
arr = [77, 2, 10, -2, 1, 7]
25+
26+
print(tree_sort(arr))
27+
# -> [-2, 1, 2, 7, 10, 77]
28+
```
29+
30+
## API
31+
32+
### tree_sort(array)
33+
34+
> Returns a sorted array
35+
36+
##### Params:
37+
38+
- `array`: Unsorted Array

tests/test_sorting.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
selection_sort,
88
pidgeonhole_sort,
99
stooge_sort,
10-
cocktail_shaker_sort
10+
cocktail_shaker_sort,
11+
tree_sort
1112
)
1213

1314

@@ -32,6 +33,9 @@ def test_stooge_sort(self):
3233

3334
def test_cocktail_shaker_sort(self):
3435
self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1]))
36+
37+
def tree_sort(self):
38+
self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1]))
3539

3640

3741
if __name__ == "__main__":

0 commit comments

Comments
 (0)