23
loading...
This website collects cookies to deliver better user experience
in
method.a=[1,3,4,6,5,2,6]
n=int(input("Please enter the number to be searched "))
#Method-1
for i in range(len(a)):
if (a[i]==n):
print("Method-1 Yes, the number is in the list ")
break
#Method-2
if(n in a):
print("Method-2 Yes, the number is in the list ")
Please enter the number to be searched 6
Method-1 Yes, the number is in the list
Method-2 Yes, the number is in the list
a=[1,3,5,6,8,7,10,12,14]
x=int(input("Please enter a number "))
low = 0
high = len(a) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
# If x is greater, ignore left half
if (a[mid] < x):
low = mid + 1
# If x is smaller, ignore right half
elif (a[mid] > x):
high = mid - 1
# means x is present at mid
else:
print(mid+1)
break
Please enter a number 6
4
a=[2,4,3,7,6,5,9,10,12]
# Traverse through all array elements
for i in range(len(a)):
# Last i elements are already in place
for j in range(0, len(a)-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if (a[j] > a[j+1]) :
(a[j], a[j+1]) = (a[j+1], a[j]) #Swapping the two
print ("Sorted array is:",a)
Sorted array is: [2, 3, 4, 5, 6, 7, 9, 10, 12]