bin c3 a4re suche algorithmus python programmieren

Solutions on MaxInterview for bin c3 a4re suche algorithmus python programmieren by the best coders in the world

showing results for - "bin c3 a4re suche algorithmus python programmieren"
Silvia
06 Jun 2018
1## searching function
2def search_element(sorted_arr, n, element):
3
4	## array index for iteration
5	i = 0
6
7	## variables to track the search area
8	## initializing them with start and end indexes
9	start = 0
10	end = n - 1
11
12	## iterating over the array
13	while i < n:
14		## getting the index of the middle element
15		middle = (start + end) // 2
16
17		## checking the middle element with required element
18		if sorted_arr[middle] == element:
19			## returning True since the element is in the array
20			return True
21		elif sorted_arr[middle] < element:
22			## moving the start index of the search area to right
23			start = middle + 1
24		else:
25			## moving the end index of the search area to left
26			end = middle - 1
27		i += 1
28	return False
29
30
31if __name__ == '__main__':
32	## initializing the array, length, and element to be searched
33	arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
34	n = 10
35	element_to_be_searched = 9
36	# element_to_be_searched = 11
37
38	if search_element(arr, n, element_to_be_searched):
39		print(element_to_be_searched, "is found")
40	else:
41		print(element_to_be_searched, "is not found")
42