Алгоритм определения позиции числа в массиве
const = [ ]
Функция создается ниже
def searchFunction( ):
array = []
numbers_In_Array = int(input("how many numbers are in your array ? \n"))
for x in range(numbers_In_Array):
x = int(input("Type the array number : \n"))
array.append(x)
array.sort()
const.append(array) # to copy the array into the empty array
value = int(input("Which number position you want to check : \n")) # The number whose position is to be checked
mid_value_position = (len(array) - 1) // 2 # the number in the middle of the array
# new value created below #
i = 0
j = [] # New array
outOfLimit = True
**A loop inside the function**
while (mid_value_position < len(array)) and outOfLimit == False:
length = len(array)
if value > array[mid_value_position]:
for x in range( mid_value_position + 1 , length):
j = []
j.append(x)
array = j
elif value < array[mid_value_position] :
for x in range( 0 , mid_value_position):
j = []
j.append(x)
array = j
else:
outOfLimit = True
mid_value_position = (len(array) - 1) // 2
print("The position of ", array , "in the array is ", mid_value_position)
Вызов функции
searchFunction()