Skip to content

Commit 5c917c8

Browse files
committed
Fixes - TheAlgorithms#8098 all soring algorithm
1 parent cffdf99 commit 5c917c8

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

Diff for: Sorting/bubble_sort.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def bubblesort(arr):
2+
n = len(arr)
3+
for i in range(n):
4+
for j in range(0, n - i - 1):
5+
# Compare adjacent elements and swap if they are in the wrong order.
6+
if arr[j] > arr[j + 1]:
7+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
8+
9+
# Example usage:
10+
arr = [3, 6, 8, 10, 1, 2, 1]
11+
bubblesort(arr)
12+
print(arr) # Output: [1, 1, 2, 3, 6, 8, 10]

Diff for: Sorting/heap_sort.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def heapsort(arr):
2+
import heapq
3+
# Convert the list into a min-heap.
4+
heapq.heapify(arr)
5+
sorted_arr = []
6+
while arr:
7+
# Pop elements from the heap to build the sorted array.
8+
sorted_arr.append(heapq.heappop(arr))
9+
return sorted_arr
10+
11+
# Example usage:
12+
arr = [3, 6, 8, 10, 1, 2, 1]
13+
sorted_arr = heapsort(arr)
14+
print(sorted_arr) # Output: [1, 1, 2, 3, 6, 8, 10]

Diff for: Sorting/insertion_sort.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def insertionsort(arr):
2+
for i in range(1, len(arr)):
3+
key = arr[i]
4+
j = i - 1
5+
# Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position.
6+
while j >= 0 and key < arr[j]:
7+
arr[j + 1] = arr[j]
8+
j -= 1
9+
arr[j + 1] = key
10+
11+
# Example usage:
12+
arr = [3, 6, 8, 10, 1, 2, 1]
13+
insertionsort(arr)
14+
print(arr) # Output: [1, 1, 2, 3, 6, 8, 10]

Diff for: Sorting/merge_sort.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def mergesort(arr):
2+
# Base case: If the list has 1 or 0 elements, it's already sorted.
3+
if len(arr) <= 1:
4+
return arr
5+
6+
def merge(left, right):
7+
result = []
8+
i = j = 0
9+
10+
# Merge the left and right sublists in sorted order.
11+
while i < len(left) and j < len(right):
12+
if left[i] < right[j]:
13+
result.append(left[i])
14+
i += 1
15+
else:
16+
result.append(right[j])
17+
j += 1
18+
19+
# Append any remaining elements from left and right sublists.
20+
result.extend(left[i:])
21+
result.extend(right[j:])
22+
return result
23+
24+
middle = len(arr) // 2
25+
left = arr[:middle]
26+
right = arr[middle:]
27+
28+
# Recursively merge and sort the left and right partitions.
29+
return merge(mergesort(left), mergesort(right))
30+
31+
# Example usage:
32+
arr = [3, 6, 8, 10, 1, 2, 1]
33+
sorted_arr = mergesort(arr)
34+
print(sorted_arr) # Output: [1, 1, 2, 3, 6, 8, 10]

Diff for: Sorting/quick_sort.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def quicksort(arr):
2+
# Base case: If the list has 1 or 0 elements, it's already sorted.
3+
if len(arr) <= 1:
4+
return arr
5+
6+
# Choose a pivot element (typically the middle element).
7+
pivot = arr[len(arr) // 2]
8+
9+
# Partition the list into three parts: elements less than, equal to, and greater than the pivot.
10+
left = [x for x in arr if x < pivot]
11+
middle = [x for x in arr if x == pivot]
12+
right = [x for x in arr if x > pivot]
13+
14+
# Recursively sort the left and right partitions and concatenate the results.
15+
return quicksort(left) + middle + quicksort(right)
16+
17+
# Example usage:
18+
arr = [3, 6, 8, 10, 1, 2, 1]
19+
sorted_arr = quicksort(arr)
20+
print(sorted_arr) # Output: [1, 1, 2, 3, 6, 8, 10]

Diff for: Sorting/selection_sort.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def selectionsort(arr):
2+
for i in range(len(arr)):
3+
min_index = i
4+
for j in range(i + 1, len(arr)):
5+
# Find the minimum element in the remaining unsorted portion of the array.
6+
if arr[j] < arr[min_index]:
7+
min_index = j
8+
# Swap the found minimum element with the first element.
9+
arr[i], arr[min_index] = arr[min_index], arr[i]
10+
11+
# Example usage:
12+
arr = [3, 6, 8, 10, 1, 2, 1]
13+
selectionsort(arr)
14+
print(arr) # Output: [1, 1, 2, 3, 6, 8, 10]

0 commit comments

Comments
 (0)