-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Bubble sort , counting sort , head sort , insertion sort , merge sort , quick sort , radix sort , selection sort .
- Loading branch information
1 parent
9849381
commit 3738e73
Showing
8 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
Algorithms_and_Data_Structures/sorting_algorithms/Bubble_Sort.py
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 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 |
12 changes: 12 additions & 0 deletions
12
Algorithms_and_Data_Structures/sorting_algorithms/Counting_sort.py
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 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 |
25 changes: 25 additions & 0 deletions
25
Algorithms_and_Data_Structures/sorting_algorithms/Heap_sort.py
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,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 |
9 changes: 9 additions & 0 deletions
9
Algorithms_and_Data_Structures/sorting_algorithms/Insertion_Sort.py
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,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 |
32 changes: 32 additions & 0 deletions
32
Algorithms_and_Data_Structures/sorting_algorithms/Merge_Sort.py
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,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 |
9 changes: 9 additions & 0 deletions
9
Algorithms_and_Data_Structures/sorting_algorithms/Quick_sort.py
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,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) |
27 changes: 27 additions & 0 deletions
27
Algorithms_and_Data_Structures/sorting_algorithms/Radix_Sort.py
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,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 |
8 changes: 8 additions & 0 deletions
8
Algorithms_and_Data_Structures/sorting_algorithms/Selection_Sort
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,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 |