forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes - TheAlgorithms#8098 all soring algorithm
- Loading branch information
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |