Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Priyannshugupta patch 3 #1749

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Python program for the execution of binary search algorithm
# The function will return the index of the element if it is present,
# Otherwise, it will return -1

# Defining the function
def binary_search(array, x):
lower = 0
higher = len(array) - 1
middle = 0

while lower <= higher:

middle = (higher + lower) // 2

# If x is greater than the middle element, ignoring the left half
if array[middle] < x:
lower = middle + 1

# If x is smaller than the middle element, ignoring the right half
elif array[middle] > x:
higher = middle - 1

# This means x is present in the middle
else:
return middle

# If the loop ends without returning, then the element is not present in the list
return -1


# Implementing the code
array = [5, 8, 16, 37, 59, 80]

# Case - 1:- Element is present in the list
x = 59

# Calling the function
index = binary_search(array, x)
print(f"Array = {array}")

if index != -1:
print(f"The given element {x} is present at the index", str(index))
else:
print(f"The given element {x} is not present in the array")

# Case - 2:- Element is not present in the array
x = 35

# Calling the function
index = binary_search(array, x)

if index != -1:
print(f"The given element {x} is present at the index", str(index))
else:
print(f"The given element {x} is not present in the array")
27 changes: 27 additions & 0 deletions InsertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Python program for the execution of insertion sort algorithm

# Defining the function
def insertion_sort(array):

# Traversing through 1 to the length of the array
for i in range(1, len(array)):

key = array[i]

# Moving elements of array[0..i-1], that are
# bigger than the key, to one index ahead
# of their current index
c = i-1
while c >= 0 and key < array[c]:
array[c + 1] = array[c]
c -= 1

array[c + 1] = key

# Sorting the array using insertion_sort
array = [23, 42, 3, 83, 36, 49, 19]
print("Unsorted array:", array)

# Calling the function
insertion_sort(array)
print("Sorted array:", array)
42 changes: 42 additions & 0 deletions LinearSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Python program for the execution of linear search algorithm
# The function will loop over every element and return the index if it matches.
# Otherwise, it will return -1

# Defining the function
def linear_search(array, x):

# looping over every element
for i in range(len(array)):
# Comparing the element
if array[i] == x:
return i
# If not found, any index returning -1
return -1



# Implementing the code
array = [5, 8, 16, 37, 59, 80]
print(f"Array = {array}")

# Case - 1:- Element is present in the list
x = 59

# Calling the function
index = linear_search(array, x)

if index != -1:
print(f"The given element {x} is present at the index", str(index))
else:
print(f"The given element {x} is not present in the array")

# Case - 2:- Element is not present in the array
x = 35

# Calling the function
index = linear_search(array, x)

if index != -1:
print(f"The given element {x} is present at the index", str(index))
else:
print(f"The given element {x} is not present in the array")