Skip to content

Commit

Permalink
addd sorting algoorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
DematteisGiacomo committed Aug 29, 2024
1 parent b515019 commit ef18935
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sorting/insertion_sort.py
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
34 changes: 34 additions & 0 deletions sorting/merge.py
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
29 changes: 29 additions & 0 deletions sorting/merge_sort.py
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
18 changes: 18 additions & 0 deletions sorting/partition.py
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
28 changes: 28 additions & 0 deletions sorting/quicksort.py
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
26 changes: 26 additions & 0 deletions sorting/selection_sort.py
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

0 comments on commit ef18935

Please sign in to comment.