Skip to content

Commit 0bc50cb

Browse files
authored
Merge pull request #18 from BjornMelin/feat-0.1.0-python-add-sorting-algorithms
2 parents 94389e7 + 485d693 commit 0bc50cb

32 files changed

+1979
-1
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,47 @@ algorithms-and-data-structures/
9292
├── README.md
9393
├── algorithms/
9494
│ ├── sorting/
95+
│ │ ├── quick_sort.py
96+
│ │ ├── merge_sort.py
97+
│ │ ├── heap_sort.py
98+
│ │ ├── bubble_sort.py
99+
│ │ ├── selection_sort.py
100+
│ │ ├── insertion_sort.py
101+
│ │ ├── radix_sort.py
102+
│ │ ├── counting_sort.py
103+
│ │ ├── bucket_sort.py
104+
│ │ └── tim_sort.py
95105
│ ├── searching/
106+
│ │ ├── binary_search.py
107+
│ │ └── linear_search.py
96108
│ └── graph/
109+
│ ├── dfs.py
110+
│ └── bfs.py
97111
├── data_structures/
98112
│ ├── linear/
113+
│ │ ├── linked_list.py
114+
│ │ ├── stack.py
115+
│ │ └── queue.py
99116
│ ├── trees/
117+
│ │ ├── binary_tree.py
118+
│ │ └── avl_tree.py
100119
│ └── graphs/
101-
└── tests/
120+
│ └── graph.py
121+
├── tests/
122+
│ ├── algorithms/
123+
│ │ ├── test_quick_sort.py
124+
│ │ ├── test_merge_sort.py
125+
│ │ ├── test_heap_sort.py
126+
│ │ ├── test_bubble_sort.py
127+
│ │ ├── test_selection_sort.py
128+
│ │ ├── test_insertion_sort.py
129+
│ │ ├── test_radix_sort.py
130+
│ │ ├── test_counting_sort.py
131+
│ │ ├── test_bucket_sort.py
132+
│ │ └── test_tim_sort.py
133+
│ └── data_structures/
134+
├── requirements.txt
135+
└── setup.py
102136
```
103137

104138
</details>
@@ -149,6 +183,13 @@ python -m pytest
149183
| Binary Search | Searching ||| O(log n) |
150184
| DFS | Graph ||| O(V + E) |
151185
| BFS | Graph ||| O(V + E) |
186+
| Bubble Sort | Sorting ||| O(n^2) |
187+
| Selection Sort| Sorting ||| O(n^2) |
188+
| Insertion Sort| Sorting ||| O(n^2) |
189+
| Radix Sort | Sorting ||| O(nk) |
190+
| Counting Sort | Sorting ||| O(n + k) |
191+
| Bucket Sort | Sorting ||| O(n + k) |
192+
| Tim Sort | Sorting ||| O(n log n) |
152193

153194
## 🤝 Contributing
154195

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Bubble Sort Algorithm Implementation
3+
Author: Bjorn Melin
4+
Date: 1/28/2025
5+
6+
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list,
7+
compares adjacent elements and swaps them if they are in the wrong order.
8+
The pass through the list is repeated until the list is sorted.
9+
10+
Time Complexity:
11+
- Best Case: O(n) when the array is already sorted
12+
- Average Case: O(n^2)
13+
- Worst Case: O(n^2)
14+
15+
Space Complexity: O(1)
16+
"""
17+
18+
from typing import List
19+
20+
def bubble_sort(arr: List[int]) -> List[int]:
21+
"""
22+
Sorts an array of integers using the Bubble Sort algorithm.
23+
24+
Args:
25+
arr (List[int]): The list of integers to be sorted.
26+
27+
Returns:
28+
List[int]: The sorted list of integers.
29+
"""
30+
n = len(arr)
31+
for i in range(n):
32+
swapped = False
33+
for j in range(0, n-i-1):
34+
if arr[j] > arr[j+1]:
35+
arr[j], arr[j+1] = arr[j+1], arr[j]
36+
swapped = True
37+
if not swapped:
38+
break
39+
return arr
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Bubble Sort Algorithm
2+
3+
## Table of Contents
4+
- [Introduction](#introduction)
5+
- [Algorithm Explanation](#algorithm-explanation)
6+
- [Pseudocode](#pseudocode)
7+
- [Time and Space Complexity](#time-and-space-complexity)
8+
- [Mermaid Diagram](#mermaid-diagram)
9+
- [Testing Results](#testing-results)
10+
- [Usage Guide](#usage-guide)
11+
12+
## Introduction
13+
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
14+
15+
## Algorithm Explanation
16+
1. Start at the beginning of the list.
17+
2. Compare the first two elements.
18+
3. If the first element is greater than the second, swap them.
19+
4. Move to the next pair of elements and repeat step 3.
20+
5. Continue this process until the end of the list.
21+
6. Repeat the entire process for the remaining unsorted elements until the list is sorted.
22+
23+
## Pseudocode
24+
```
25+
procedure bubbleSort(A: list of sortable items)
26+
n := length(A)
27+
repeat
28+
swapped := false
29+
for i := 1 to n-1 inclusive do
30+
if A[i-1] > A[i] then
31+
swap(A[i-1], A[i])
32+
swapped := true
33+
end if
34+
end for
35+
n := n - 1
36+
until not swapped
37+
end procedure
38+
```
39+
40+
## Time and Space Complexity
41+
- **Best Case:** O(n) when the array is already sorted
42+
- **Average Case:** O(n^2)
43+
- **Worst Case:** O(n^2)
44+
- **Space Complexity:** O(1)
45+
46+
## Mermaid Diagram
47+
```mermaid
48+
graph TD
49+
A[Start] --> B[Initialize n to length of list]
50+
B --> C[Repeat until no swaps]
51+
C --> D[Set swapped to false]
52+
D --> E[For i from 1 to n-1]
53+
E --> F{A[i-1] > A[i]?}
54+
F -- Yes --> G[Swap A[i-1] and A[i]]
55+
G --> H[Set swapped to true]
56+
F -- No --> I[Continue]
57+
H --> I
58+
I --> J[Decrease n by 1]
59+
J --> C
60+
C --> K[End]
61+
```
62+
63+
## Testing Results
64+
| Test Case | Input | Expected Output | Actual Output |
65+
|--------------------------|----------------------|-----------------------|-----------------------|
66+
| Empty List | [] | [] | [] |
67+
| Single Element | [1] | [1] | [1] |
68+
| Already Sorted | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] |
69+
| Reverse Sorted | [5, 4, 3, 2, 1] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] |
70+
| Duplicates | [3, 1, 2, 3, 1] | [1, 1, 2, 3, 3] | [1, 1, 2, 3, 3] |
71+
| Large Dataset | [1000, 999, ..., 1] | [1, 2, ..., 1000] | [1, 2, ..., 1000] |
72+
73+
## Usage Guide
74+
To use the Bubble Sort algorithm, follow these steps:
75+
1. Import the `bubble_sort` function from the `bubble_sort` module.
76+
2. Pass the list of integers to be sorted as an argument to the `bubble_sort` function.
77+
3. The function will return the sorted list of integers.
78+
79+
```python
80+
from bubble_sort import bubble_sort
81+
82+
arr = [64, 34, 25, 12, 22, 11, 90]
83+
sorted_arr = bubble_sort(arr)
84+
print("Sorted array is:", sorted_arr)
85+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Bucket Sort Algorithm Implementation
3+
Author: Bjorn Melin
4+
Date: 1/28/2025
5+
6+
Bucket Sort is a distribution sort algorithm that works by distributing the elements
7+
of an array into a number of buckets. Each bucket is then sorted individually, either
8+
using a different sorting algorithm or by recursively applying the bucket sort algorithm.
9+
10+
Time Complexity:
11+
- Best Case: O(n + k) where k is the number of buckets
12+
- Average Case: O(n + k)
13+
- Worst Case: O(n^2) when all elements are placed in a single bucket
14+
15+
Space Complexity: O(n + k)
16+
"""
17+
18+
from typing import List
19+
20+
def bucket_sort(arr: List[int]) -> List[int]:
21+
"""
22+
Sorts an array of integers using the Bucket Sort algorithm.
23+
24+
Args:
25+
arr (List[int]): The list of integers to be sorted.
26+
27+
Returns:
28+
List[int]: The sorted list of integers.
29+
"""
30+
if len(arr) == 0:
31+
return arr
32+
33+
# Determine minimum and maximum values
34+
min_value = min(arr)
35+
max_value = max(arr)
36+
37+
# Initialize buckets
38+
bucket_count = len(arr)
39+
buckets = [[] for _ in range(bucket_count)]
40+
41+
# Distribute input array values into buckets
42+
for i in range(len(arr)):
43+
index = int((arr[i] - min_value) / (max_value - min_value + 1) * bucket_count)
44+
buckets[index].append(arr[i])
45+
46+
# Sort each bucket and concatenate the results
47+
sorted_array = []
48+
for bucket in buckets:
49+
sorted_array.extend(sorted(bucket))
50+
51+
return sorted_array
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Bucket Sort Algorithm
2+
3+
## Table of Contents
4+
- [Introduction](#introduction)
5+
- [Algorithm Explanation](#algorithm-explanation)
6+
- [Pseudocode](#pseudocode)
7+
- [Time and Space Complexity](#time-and-space-complexity)
8+
- [Mermaid Diagram](#mermaid-diagram)
9+
- [Testing Results](#testing-results)
10+
- [Usage Guide](#usage-guide)
11+
12+
## Introduction
13+
Bucket Sort is a distribution sort algorithm that works by distributing the elements
14+
of an array into a number of buckets. Each bucket is then sorted individually, either
15+
using a different sorting algorithm or by recursively applying the bucket sort algorithm.
16+
17+
## Algorithm Explanation
18+
1. Determine the minimum and maximum values in the array.
19+
2. Initialize a number of empty buckets.
20+
3. Distribute the elements of the array into the buckets based on a calculated index.
21+
4. Sort each bucket individually.
22+
5. Concatenate the sorted buckets to form the final sorted array.
23+
24+
## Pseudocode
25+
```
26+
procedure bucketSort(A: list of sortable items)
27+
n := length(A)
28+
if n = 0 then
29+
return A
30+
end if
31+
min_value := min(A)
32+
max_value := max(A)
33+
bucket_count := n
34+
buckets := array of empty lists of size bucket_count
35+
for i := 0 to n-1 do
36+
index := (A[i] - min_value) / (max_value - min_value + 1) * bucket_count
37+
buckets[index].append(A[i])
38+
end for
39+
sorted_array := empty list
40+
for each bucket in buckets do
41+
sorted_array.extend(sort(bucket))
42+
end for
43+
return sorted_array
44+
end procedure
45+
```
46+
47+
## Time and Space Complexity
48+
- **Best Case:** O(n + k) where k is the number of buckets
49+
- **Average Case:** O(n + k)
50+
- **Worst Case:** O(n^2) when all elements are placed in a single bucket
51+
- **Space Complexity:** O(n + k)
52+
53+
## Mermaid Diagram
54+
```mermaid
55+
graph TD
56+
A[Start] --> B[Determine min and max values]
57+
B --> C[Initialize empty buckets]
58+
C --> D[Distribute elements into buckets]
59+
D --> E[Sort each bucket]
60+
E --> F[Concatenate sorted buckets]
61+
F --> G[End]
62+
```
63+
64+
## Testing Results
65+
| Test Case | Input | Expected Output | Actual Output |
66+
|--------------------------|----------------------|-----------------------|-----------------------|
67+
| Empty List | [] | [] | [] |
68+
| Single Element | [1] | [1] | [1] |
69+
| Already Sorted | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] |
70+
| Reverse Sorted | [5, 4, 3, 2, 1] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] |
71+
| Duplicates | [3, 1, 2, 3, 1] | [1, 1, 2, 3, 3] | [1, 1, 2, 3, 3] |
72+
| Large Dataset | [1000, 999, ..., 1] | [1, 2, ..., 1000] | [1, 2, ..., 1000] |
73+
74+
## Usage Guide
75+
To use the Bucket Sort algorithm, follow these steps:
76+
1. Import the `bucket_sort` function from the `bucket_sort` module.
77+
2. Pass the list of integers to be sorted as an argument to the `bucket_sort` function.
78+
3. The function will return the sorted list of integers.
79+
80+
```python
81+
from bucket_sort import bucket_sort
82+
83+
arr = [64, 34, 25, 12, 22, 11, 90]
84+
sorted_arr = bucket_sort(arr)
85+
print("Sorted array is:", sorted_arr)
86+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Counting Sort Algorithm Implementation
3+
Author: Bjorn Melin
4+
Date: 1/28/2025
5+
6+
Counting Sort is an integer sorting algorithm that operates by counting the
7+
number of objects that have distinct key values (kind of hashing). It is not
8+
a comparison-based sorting algorithm and its time complexity is O(n + k),
9+
where n is the number of elements in the input array and k is the range of
10+
the input.
11+
12+
Time Complexity:
13+
- Best Case: O(n + k)
14+
- Average Case: O(n + k)
15+
- Worst Case: O(n + k)
16+
17+
Space Complexity: O(n + k)
18+
"""
19+
20+
from typing import List
21+
22+
def counting_sort(arr: List[int]) -> List[int]:
23+
"""
24+
Sorts an array of integers using the Counting Sort algorithm.
25+
26+
Args:
27+
arr (List[int]): The list of integers to be sorted.
28+
29+
Returns:
30+
List[int]: The sorted list of integers.
31+
"""
32+
if not arr:
33+
return []
34+
35+
max_val = max(arr)
36+
min_val = min(arr)
37+
range_of_elements = max_val - min_val + 1
38+
39+
# Initialize count array
40+
count = [0] * range_of_elements
41+
output = [0] * len(arr)
42+
43+
# Store the count of each element
44+
for num in arr:
45+
count[num - min_val] += 1
46+
47+
# Change count[i] so that count[i] now contains the actual
48+
# position of this element in the output array
49+
for i in range(1, len(count)):
50+
count[i] += count[i - 1]
51+
52+
# Build the output array
53+
for num in reversed(arr):
54+
output[count[num - min_val] - 1] = num
55+
count[num - min_val] -= 1
56+
57+
return output

0 commit comments

Comments
 (0)