forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d635926
commit df58d65
Showing
33 changed files
with
366 additions
and
407 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
52 changes: 26 additions & 26 deletions
52
Sorting Algorithims/Binary_Insertion_Sort.py → Sorting Algorithms/Binary_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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
def Binary_Search(Test_arr, low, high, k): | ||
if high >= low: | ||
Mid = (low + high) // 2 | ||
if Test_arr[Mid] < k: | ||
return Binary_Search(Test_arr, Mid + 1, high, k) | ||
elif Test_arr[Mid] > k: | ||
return Binary_Search(Test_arr, low, Mid - 1, k) | ||
else: | ||
return Mid | ||
else: | ||
return low | ||
|
||
|
||
def Insertion_Sort(Test_arr): | ||
for i in range(1, len(Test_arr)): | ||
val = Test_arr[i] | ||
j = Binary_Search(Test_arr[:i], 0, len(Test_arr[:i]) - 1, val) | ||
Test_arr.pop(i) | ||
Test_arr.insert(j, val) | ||
return Test_arr | ||
|
||
|
||
if __name__ == "__main__": | ||
Test_list = input("Enter the list of Numbers: ").split() | ||
Test_list = [int(i) for i in Test_list] | ||
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}") | ||
def Binary_Search(Test_arr, low, high, k): | ||
if high >= low: | ||
Mid = (low + high) // 2 | ||
if Test_arr[Mid] < k: | ||
return Binary_Search(Test_arr, Mid + 1, high, k) | ||
elif Test_arr[Mid] > k: | ||
return Binary_Search(Test_arr, low, Mid - 1, k) | ||
else: | ||
return Mid | ||
else: | ||
return low | ||
|
||
|
||
def Insertion_Sort(Test_arr): | ||
for i in range(1, len(Test_arr)): | ||
val = Test_arr[i] | ||
j = Binary_Search(Test_arr[:i], 0, len(Test_arr[:i]) - 1, val) | ||
Test_arr.pop(i) | ||
Test_arr.insert(j, val) | ||
return Test_arr | ||
|
||
|
||
if __name__ == "__main__": | ||
Test_list = input("Enter the list of Numbers: ").split() | ||
Test_list = [int(i) for i in Test_list] | ||
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
88 changes: 44 additions & 44 deletions
88
Sorting Algorithims/Counting-sort.py → 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 |
---|---|---|
@@ -1,44 +1,44 @@ | ||
# python program for counting sort (updated) | ||
n = int(input("please give the number of elements\n")) | ||
print("okey now plase enter n numbers seperated by spaces") | ||
tlist = list(map(int, input().split())) | ||
k = max(tlist) | ||
n = len(tlist) | ||
|
||
|
||
def counting_sort(tlist, k, n): | ||
|
||
"""Counting sort algo with sort in place. | ||
Args: | ||
tlist: target list to sort | ||
k: max value assume known before hand | ||
n: the length of the given list | ||
map info to index of the count list. | ||
Adv: | ||
The count (after cum sum) will hold the actual position of the element in sorted order | ||
Using the above, | ||
""" | ||
|
||
# Create a count list and using the index to map to the integer in tlist. | ||
count_list = [0] * (k + 1) | ||
|
||
# iterate the tgt_list to put into count list | ||
for i in range(0, n): | ||
count_list[tlist[i]] += 1 | ||
|
||
# Modify count list such that each index of count list is the combined sum of the previous counts | ||
# each index indicate the actual position (or sequence) in the output sequence. | ||
for i in range(1, k + 1): | ||
count_list[i] = count_list[i] + count_list[i - 1] | ||
|
||
flist = [0] * (n) | ||
for i in range(n - 1, -1, -1): | ||
count_list[tlist[i]] = count_list[tlist[i]] - 1 | ||
flist[count_list[tlist[i]]] = tlist[i] | ||
|
||
return flist | ||
|
||
|
||
flist = counting_sort(tlist, k, n) | ||
print(flist) | ||
# python program for counting sort (updated) | ||
n = int(input("please give the number of elements\n")) | ||
print("okey now plase enter n numbers seperated by spaces") | ||
tlist = list(map(int, input().split())) | ||
k = max(tlist) | ||
n = len(tlist) | ||
|
||
|
||
def counting_sort(tlist, k, n): | ||
|
||
"""Counting sort algo with sort in place. | ||
Args: | ||
tlist: target list to sort | ||
k: max value assume known before hand | ||
n: the length of the given list | ||
map info to index of the count list. | ||
Adv: | ||
The count (after cum sum) will hold the actual position of the element in sorted order | ||
Using the above, | ||
""" | ||
|
||
# Create a count list and using the index to map to the integer in tlist. | ||
count_list = [0] * (k + 1) | ||
|
||
# iterate the tgt_list to put into count list | ||
for i in range(0, n): | ||
count_list[tlist[i]] += 1 | ||
|
||
# Modify count list such that each index of count list is the combined sum of the previous counts | ||
# each index indicate the actual position (or sequence) in the output sequence. | ||
for i in range(1, k + 1): | ||
count_list[i] = count_list[i] + count_list[i - 1] | ||
|
||
flist = [0] * (n) | ||
for i in range(n - 1, -1, -1): | ||
count_list[tlist[i]] = count_list[tlist[i]] - 1 | ||
flist[count_list[tlist[i]]] = tlist[i] | ||
|
||
return flist | ||
|
||
|
||
flist = counting_sort(tlist, k, n) | ||
print(flist) |
File renamed without changes.
98 changes: 49 additions & 49 deletions
98
Sorting Algorithims/Heap sort.py → 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 |
---|---|---|
@@ -1,49 +1,49 @@ | ||
# Python program for implementation of heap Sort | ||
|
||
# To heapify subtree rooted at index i. | ||
# n is size of heap | ||
def heapify(arr, n, i): | ||
largest = i # Initialize largest as root | ||
l = 2 * i + 1 # left = 2*i + 1 | ||
r = 2 * i + 2 # right = 2*i + 2 | ||
|
||
# See if left child of root exists and is | ||
# greater than root | ||
if l < n and arr[i] < arr[l]: | ||
largest = l | ||
|
||
# See if right child of root exists and is | ||
# greater than root | ||
if r < n and arr[largest] < arr[r]: | ||
largest = r | ||
|
||
# Change root, if needed | ||
if largest != i: | ||
arr[i], arr[largest] = arr[largest], arr[i] # swap | ||
|
||
# Heapify the root. | ||
heapify(arr, n, largest) | ||
|
||
|
||
# The main function to sort an array of given size | ||
def heapSort(arr): | ||
n = len(arr) | ||
|
||
# Build a maxheap. | ||
# Since last parent will be at ((n//2)-1) we can start at that location. | ||
for i in range(n // 2 - 1, -1, -1): | ||
heapify(arr, n, i) | ||
|
||
# One by one extract elements | ||
for i in range(n - 1, 0, -1): | ||
arr[i], arr[0] = arr[0], arr[i] # swap | ||
heapify(arr, i, 0) | ||
|
||
|
||
# Driver code to test above | ||
arr = [12, 11, 13, 5, 6, 7] | ||
heapSort(arr) | ||
n = len(arr) | ||
print("Sorted array is") | ||
for i in range(n): | ||
print("%d" % arr[i]), | ||
# Python program for implementation of heap Sort | ||
|
||
# To heapify subtree rooted at index i. | ||
# n is size of heap | ||
def heapify(arr, n, i): | ||
largest = i # Initialize largest as root | ||
l = 2 * i + 1 # left = 2*i + 1 | ||
r = 2 * i + 2 # right = 2*i + 2 | ||
|
||
# See if left child of root exists and is | ||
# greater than root | ||
if l < n and arr[i] < arr[l]: | ||
largest = l | ||
|
||
# See if right child of root exists and is | ||
# greater than root | ||
if r < n and arr[largest] < arr[r]: | ||
largest = r | ||
|
||
# Change root, if needed | ||
if largest != i: | ||
arr[i], arr[largest] = arr[largest], arr[i] # swap | ||
|
||
# Heapify the root. | ||
heapify(arr, n, largest) | ||
|
||
|
||
# The main function to sort an array of given size | ||
def heapSort(arr): | ||
n = len(arr) | ||
|
||
# Build a maxheap. | ||
# Since last parent will be at ((n//2)-1) we can start at that location. | ||
for i in range(n // 2 - 1, -1, -1): | ||
heapify(arr, n, i) | ||
|
||
# One by one extract elements | ||
for i in range(n - 1, 0, -1): | ||
arr[i], arr[0] = arr[0], arr[i] # swap | ||
heapify(arr, i, 0) | ||
|
||
|
||
# Driver code to test above | ||
arr = [12, 11, 13, 5, 6, 7] | ||
heapSort(arr) | ||
n = len(arr) | ||
print("Sorted array is") | ||
for i in range(n): | ||
print("%d" % arr[i]), |
File renamed without changes.
42 changes: 21 additions & 21 deletions
42
Sorting Algorithims/Linear_Insertion_Sort.py → Sorting Algorithms/Linear_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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
def Linear_Search(Test_arr, val): | ||
index = 0 | ||
for i in range(len(Test_arr)): | ||
if val > Test_arr[i]: | ||
index = i + 1 | ||
return index | ||
|
||
|
||
def Insertion_Sort(Test_arr): | ||
for i in range(1, len(Test_arr)): | ||
val = Test_arr[i] | ||
j = Linear_Search(Test_arr[:i], val) | ||
Test_arr.pop(i) | ||
Test_arr.insert(j, val) | ||
return Test_arr | ||
|
||
|
||
if __name__ == "__main__": | ||
Test_list = input("Enter the list of Numbers: ").split() | ||
Test_list = [int(i) for i in Test_list] | ||
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}") | ||
def Linear_Search(Test_arr, val): | ||
index = 0 | ||
for i in range(len(Test_arr)): | ||
if val > Test_arr[i]: | ||
index = i + 1 | ||
return index | ||
|
||
|
||
def Insertion_Sort(Test_arr): | ||
for i in range(1, len(Test_arr)): | ||
val = Test_arr[i] | ||
j = Linear_Search(Test_arr[:i], val) | ||
Test_arr.pop(i) | ||
Test_arr.insert(j, val) | ||
return Test_arr | ||
|
||
|
||
if __name__ == "__main__": | ||
Test_list = input("Enter the list of Numbers: ").split() | ||
Test_list = [int(i) for i in Test_list] | ||
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}") |
Oops, something went wrong.