-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def heapsort(arr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def insertionsort(arr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def mergesort(arr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
# Base case: If the list has 1 or 0 elements, it's already sorted. | ||
if len(arr) <= 1: | ||
return arr | ||
|
||
def merge(left, right): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def quicksort(arr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
# 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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def selectionsort(arr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
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] |
There was a problem hiding this comment.
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 functionbubblesort
Please provide type hint for the parameter:
arr