Sep-02-2017, 07:00 AM
please help me. i am unable to understand IndexError.
Enter list of element:2,3,4,5,6
Enter search element:2
Value of index 2
Value of element 4
Value of index 0
Value of element 2
element 2 found at location 0
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:3
Value of index 2
Value of element 4
Value of index 0
Value of element 2
Value of index 1
Value of element 3
element 3 found at location 1
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:3
Value of index 2
Value of element 4
Value of index 0
Value of element 2
Value of index 1
Value of element 3
element 3 found at location 1
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:5
Value of index 2
Value of element 4
Value of index 5
Traceback (most recent call last):
File "C:/python/program/binary.py", line 18, in <module>
binarySearch(arr,0,len(arr)-1,element)
File "C:/python/program/binary.py", line 10, in binarySearch
return binarySearch(arr,mid+1,last,element)
File "C:/python/program/binary.py", line 4, in binarySearch
print("Value of element ",arr[mid])
IndexError: list index out of range
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:6
Value of index 2
Value of element 4
Value of index 5
Traceback (most recent call last):
File "C:/python/program/binary.py", line 18, in <module>
binarySearch(arr,0,len(arr)-1,element)
File "C:/python/program/binary.py", line 10, in binarySearch
return binarySearch(arr,mid+1,last,element)
File "C:/python/program/binary.py", line 4, in binarySearch
print("Value of element ",arr[mid])
IndexError: list index out of range
def binarySearch (arr,first,last,element): mid = first+last//2 print("Value of index ",mid) print("Value of element ",arr[mid]) if last>=first: if(element==arr[mid]): print("element",element,"found at location ",mid) else: if(element>arr[mid]): return binarySearch(arr,mid+1,last,element) else: return binarySearch(arr,first,mid-1,element) else: print("Element is not present in array") arr = [int(x) for x in input("Enter list of element:").split(',')] element=int(input("Enter search element:")) binarySearch(arr,0,len(arr)-1,element)==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:2
Value of index 2
Value of element 4
Value of index 0
Value of element 2
element 2 found at location 0
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:3
Value of index 2
Value of element 4
Value of index 0
Value of element 2
Value of index 1
Value of element 3
element 3 found at location 1
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:3
Value of index 2
Value of element 4
Value of index 0
Value of element 2
Value of index 1
Value of element 3
element 3 found at location 1
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:5
Value of index 2
Value of element 4
Value of index 5
Traceback (most recent call last):
File "C:/python/program/binary.py", line 18, in <module>
binarySearch(arr,0,len(arr)-1,element)
File "C:/python/program/binary.py", line 10, in binarySearch
return binarySearch(arr,mid+1,last,element)
File "C:/python/program/binary.py", line 4, in binarySearch
print("Value of element ",arr[mid])
IndexError: list index out of range
>>>
==================== RESTART: C:/python/program/binary.py ====================
Enter list of element:2,3,4,5,6
Enter search element:6
Value of index 2
Value of element 4
Value of index 5
Traceback (most recent call last):
File "C:/python/program/binary.py", line 18, in <module>
binarySearch(arr,0,len(arr)-1,element)
File "C:/python/program/binary.py", line 10, in binarySearch
return binarySearch(arr,mid+1,last,element)
File "C:/python/program/binary.py", line 4, in binarySearch
print("Value of element ",arr[mid])
IndexError: list index out of range