Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added counting sort (Issue #23)#48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
n-gao wants to merge1 commit intoabranhe:master
base:master
Choose a base branch
Loading
fromn-gao:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsallalgorithms/sorting/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,3 +7,4 @@
from .cocktail_shaker_sort import cocktail_shaker_sort
from .tree_sort import tree_sort
from .heap_sort import heap_sort
from .counting_sort import counting_sort
56 changes: 56 additions & 0 deletionsallalgorithms/sorting/counting_sort.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# -*- coding: UTF-8 -*-
#
# Radix Sort Algorithm
# The All ▲lgorithms library for python
#
# Contributed by: Nicholas Gao
# Github: @n-gao
#
class BinaryTree:
def __init__(self, value):
self.value = value
self.left_child = None
self.right_child = None

def add_child(self, value):
if value < self.value:
if self.left_child is None:
self.left_child = BinaryTree(value)
else:
self.left_child.add_child(value)
else:
if self.right_child is None:
self.right_child = BinaryTree(value)
else:
self.right_child.add_child(value)

def get_sorted_values(self):
result = []
if self.left_child is not None:
result = self.left_child.get_sorted_values()
result.append(self.value)
if self.right_child is not None:
result = result + self.right_child.get_sorted_values()
return result

def counting_sort(arr):
counts = {}
tree = None
for value in arr:
if value in counts:
counts[value] += 1
else:
counts[value] = counts[value] + 1 if value in counts else 1
if tree is None:
tree = BinaryTree(value)
else:
tree.add_child(value)

sorted_values = tree.get_sorted_values()
i = 0
for value in sorted_values:
for _ in range(counts[value]):
arr[i] = value
i += 1

return arr
39 changes: 39 additions & 0 deletionsdocs/sorting/counting-sort.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
# Counting sort
Counting sort is a sorting algorithm which does not actually sort
the array but rather reconstructs it. It operates by counting how
often every unique item (e.g., a certain number) occurs, sorts the
unique items by TreeSort and then reconstructs an array by copying
every unique item (the number of occurences)th times into the result.
Its advantage is the fast computation which is only linear with respect
of the input array *if* the number of unique items is significantly
smaller than the number of items.

## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.sorting import counting_sort

arr = [77, 2, 10, -2, 1, 7]

print(counting_sort(arr))
# -> [-2, 1, 2, 7, 10, 77]
```

## API

```
counting_sort(array)
```

> Returns a sorted array (does in-place replacements)

##### Params:

- `array`: Array to sort

4 changes: 4 additions & 0 deletionstests/test_sorting.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
tree_sort,
heap_sort,
shell_sort,
counting_sort
)


Expand DownExpand Up@@ -39,6 +40,9 @@ def test_cocktail_shaker_sort(self):
def test_tree_sort(self):
self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1]))

def test_counting_sort(self):
self.assertEqual([-44, 1, 2, 3, 7, 19], counting_sort([7, 3, 2, 19, -44, 1]))

def test_heap_sort(self):
array = [7, 3, 2, 19, -44, 1]
heap_sort(array)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp