Skip to content

Commit df58d65

Browse files
committed
typo corrected
1 parent d635926 commit df58d65

33 files changed

+366
-407
lines changed

Sorting Algorithims/Quick_Sort.py

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
def Binary_Search(Test_arr, low, high, k):
2-
if high >= low:
3-
Mid = (low + high) // 2
4-
if Test_arr[Mid] < k:
5-
return Binary_Search(Test_arr, Mid + 1, high, k)
6-
elif Test_arr[Mid] > k:
7-
return Binary_Search(Test_arr, low, Mid - 1, k)
8-
else:
9-
return Mid
10-
else:
11-
return low
12-
13-
14-
def Insertion_Sort(Test_arr):
15-
for i in range(1, len(Test_arr)):
16-
val = Test_arr[i]
17-
j = Binary_Search(Test_arr[:i], 0, len(Test_arr[:i]) - 1, val)
18-
Test_arr.pop(i)
19-
Test_arr.insert(j, val)
20-
return Test_arr
21-
22-
23-
if __name__ == "__main__":
24-
Test_list = input("Enter the list of Numbers: ").split()
25-
Test_list = [int(i) for i in Test_list]
26-
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}")
1+
def Binary_Search(Test_arr, low, high, k):
2+
if high >= low:
3+
Mid = (low + high) // 2
4+
if Test_arr[Mid] < k:
5+
return Binary_Search(Test_arr, Mid + 1, high, k)
6+
elif Test_arr[Mid] > k:
7+
return Binary_Search(Test_arr, low, Mid - 1, k)
8+
else:
9+
return Mid
10+
else:
11+
return low
12+
13+
14+
def Insertion_Sort(Test_arr):
15+
for i in range(1, len(Test_arr)):
16+
val = Test_arr[i]
17+
j = Binary_Search(Test_arr[:i], 0, len(Test_arr[:i]) - 1, val)
18+
Test_arr.pop(i)
19+
Test_arr.insert(j, val)
20+
return Test_arr
21+
22+
23+
if __name__ == "__main__":
24+
Test_list = input("Enter the list of Numbers: ").split()
25+
Test_list = [int(i) for i in Test_list]
26+
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}")
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
# python program for counting sort (updated)
2-
n = int(input("please give the number of elements\n"))
3-
print("okey now plase enter n numbers seperated by spaces")
4-
tlist = list(map(int, input().split()))
5-
k = max(tlist)
6-
n = len(tlist)
7-
8-
9-
def counting_sort(tlist, k, n):
10-
11-
"""Counting sort algo with sort in place.
12-
Args:
13-
tlist: target list to sort
14-
k: max value assume known before hand
15-
n: the length of the given list
16-
map info to index of the count list.
17-
Adv:
18-
The count (after cum sum) will hold the actual position of the element in sorted order
19-
Using the above,
20-
21-
"""
22-
23-
# Create a count list and using the index to map to the integer in tlist.
24-
count_list = [0] * (k + 1)
25-
26-
# iterate the tgt_list to put into count list
27-
for i in range(0, n):
28-
count_list[tlist[i]] += 1
29-
30-
# Modify count list such that each index of count list is the combined sum of the previous counts
31-
# each index indicate the actual position (or sequence) in the output sequence.
32-
for i in range(1, k + 1):
33-
count_list[i] = count_list[i] + count_list[i - 1]
34-
35-
flist = [0] * (n)
36-
for i in range(n - 1, -1, -1):
37-
count_list[tlist[i]] = count_list[tlist[i]] - 1
38-
flist[count_list[tlist[i]]] = tlist[i]
39-
40-
return flist
41-
42-
43-
flist = counting_sort(tlist, k, n)
44-
print(flist)
1+
# python program for counting sort (updated)
2+
n = int(input("please give the number of elements\n"))
3+
print("okey now plase enter n numbers seperated by spaces")
4+
tlist = list(map(int, input().split()))
5+
k = max(tlist)
6+
n = len(tlist)
7+
8+
9+
def counting_sort(tlist, k, n):
10+
11+
"""Counting sort algo with sort in place.
12+
Args:
13+
tlist: target list to sort
14+
k: max value assume known before hand
15+
n: the length of the given list
16+
map info to index of the count list.
17+
Adv:
18+
The count (after cum sum) will hold the actual position of the element in sorted order
19+
Using the above,
20+
21+
"""
22+
23+
# Create a count list and using the index to map to the integer in tlist.
24+
count_list = [0] * (k + 1)
25+
26+
# iterate the tgt_list to put into count list
27+
for i in range(0, n):
28+
count_list[tlist[i]] += 1
29+
30+
# Modify count list such that each index of count list is the combined sum of the previous counts
31+
# each index indicate the actual position (or sequence) in the output sequence.
32+
for i in range(1, k + 1):
33+
count_list[i] = count_list[i] + count_list[i - 1]
34+
35+
flist = [0] * (n)
36+
for i in range(n - 1, -1, -1):
37+
count_list[tlist[i]] = count_list[tlist[i]] - 1
38+
flist[count_list[tlist[i]]] = tlist[i]
39+
40+
return flist
41+
42+
43+
flist = counting_sort(tlist, k, n)
44+
print(flist)
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
# Python program for implementation of heap Sort
2-
3-
# To heapify subtree rooted at index i.
4-
# n is size of heap
5-
def heapify(arr, n, i):
6-
largest = i # Initialize largest as root
7-
l = 2 * i + 1 # left = 2*i + 1
8-
r = 2 * i + 2 # right = 2*i + 2
9-
10-
# See if left child of root exists and is
11-
# greater than root
12-
if l < n and arr[i] < arr[l]:
13-
largest = l
14-
15-
# See if right child of root exists and is
16-
# greater than root
17-
if r < n and arr[largest] < arr[r]:
18-
largest = r
19-
20-
# Change root, if needed
21-
if largest != i:
22-
arr[i], arr[largest] = arr[largest], arr[i] # swap
23-
24-
# Heapify the root.
25-
heapify(arr, n, largest)
26-
27-
28-
# The main function to sort an array of given size
29-
def heapSort(arr):
30-
n = len(arr)
31-
32-
# Build a maxheap.
33-
# Since last parent will be at ((n//2)-1) we can start at that location.
34-
for i in range(n // 2 - 1, -1, -1):
35-
heapify(arr, n, i)
36-
37-
# One by one extract elements
38-
for i in range(n - 1, 0, -1):
39-
arr[i], arr[0] = arr[0], arr[i] # swap
40-
heapify(arr, i, 0)
41-
42-
43-
# Driver code to test above
44-
arr = [12, 11, 13, 5, 6, 7]
45-
heapSort(arr)
46-
n = len(arr)
47-
print("Sorted array is")
48-
for i in range(n):
49-
print("%d" % arr[i]),
1+
# Python program for implementation of heap Sort
2+
3+
# To heapify subtree rooted at index i.
4+
# n is size of heap
5+
def heapify(arr, n, i):
6+
largest = i # Initialize largest as root
7+
l = 2 * i + 1 # left = 2*i + 1
8+
r = 2 * i + 2 # right = 2*i + 2
9+
10+
# See if left child of root exists and is
11+
# greater than root
12+
if l < n and arr[i] < arr[l]:
13+
largest = l
14+
15+
# See if right child of root exists and is
16+
# greater than root
17+
if r < n and arr[largest] < arr[r]:
18+
largest = r
19+
20+
# Change root, if needed
21+
if largest != i:
22+
arr[i], arr[largest] = arr[largest], arr[i] # swap
23+
24+
# Heapify the root.
25+
heapify(arr, n, largest)
26+
27+
28+
# The main function to sort an array of given size
29+
def heapSort(arr):
30+
n = len(arr)
31+
32+
# Build a maxheap.
33+
# Since last parent will be at ((n//2)-1) we can start at that location.
34+
for i in range(n // 2 - 1, -1, -1):
35+
heapify(arr, n, i)
36+
37+
# One by one extract elements
38+
for i in range(n - 1, 0, -1):
39+
arr[i], arr[0] = arr[0], arr[i] # swap
40+
heapify(arr, i, 0)
41+
42+
43+
# Driver code to test above
44+
arr = [12, 11, 13, 5, 6, 7]
45+
heapSort(arr)
46+
n = len(arr)
47+
print("Sorted array is")
48+
for i in range(n):
49+
print("%d" % arr[i]),
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
def Linear_Search(Test_arr, val):
2-
index = 0
3-
for i in range(len(Test_arr)):
4-
if val > Test_arr[i]:
5-
index = i + 1
6-
return index
7-
8-
9-
def Insertion_Sort(Test_arr):
10-
for i in range(1, len(Test_arr)):
11-
val = Test_arr[i]
12-
j = Linear_Search(Test_arr[:i], val)
13-
Test_arr.pop(i)
14-
Test_arr.insert(j, val)
15-
return Test_arr
16-
17-
18-
if __name__ == "__main__":
19-
Test_list = input("Enter the list of Numbers: ").split()
20-
Test_list = [int(i) for i in Test_list]
21-
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}")
1+
def Linear_Search(Test_arr, val):
2+
index = 0
3+
for i in range(len(Test_arr)):
4+
if val > Test_arr[i]:
5+
index = i + 1
6+
return index
7+
8+
9+
def Insertion_Sort(Test_arr):
10+
for i in range(1, len(Test_arr)):
11+
val = Test_arr[i]
12+
j = Linear_Search(Test_arr[:i], val)
13+
Test_arr.pop(i)
14+
Test_arr.insert(j, val)
15+
return Test_arr
16+
17+
18+
if __name__ == "__main__":
19+
Test_list = input("Enter the list of Numbers: ").split()
20+
Test_list = [int(i) for i in Test_list]
21+
print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}")

0 commit comments

Comments
 (0)