Data Structures and Algorithms
Data structures and algorithms are essential concepts in software engineering. Data structures refer to the way data is organized and stored in memory, while algorithms are a set of instructions for solving a particular problem.
Example of binary search algorithm in Python:
def binary_search(arr, x):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1
arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search(arr, x)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
No comments:
Post a Comment