Skip to content

Fixes - #8098 all soring algorithm #9891

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 13 additions & 0 deletions Sorting/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def bubblesort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: bubblesort. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/bubble_sort.py, please provide doctest for the function bubblesort

Please provide type hint for the parameter: 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]
16 changes: 16 additions & 0 deletions Sorting/heap_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def heapsort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: heapsort. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/heap_sort.py, please provide doctest for the function heapsort

Please provide type hint for the parameter: 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]
15 changes: 15 additions & 0 deletions Sorting/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def insertionsort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: insertionsort. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/insertion_sort.py, please provide doctest for the function insertionsort

Please provide type hint for the parameter: 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]
35 changes: 35 additions & 0 deletions Sorting/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def mergesort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: mergesort. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/merge_sort.py, please provide doctest for the function mergesort

Please provide type hint for the parameter: 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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: merge. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/merge_sort.py, please provide doctest for the function merge

Please provide type hint for the parameter: left

Please provide type hint for the parameter: 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]
21 changes: 21 additions & 0 deletions Sorting/quick_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def quicksort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: quicksort. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/quick_sort.py, please provide doctest for the function quicksort

Please provide type hint for the parameter: 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]
15 changes: 15 additions & 0 deletions Sorting/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def selectionsort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: selectionsort. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file Sorting/selection_sort.py, please provide doctest for the function selectionsort

Please provide type hint for the parameter: 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]