Skip to content

Commit

Permalink
Fixes - TheAlgorithms#8098 all soring algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
anasadh committed Oct 6, 2023
1 parent cffdf99 commit 5c917c8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sorting/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def bubblesort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
# Compare adjacent elements and swap if they are in the wrong order.
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
bubblesort(arr)
print(arr) # Output: [1, 1, 2, 3, 6, 8, 10]
14 changes: 14 additions & 0 deletions Sorting/heap_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def heapsort(arr):
import heapq
# Convert the list into a min-heap.
heapq.heapify(arr)
sorted_arr = []
while arr:
# Pop elements from the heap to build the sorted array.
sorted_arr.append(heapq.heappop(arr))
return sorted_arr

# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
sorted_arr = heapsort(arr)
print(sorted_arr) # Output: [1, 1, 2, 3, 6, 8, 10]
14 changes: 14 additions & 0 deletions Sorting/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def insertionsort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
# Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position.
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
insertionsort(arr)
print(arr) # Output: [1, 1, 2, 3, 6, 8, 10]
34 changes: 34 additions & 0 deletions Sorting/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def mergesort(arr):
# Base case: If the list has 1 or 0 elements, it's already sorted.
if len(arr) <= 1:
return arr

def merge(left, right):
result = []
i = j = 0

# Merge the left and right sublists in sorted order.
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1

# Append any remaining elements from left and right sublists.
result.extend(left[i:])
result.extend(right[j:])
return result

middle = len(arr) // 2
left = arr[:middle]
right = arr[middle:]

# Recursively merge and sort the left and right partitions.
return merge(mergesort(left), mergesort(right))

# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
sorted_arr = mergesort(arr)
print(sorted_arr) # Output: [1, 1, 2, 3, 6, 8, 10]
20 changes: 20 additions & 0 deletions Sorting/quick_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def quicksort(arr):
# Base case: If the list has 1 or 0 elements, it's already sorted.
if len(arr) <= 1:
return arr

# Choose a pivot element (typically the middle element).
pivot = arr[len(arr) // 2]

# Partition the list into three parts: elements less than, equal to, and greater than the pivot.
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]

# Recursively sort the left and right partitions and concatenate the results.
return quicksort(left) + middle + quicksort(right)

# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
sorted_arr = quicksort(arr)
print(sorted_arr) # Output: [1, 1, 2, 3, 6, 8, 10]
14 changes: 14 additions & 0 deletions Sorting/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def selectionsort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
# Find the minimum element in the remaining unsorted portion of the array.
if arr[j] < arr[min_index]:
min_index = j
# Swap the found minimum element with the first element.
arr[i], arr[min_index] = arr[min_index], arr[i]

# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
selectionsort(arr)
print(arr) # Output: [1, 1, 2, 3, 6, 8, 10]

0 comments on commit 5c917c8

Please sign in to comment.