diff --git a/Sorting/bubble_sort.py b/Sorting/bubble_sort.py new file mode 100644 index 000000000000..2c4919ecb611 --- /dev/null +++ b/Sorting/bubble_sort.py @@ -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] diff --git a/Sorting/heap_sort.py b/Sorting/heap_sort.py new file mode 100644 index 000000000000..71d50b065108 --- /dev/null +++ b/Sorting/heap_sort.py @@ -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] diff --git a/Sorting/insertion_sort.py b/Sorting/insertion_sort.py new file mode 100644 index 000000000000..e8e288e6dfa6 --- /dev/null +++ b/Sorting/insertion_sort.py @@ -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] diff --git a/Sorting/merge_sort.py b/Sorting/merge_sort.py new file mode 100644 index 000000000000..3293c222a303 --- /dev/null +++ b/Sorting/merge_sort.py @@ -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] diff --git a/Sorting/quick_sort.py b/Sorting/quick_sort.py new file mode 100644 index 000000000000..23e4a761a2e5 --- /dev/null +++ b/Sorting/quick_sort.py @@ -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] diff --git a/Sorting/selection_sort.py b/Sorting/selection_sort.py new file mode 100644 index 000000000000..feffeaf8def0 --- /dev/null +++ b/Sorting/selection_sort.py @@ -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]