-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b515019
commit ef18935
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Insertion sort algorithm | ||
# Best-case time complexity: O(n) | ||
# Worst-case time complexity: O(n^2) | ||
# Average-case time complexity: O(n^2) | ||
# Space complexity: O(1) | ||
# Stable: Yes | ||
# Method: Insertion | ||
# Type: Deterministic | ||
# In-place: Yes | ||
# Note: This algorithm is a basic version of the insertion sort algorithm. | ||
|
||
def insertion_sort(A, n): | ||
""" | ||
Inputs: | ||
A: list of integers | ||
n: number of elements in A | ||
Output: sorted list of A | ||
""" | ||
for i in range(1, n): | ||
key = A[i] | ||
j = i - 1 | ||
while j >= 0 and key < A[j]: | ||
A[j + 1] = A[j] | ||
j -= 1 | ||
A[j + 1] = key | ||
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Merge procedure | ||
|
||
def merge(A, p, q, r): | ||
""" | ||
Inputs: | ||
A: list of integers | ||
p: starting index of A | ||
q: index somewhere in A | ||
r: ending index of A | ||
Note: subarrays A[p..q] and A[q+1..r] are sorted | ||
Output: | ||
A: sorted list of A | ||
""" | ||
n1 = q - p + 1 | ||
n2 = r - q | ||
B = [0] * n1 | ||
C = [0] * n2 | ||
for i in range(n1): | ||
B[i] = A[p + i] | ||
for j in range(n2): | ||
C[j] = A[q + j + 1] | ||
B[n1] = float('inf') #positvie infinity | ||
C[n2] = float('inf') | ||
i = 0 | ||
j = 0 | ||
for k in range(p, r + 1): | ||
if B[i] <= C[j]: | ||
A[k] = B[i] | ||
i += 1 | ||
else: | ||
A[k] = C[j] | ||
j += 1 | ||
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Merge sort algorithm | ||
# Best-case time complexity: O(n log n) | ||
# Worst-case time complexity: O(n log n) | ||
# Average-case time complexity: O(n log n) | ||
# Time complexity: O(n log n) for all cases | ||
# Space complexity: O(n) | ||
# Stable: Yes | ||
# Method: Merging | ||
# Type: Deterministic | ||
# In-place: No | ||
# Note: This algorithm is a basic version of the merge sort algorithm. | ||
|
||
from sorting.merge import merge | ||
|
||
def merge_sort(A, p, r): | ||
""" | ||
Input: | ||
A: list of integers | ||
p: starting index of A | ||
r: ending index of A | ||
Output: non-decreasing sorted list of A | ||
""" | ||
if p < r: | ||
q = (p + r) // 2 | ||
merge_sort(A, p, q) | ||
merge_sort(A, q + 1, r) | ||
merge(A, p, q, r) | ||
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Partition procedure | ||
|
||
def partition(A, p, r): | ||
""" | ||
Input: | ||
A: list of integers | ||
p: starting index of A | ||
r: ending index of A | ||
Output: return the index of the pivot element | ||
""" | ||
q = p | ||
for u in range(p, r): | ||
if A[u] <= A[r]: | ||
A[q], A[u] = A[u], A[q] | ||
q += 1 | ||
A[q], A[r] = A[r], A[q] | ||
return q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Quick sort algorithm | ||
# Best-case time complexity: O(n log n) | ||
# Worst-case time complexity: O(n^2) | ||
# Average-case time complexity: O(n log n) | ||
# Time complexity: O(n log n) for all cases | ||
# Space complexity: O(log n) | ||
# Stable: No | ||
# Method: Partitioning | ||
# Type: Deterministic | ||
# In-place: Yes | ||
# Note: This algorithm is a basic version of the quick sort algorithm. | ||
|
||
from sorting.partition import partition | ||
|
||
def quicksort(A, p, r): | ||
""" | ||
Input: | ||
A: list of integers | ||
p: starting index of A | ||
r: ending index of A | ||
Output: non-decreasing sorted list of A | ||
""" | ||
if p < r: | ||
q = partition(A, p, r) | ||
quicksort(A, p, q - 1) | ||
quicksort(A, q + 1, r) | ||
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Selection sort algorithm | ||
# Best-case time complexity: O(n^2) | ||
# Worst-case time complexity: O(n^2) | ||
# Average-case time complexity: O(n^2) | ||
# Space complexity: O(1) | ||
# Stable: No | ||
# Method: Selection | ||
# Type: Deterministic | ||
# In-place: Yes | ||
# Note: This algorithm is a basic version of the selection sort algorithm. | ||
|
||
def selection_sort(A, n): | ||
""" | ||
Inputs: | ||
A: list of integers | ||
n: number of elements in A | ||
Output: sorted list of A | ||
""" | ||
for i in range(n - 1): | ||
smallest = i | ||
for j in range(i+1, n): | ||
if A[j] < A[smallest]: | ||
smallest = j | ||
A[i], A[smallest] = A[smallest], A[i] | ||
return A |