AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Binary search algorithm via Divide et Impera method - Python

Go down

Binary search algorithm via Divide et Impera method - Python

Post by Lucaci Andrei on Mon Feb 13, 2012 9:30 pm

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'''
Created on Feb 14, 2012

@author: sin
'''
import random
n = int(raw_input("Insert range number: "))
a = int(raw_input("Insert searched number: "))
x = []
for i in range(0, n):
x.append(random.randint(0, n))
print x
x.sort()
print x

def ct(a, p, q):
if q < p:
return 0
else:
mid = (p + q) / 2
if x[mid] == a:
return 1
else:
if a < x[mid]:
return ct(a, p, mid - 1)
else:
return ct(a, mid + 1, q)

if ct(a, 0, n - 1) == 0:
print "The value was not found in the list."
else:
print "The value was found in the list."
raw_input("Press any key to exit...")

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 26
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum