Skip to content

Commit dc3c080

Browse files
committed
Initial commit
0 parents  commit dc3c080

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Othneil Drew
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Basic sorting algorithms using Python
2+
3+
Basic sorting algorithms:
4+
5+
- [Bubble Sort](sorting_algorithms/bubble_sort.py)
6+
- [Merge Sort](sorting_algorithms/merge_sort.py)
7+
- [Quicksort](sorting_algorithms/quicksort.py)
8+
9+
## Resources
10+
11+
**Big-O:**
12+
[Big-O Cheat Sheet: Array Sorting Algorithms](https://www.bigocheatsheet.com/#sorting)
13+
**Bubble Sort:**
14+
[Bubble Sort: An Archaeological Algorithmic Analysis ](https://users.cs.duke.edu/~ola/bubble/bubble.html)
15+
[Bubble Sort: Quick Tutorial and Implementation Guide](https://www.pythoncentral.io/bubble-sort-implementation-guide/)
16+
**Quicksort:**
17+
[Quicksort in python (from Ken's Blog)](http://www.99nth.com/~krm/blog/quicksort-python.html)
18+
[Quicksort in Python](https://stackabuse.com/quicksort-in-python/)
19+
**Merge Sort:**
20+
[Merge Sort: A Quick Tutorial and Implementation Guide](https://www.pythoncentral.io/merge-sort-implementation-guide/)
21+
22+
## License
23+
24+
Distributed under the MIT License. See `LICENSE.txt` for more information.
25+
26+
## Contact
27+
28+
Julien Cortesi - [@serializers](https://twitter.com/serializers) - [julienc.net](https://julienc.net) - julien.cortesi@potonmail.com

sorting_algorithms/bubble_sort.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def bubble_sort(alist):
2+
3+
for i in range(len(alist)-1, 0, -1):
4+
for j in range(i):
5+
if alist[j] > alist[j+1]:
6+
temp = alist[j]
7+
alist[j] = alist[j+1]
8+
alist[j+1] = temp
9+
10+
return alist
11+
12+
13+
list_to_sort = [147, -16, 18, 91, 44, 1, 8, 54, 31, 18]
14+
bubble_sort(list_to_sort)
15+
print(list_to_sort)

sorting_algorithms/merge_sort.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def merge_sort(alist):
2+
if len(alist) > 1:
3+
left = alist[:len(alist) // 2]
4+
right = alist[len(alist) // 2:]
5+
6+
merge_sort(left)
7+
merge_sort(right)
8+
9+
i = 0
10+
j = 0
11+
k = 0
12+
13+
while i < len(left) and j < len(right):
14+
if left[i] < right[j]:
15+
alist[k] = left[i]
16+
i += 1
17+
else:
18+
alist[k] = right[j]
19+
j += 1
20+
k += 1
21+
22+
while i < len(left):
23+
alist[k] = left[i]
24+
i += 1
25+
k += 1
26+
27+
while j < len(right):
28+
alist[k] = right[j]
29+
j += 1
30+
k += 1
31+
32+
33+
list_to_sort = [147, -16, 18, 91, 44, 1, 8, 54, 31, 18]
34+
merge_sort(list_to_sort)
35+
print(list_to_sort)

sorting_algorithms/quicksort.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def quicksort(alist, lower, higher):
2+
"""
3+
lo - index of the lower end in the range
4+
hi - index of the higher end"""
5+
if lower < higher:
6+
p = partition(alist, lower, higher)
7+
quicksort(alist, lower, p - 1)
8+
quicksort(alist, p + 1, higher)
9+
10+
11+
def partition(alist, lower, higher):
12+
"""Partitions the list within the given range
13+
alist - a list to partition
14+
lower - index of the lower end in list to start partitioning from
15+
higher - index of higher end in list to end the partitioning"""
16+
pivot = alist[higher]
17+
i = lower
18+
j = lower
19+
20+
while j < higher:
21+
if alist[j] <= pivot:
22+
alist[i], alist[j] = alist[j], alist[i]
23+
i += 1
24+
j += 1
25+
26+
alist[i], alist[higher] = alist[higher], alist[i]
27+
28+
return i
29+
30+
31+
list_to_sort = [147, -16, 18, 91, 44, 1, 8, 54, 31, 18]
32+
last_index = len(list_to_sort) - 1
33+
quicksort(list_to_sort, 0, last_index)
34+
print(list_to_sort)

0 commit comments

Comments
 (0)