Added assignment-11 (linear, sentinel, binary, fibonacci) code. Binary and fibonacci don't exactly work
This commit is contained in:
parent
015ff48f61
commit
21e67b031a
@ -13,7 +13,7 @@ In this repository, you'll find codes for Data Structure Lab.
|
|||||||
1. [DSL - Assignment 1 (Cricket, Badminton, Football)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-1.py)
|
1. [DSL - Assignment 1 (Cricket, Badminton, Football)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-1.py)
|
||||||
2. [DSL - Assignment 2 (Marks)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-2.py)
|
2. [DSL - Assignment 2 (Marks)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-2.py)
|
||||||
5. [DSL - Assignment 5 (String)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-5.py)
|
5. [DSL - Assignment 5 (String)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-5.py)
|
||||||
11. DSL - Assignment 11 (Linear, Sentinel, Binary, Fibonnaci Search) - _PENDING_
|
11. [DSL - Assignment 11 (Linear, Sentinel, Binary, Fibonnaci Search)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-11.py)
|
||||||
14. [DSL - Assignment 14 (Selection, Bubble Sorting)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-14.py)
|
14. [DSL - Assignment 14 (Selection, Bubble Sorting)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-14.py)
|
||||||
16. [DSL - Assignment 16 (Quick sort)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-16.py)
|
16. [DSL - Assignment 16 (Quick sort)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-16.py)
|
||||||
19. [DSL - Assignment 19 (Pinnacle Club)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-19.cpp)
|
19. [DSL - Assignment 19 (Pinnacle Club)](https://git.kska.io/sppu-se-comp-codes/DSL/src/branch/main/assignment-19.cpp)
|
||||||
|
124
assignment-11.py
Normal file
124
assignment-11.py
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
# BINARY AND FIBONACCI SEARCH DON'T EXACTLY WORK.
|
||||||
|
# HELP US IF YOU CAN BY CREATING AN ISSUE.
|
||||||
|
|
||||||
|
# List to store roll numbers of students that attended the program
|
||||||
|
programRoll=[]
|
||||||
|
attendees=int(input("Enter the number of students attending the program:\t"))
|
||||||
|
begin=0
|
||||||
|
|
||||||
|
# Function to take input for roll numbers in list
|
||||||
|
def attendInput():
|
||||||
|
for i in range(attendees):
|
||||||
|
roll=int(input(f"Enter roll number for student {i+1}:\t"))
|
||||||
|
programRoll.append(roll)
|
||||||
|
print(f"Roll numbers of {attendees} students that attended the program are:\t {programRoll}")
|
||||||
|
|
||||||
|
# Linear search
|
||||||
|
def linearSearch(roll_list, roll_search):
|
||||||
|
for i in roll_list:
|
||||||
|
if i==roll_search:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# Sentinel search
|
||||||
|
def sentinelSearch(roll_list, roll_search, n):
|
||||||
|
end=roll_list[n-1]
|
||||||
|
roll_list[n-1]=roll_search
|
||||||
|
i=0
|
||||||
|
while (roll_list[i] != roll_search):
|
||||||
|
i+=1
|
||||||
|
roll_list[n-1]=end
|
||||||
|
if ((i<n-1) or (roll_search==roll_list[n-1])):
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
# Binary search
|
||||||
|
def binarySearch(roll_list, roll_search, begin, n):
|
||||||
|
if begin>n:
|
||||||
|
return -1
|
||||||
|
mid=(begin+n)//2
|
||||||
|
if roll_search==roll_list[mid]:
|
||||||
|
return mid
|
||||||
|
if roll_search<roll_list[mid]:
|
||||||
|
return binarySearch(roll_list, roll_search, begin, mid-1)
|
||||||
|
else:
|
||||||
|
return binarySearch(roll_list,roll_search,mid+1,n)
|
||||||
|
|
||||||
|
# Fibonacci search
|
||||||
|
def fibonacciSearch(students_attend, search_element, n):
|
||||||
|
fibMMm2 = 0
|
||||||
|
fibMMm1 = 1
|
||||||
|
fibM = fibMMm2 + fibMMm1
|
||||||
|
while (fibM<n):
|
||||||
|
fibMMm2=fibMMm1
|
||||||
|
fibMMm1=fibM
|
||||||
|
fibM=fibMMm2+fibMMm1
|
||||||
|
offset=-1;
|
||||||
|
while (fibM>1):
|
||||||
|
i=min(offset+fibMMm2, n-1)
|
||||||
|
if (students_attend[i]<search_element):
|
||||||
|
fibM=fibMMm1
|
||||||
|
fibMMm1=fibMMm2
|
||||||
|
fibMMm2=fibM-fibMMm1
|
||||||
|
offset=i
|
||||||
|
elif (students_attend[i]>search_element):
|
||||||
|
fibM=fibMMm2
|
||||||
|
fibMMm1=fibMMm1-fibMMm2
|
||||||
|
fibMMm2=fibM-fibMMm1
|
||||||
|
else:
|
||||||
|
return i
|
||||||
|
if(fibMMm1 and students_attend[offset+1]==search_element):
|
||||||
|
return offset+1;
|
||||||
|
else:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
# Input for marks and search roll
|
||||||
|
attendInput()
|
||||||
|
searchAttendee=int(input("\nEnter the roll number you want to search:\t"))
|
||||||
|
|
||||||
|
# Options
|
||||||
|
while True:
|
||||||
|
print("\n----- CHOOSE SEARCH TYPE -----")
|
||||||
|
print("1 -> Linear search")
|
||||||
|
print("2 -> Sentinel search")
|
||||||
|
print("3 -> Binary search")
|
||||||
|
print("4 -> Fibonacci search")
|
||||||
|
print("5 -> Exit")
|
||||||
|
optn=int(input("\nChoose an option (1-5):\t"))
|
||||||
|
|
||||||
|
if (optn==1):
|
||||||
|
result=linearSearch(programRoll, searchAttendee)
|
||||||
|
if (result==1):
|
||||||
|
print(f"\nRoll number {searchAttendee} attended the program.\n")
|
||||||
|
else:
|
||||||
|
print(f"\nRoll number {searchAttendee} has either not been added to the list or was absent.\n")
|
||||||
|
elif (optn==2):
|
||||||
|
result=sentinelSearch(programRoll, searchAttendee, attendees)
|
||||||
|
if (result==1):
|
||||||
|
print(f"\nRoll number {searchAttendee} attended the program.\n")
|
||||||
|
else:
|
||||||
|
print(f"\nRoll number {searchAttendee} has either not been added to the list or was absent.\n")
|
||||||
|
elif (optn==3):
|
||||||
|
result=binarySearch(programRoll, searchAttendee, begin, attendees)
|
||||||
|
if (result==-1):
|
||||||
|
print(f"\nRoll number {searchAttendee} attended the program.\n")
|
||||||
|
else:
|
||||||
|
print(f"\nRoll number {searchAttendee} has either not been added to the list or was absent.\n")
|
||||||
|
elif (optn==4):
|
||||||
|
result=fibonacciSearch(programRoll, searchAttendee, attendees)
|
||||||
|
if (result==-1):
|
||||||
|
print(f"\nRoll number {searchAttendee} attended the program.\n")
|
||||||
|
else:
|
||||||
|
print(f"\nRoll number {searchAttendee} has either not been added to the list or was absent.\n")
|
||||||
|
elif (optn==5):
|
||||||
|
print("## DESIGNED AND ENGINEERED BY KSHITIJ\n## END OF CODE")
|
||||||
|
quit()
|
||||||
|
else:
|
||||||
|
print("\nPlease choose a valid option (1-5)\n")
|
||||||
|
main()
|
||||||
|
|
||||||
|
# Calling main function
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user