diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Bubble_Sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Bubble_Sort.py new file mode 100644 index 0000000000..5645a90888 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Bubble_Sort.py @@ -0,0 +1,12 @@ +def bubble_sort(arr): + n = len(arr) + for i in range(n): + # Track if a swap was made + swapped = False + for j in range(0, n-i-1): + if arr[j] > arr[j+1]: + arr[j], arr[j+1] = arr[j+1], arr[j] # Swap + swapped = True + if not swapped: + break # If no swaps, the array is sorted + return arr diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Counting_sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Counting_sort.py new file mode 100644 index 0000000000..1676d7861e --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Counting_sort.py @@ -0,0 +1,12 @@ +def counting_sort(arr): + max_val = max(arr) + 1 + count = [0] * max_val + + for num in arr: + count[num] += 1 + + sorted_arr = [] + for i, c in enumerate(count): + sorted_arr.extend([i] * c) + + return sorted_arr diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Heap_sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Heap_sort.py new file mode 100644 index 0000000000..b450e2ce07 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Heap_sort.py @@ -0,0 +1,25 @@ +def heapify(arr, n, i): + largest = i + left = 2 * i + 1 + right = 2 * i + 2 + + if left < n and arr[left] > arr[largest]: + largest = left + + if right < n and arr[right] > arr[largest]: + largest = right + + if largest != i: + arr[i], arr[largest] = arr[largest], arr[i] # Swap + heapify(arr, n, largest) + +def heap_sort(arr): + n = len(arr) + + for i in range(n // 2 - 1, -1, -1): + heapify(arr, n, i) + + for i in range(n - 1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] # Swap + heapify(arr, i, 0) + return arr diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Insertion_Sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Insertion_Sort.py new file mode 100644 index 0000000000..296bf41b62 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Insertion_Sort.py @@ -0,0 +1,9 @@ +def insertion_sort(arr): + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + return arr diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Merge_Sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Merge_Sort.py new file mode 100644 index 0000000000..4304c5f206 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Merge_Sort.py @@ -0,0 +1,32 @@ +def merge_sort(arr): + if len(arr) > 1: + mid = len(arr) // 2 # Finding the mid of the array + left_half = arr[:mid] # Dividing the array elements into 2 halves + right_half = arr[mid:] + + merge_sort(left_half) # Sorting the first half + merge_sort(right_half) # Sorting the second half + + i = j = k = 0 + + # Copy data to temp arrays L[] and R[] + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + + while j < len(right_half): + arr[k] = right_half[j] + j += 1 + k += 1 + return arr diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Quick_sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Quick_sort.py new file mode 100644 index 0000000000..38163c4d08 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Quick_sort.py @@ -0,0 +1,9 @@ +def quick_sort(arr): + if len(arr) <= 1: + return arr + else: + pivot = arr[len(arr) // 2] # Choosing the pivot + left = [x for x in arr if x < pivot] # Elements less than pivot + middle = [x for x in arr if x == pivot] # Elements equal to pivot + right = [x for x in arr if x > pivot] # Elements greater than pivot + return quick_sort(left) + middle + quick_sort(right) diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Radix_Sort.py b/Algorithms_and_Data_Structures/sorting_algorithms/Radix_Sort.py new file mode 100644 index 0000000000..8c91bb95f5 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Radix_Sort.py @@ -0,0 +1,27 @@ +def counting_sort_for_radix(arr, exp): + n = len(arr) + output = [0] * n + count = [0] * 10 + + for i in range(n): + index = arr[i] // exp + count[index % 10] += 1 + + for i in range(1, 10): + count[i] += count[i - 1] + + for i in range(n - 1, -1, -1): + index = arr[i] // exp + output[count[index % 10] - 1] = arr[i] + count[index % 10] -= 1 + + for i in range(n): + arr[i] = output[i] + +def radix_sort(arr): + max_val = max(arr) + exp = 1 + while max_val // exp > 0: + counting_sort_for_radix(arr, exp) + exp *= 10 + return arr diff --git a/Algorithms_and_Data_Structures/sorting_algorithms/Selection_Sort b/Algorithms_and_Data_Structures/sorting_algorithms/Selection_Sort new file mode 100644 index 0000000000..41f9944f83 --- /dev/null +++ b/Algorithms_and_Data_Structures/sorting_algorithms/Selection_Sort @@ -0,0 +1,8 @@ +def selection_sort(arr): + for i in range(len(arr)): + min_index = i + for j in range(i + 1, len(arr)): + if arr[j] < arr[min_index]: + min_index = j + arr[i], arr[min_index] = arr[min_index], arr[i] # Swap + return arr