Skip to content

Commit

Permalink
typo corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
arifaisal123 committed May 12, 2023
1 parent d635926 commit df58d65
Show file tree
Hide file tree
Showing 33 changed files with 366 additions and 407 deletions.
41 changes: 0 additions & 41 deletions Sorting Algorithims/Quick_Sort.py

This file was deleted.

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.
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.
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.
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)}")
Loading

0 comments on commit df58d65

Please sign in to comment.