From ef18935e0850c31497e3cf2e7731cfabbcdff0c1 Mon Sep 17 00:00:00 2001 From: Giacomo Dematteis Date: Wed, 21 Aug 2024 17:01:49 +0200 Subject: [PATCH] addd sorting algoorithms --- sorting/insertion_sort.py | 27 +++++++++++++++++++++++++++ sorting/merge.py | 34 ++++++++++++++++++++++++++++++++++ sorting/merge_sort.py | 29 +++++++++++++++++++++++++++++ sorting/partition.py | 18 ++++++++++++++++++ sorting/quicksort.py | 28 ++++++++++++++++++++++++++++ sorting/selection_sort.py | 26 ++++++++++++++++++++++++++ 6 files changed, 162 insertions(+) create mode 100644 sorting/insertion_sort.py create mode 100644 sorting/merge.py create mode 100644 sorting/merge_sort.py create mode 100644 sorting/partition.py create mode 100644 sorting/quicksort.py create mode 100644 sorting/selection_sort.py diff --git a/sorting/insertion_sort.py b/sorting/insertion_sort.py new file mode 100644 index 0000000..57b4547 --- /dev/null +++ b/sorting/insertion_sort.py @@ -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 diff --git a/sorting/merge.py b/sorting/merge.py new file mode 100644 index 0000000..7f28708 --- /dev/null +++ b/sorting/merge.py @@ -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 diff --git a/sorting/merge_sort.py b/sorting/merge_sort.py new file mode 100644 index 0000000..6bce20f --- /dev/null +++ b/sorting/merge_sort.py @@ -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 diff --git a/sorting/partition.py b/sorting/partition.py new file mode 100644 index 0000000..9041bbd --- /dev/null +++ b/sorting/partition.py @@ -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 diff --git a/sorting/quicksort.py b/sorting/quicksort.py new file mode 100644 index 0000000..07e990f --- /dev/null +++ b/sorting/quicksort.py @@ -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 diff --git a/sorting/selection_sort.py b/sorting/selection_sort.py new file mode 100644 index 0000000..b595115 --- /dev/null +++ b/sorting/selection_sort.py @@ -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