-
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 . ## Pull Request for PyVerse 💡 ### Requesting to submit a pull request to the PyVerse repository. --- #### Issue Title #429 **Please enter the title of the issue related to your pull request.** *Enter the issue title here.* add Sorting Algorithms - [x] I have provided the issue title. --- #### Info about the Related Issue **What's the goal of the project?** *Describe the aim of the project.* added algorithms related to sorting method - [x] I have described the aim of the project. --- #### Name **Please mention your name.** *Enter your name here.* CHILUKA AKSHITHA - [x] I have provided my name. --- #### GitHub ID **Please mention your GitHub ID.** *Enter your GitHub ID here.* AKSHITHA-CHILUKA - [x] I have provided my GitHub ID. --- #### Email ID **Please mention your email ID for further communication.** *Enter your email ID here.* 22wh1a12b5@bvrithyderabad.edu.in - [ ] I have provided my email ID. --- #### Identify Yourself **Mention in which program you are contributing (e.g., WoB, GSSOC, SSOC, SWOC).** *Enter your participant role here.* Hacktoberfest and gssoc-ext - [x] I have mentioned my participant role. --- #### Closes **Enter the issue number that will be closed through this PR.** *Closes: #issue-number* #429 - [x] I have provided the issue number. --- #### Describe the Add-ons or Changes You've Made **Give a clear description of what you have added or modified.** *Describe your changes here.* added few sorting algorithms - [x] I have described my changes. --- #### Type of Change **Select the type of change:** - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Code style update (formatting, local variables) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] This change requires a documentation update --- #### How Has This Been Tested? **Describe how your changes have been tested.** *Describe your testing process here.* - [ ] I have described my testing process. --- #### Checklist **Please confirm the following:** - [x] My code follows the guidelines of this project. - [x] I have performed a self-review of my own code. - [x] I have commented my code, particularly wherever it was hard to understand. - [x] I have made corresponding changes to the documentation. - [x] My changes generate no new warnings. - [x] I have added things that prove my fix is effective or that my feature works. - [x] Any dependent changes have been merged and published in downstream modules.
- Loading branch information
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 |