From 683f4aa8f7eb333fa2a480070ce503a71f180ae5 Mon Sep 17 00:00:00 2001 From: Xyleee <141914491+priyannshugupta@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:58:50 +0530 Subject: [PATCH 1/3] Create BinarySearch.py --- BinarySearch.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 BinarySearch.py diff --git a/BinarySearch.py b/BinarySearch.py new file mode 100644 index 000000000..4ef010fdd --- /dev/null +++ b/BinarySearch.py @@ -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") From 0865b603d1e3fd466ea781847bb77396cd62789f Mon Sep 17 00:00:00 2001 From: Xyleee <141914491+priyannshugupta@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:59:47 +0530 Subject: [PATCH 2/3] Create LinearSearch.py --- LinearSearch.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 LinearSearch.py diff --git a/LinearSearch.py b/LinearSearch.py new file mode 100644 index 000000000..14e85445b --- /dev/null +++ b/LinearSearch.py @@ -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") From 63a67dba97c67f1a0a41190e19bb601678cea87b Mon Sep 17 00:00:00 2001 From: Xyleee <141914491+priyannshugupta@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:00:36 +0530 Subject: [PATCH 3/3] Create InsertionSort.py --- InsertionSort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 InsertionSort.py diff --git a/InsertionSort.py b/InsertionSort.py new file mode 100644 index 000000000..0ba9b4a21 --- /dev/null +++ b/InsertionSort.py @@ -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)