Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 58 additions & 36 deletions BinarySearch.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
#!/usr/bin/python3
import mysql.connector
import time

a=time.time()
class DBHelper:
def fetchAllStudentInDB(self):
# 1. create SQL statement
sql = "select * from Student"

def binsearch(arr, low, high, x):

"""Function for Binary Search"""

if(low >= high):
mid = (low+high)//2
if(arr[mid] >x):
return binsearch(arr, low, mid-1,x)
elif(arr[mid] <x):
return binsearch(arr, mid+1, high,x)
elif(arr[mid] == x):
return mid
else:
return -1

"""fetching the data from the database"""

sql = "select roll_no from data"
con = mysql.connector.connect(user="*****", password="********", host="127.0.0.1", database="students")
cursor = con.cursor()
cursor.execute(sql)
r_no = []
rows = cursor.fetchall()
for row in rows:
r_no.append(row[0])

arr = r_no.copy()
arr.sort()
x = int(input("Enter the Roll Number you want to search in the database:"))
x = binsearch(arr, 0, len(arr)-1, x)

if (result!= -1):
print("The Roll No is found at the position: " + str(result+1) + " in the Database")
else:
print("Sorry the Roll No " + str(x) + " is not found in the Database! ")
# 2.create connection with database
con = mysql.connector.connect(user="root", password="", host="127.0.0.1", database="DaaLab")

# 3. Obtain Cursor to execute sql statements
cursor = con.cursor()
cursor.execute(sql)

rows=cursor.fetchall()
#print(rows) #print all rows , row is a list of Tuples , 1Tuple represents row, print rows as list of tuples
list=[]
for row in rows:
rm=row[2] #print all rows in next lines
list.append(rm)
print("list of roll numbers are:")
for i in list:
print(i)

roll=int(input("Enter the roll number you want to search"))
beg=0
end=len(list)
while(beg<end):
mid=int((beg+end)/2)
if (roll==list[mid]):
print("Rollnumber",roll," is found at location",mid,"of the list.")
break
elif(roll < list[mid]):
end=mid-1
elif (roll > list[mid]):
beg = mid + 1
else:
print("wrong input")


# Model

class Student:

def __init__(self, name, urn, phone):
self.name = name
self.urn = urn
self.phone = phone

def showStudentDetails(self):
print(">> Name: {} urn: {} Phone: {}".format(self.name, self.urn, self.phone))


db = DBHelper()
db.fetchAllStudentInDB()

b=time.time()
c=b-a
print("time taken=",c)