Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sorting algorithms #474

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Algorithms_and_Data_Structures/sorting_algorithms/Bubble_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Track if a swap was made
swapped = False
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j] # Swap
swapped = True
if not swapped:
break # If no swaps, the array is sorted
return arr
12 changes: 12 additions & 0 deletions Algorithms_and_Data_Structures/sorting_algorithms/Counting_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def counting_sort(arr):
max_val = max(arr) + 1
count = [0] * max_val

for num in arr:
count[num] += 1

sorted_arr = []
for i, c in enumerate(count):
sorted_arr.extend([i] * c)

return sorted_arr
25 changes: 25 additions & 0 deletions Algorithms_and_Data_Structures/sorting_algorithms/Heap_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2

if left < n and arr[left] > arr[largest]:
largest = left

if right < n and arr[right] > arr[largest]:
largest = right

if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # Swap
heapify(arr, n, largest)

def heap_sort(arr):
n = len(arr)

for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)

for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # Swap
heapify(arr, i, 0)
return arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
32 changes: 32 additions & 0 deletions Algorithms_and_Data_Structures/sorting_algorithms/Merge_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2 # Finding the mid of the array
left_half = arr[:mid] # Dividing the array elements into 2 halves
right_half = arr[mid:]

merge_sort(left_half) # Sorting the first half
merge_sort(right_half) # Sorting the second half

i = j = k = 0

# Copy data to temp arrays L[] and R[]
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1

# Checking if any element was left
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
return arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[len(arr) // 2] # Choosing the pivot
left = [x for x in arr if x < pivot] # Elements less than pivot
middle = [x for x in arr if x == pivot] # Elements equal to pivot
right = [x for x in arr if x > pivot] # Elements greater than pivot
return quick_sort(left) + middle + quick_sort(right)
27 changes: 27 additions & 0 deletions Algorithms_and_Data_Structures/sorting_algorithms/Radix_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def counting_sort_for_radix(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10

for i in range(n):
index = arr[i] // exp
count[index % 10] += 1

for i in range(1, 10):
count[i] += count[i - 1]

for i in range(n - 1, -1, -1):
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1

for i in range(n):
arr[i] = output[i]

def radix_sort(arr):
max_val = max(arr)
exp = 1
while max_val // exp > 0:
counting_sort_for_radix(arr, exp)
exp *= 10
return arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i] # Swap
return arr