Searching and Sorting Algorithms in DSA Python
by DataFlair Team
Program 1
# Implementation of single linked list
import os
class Node:
def __init__(self):
self.data=None
self.add=None
class LinkedList:
def __init__(self):
self.start=None
self.count=0
def create(self):
n=int(input("Enter First element: "))
self.start=Node()
self.start.data=n
self.start.add=None
self.temp=self.start
self.count=self.count+1
ch=input("Wan to continue(Y/y): ")
while(ch=='y' or ch=='Y'):
self.count=self.count+1
n=int(input("Enter next element: "))
newnode=Node()
newnode.data=n # assign data to new node
newnode.add=None # assign address to new node
self.temp.add=newnode
self.temp=self.temp.add
ch=input("Wan to continue(Y/y): ")
def display(self):
if(self.start==None):
print("List is empty")
else:
print("Elements of List: ")
self.temp=self.start
while(self.temp!=None):
print(self.temp.data,end=" ")
self.temp=self.temp.add
print("\n Total Node is : ",self.count)
def insertFirst(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count+1
n=int(input("Enter an element for insert: "))
newnode=Node()
newnode.data=n
newnode.add=None
newnode.add=self.start
self.start=newnode
def insertMiddle(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count+1
n=int(input("Enter an element for insert: "))
newnode=Node()
newnode.data=n
pos=int(input("Enter node position for insert: "))
i=1
next=self.start
while(i<pos):
prev=next
next=next.add
i=i+1
prev.add=newnode
newnode.add=next
def insertLast(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count+1
n=int(input("Enter an element for insert: "))
newnode=Node()
newnode.data=n
last=self.start
while(last.add!=None):
last=last.add
last.add=newnode
def deleteFirst(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count-1
self.temp=self.start
self.start=self.start.add
print("Deleted node is : ",self.temp.data)
del self.temp
def deleteMiddle(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count-1
i=1
pos=int(input("Enter node position for delete: "))
next=self.start
while(i<pos):
prev=next
next=next.add
i=i+1
self.temp=next
next=next.add
prev.add=next
print("Deleted node is : ",self.temp.data)
del self.temp
def deleteLast(self):
if(self.start==None):
print("List is empty")
else:
self.count=self.count-1
self.temp=self.start
while(self.temp.add!=None):
last=self.temp
self.temp=self.temp.add
print("Deleted Node is : ",self.temp.data)
last.add=None
del self.temp
self.temp=None
def countNode(self):
if(self.start==None):
print("List is empty")
else:
print("Total Node is : ",self.count)
def sortList(self):
if(self.start==None):
print("List is empty")
else:
prev=self.start
while(prev!=None): # Outer
next=prev.add
while(next!=None): # Inner
if(next.data<prev.data):
prev.data,next.data=next.data,prev.data # Swaping
next=next.add
prev=prev.add
def searchData(self):
if(self.start==None):
print("List is empty")
else:
s=int(input("Enter an element for search: "))
temp=self.start
flag=False
while(temp!=None):
if(temp.data==s):
flag=True
break
temp=temp.add
if(flag==True):
print("Searching success")
else:
print("Searching not success")
# Main
os.system('cls')
mylist=LinkedList()
while(1):
print("\n --------------------Linked List------------------------")
print("1. Create")
print("2. Display")
print("3. Insert First")
print("4. Insert Middle")
print("5. Insert Last")
print("6. Delete First ")
print("7. Delete Middle ")
print("8. Delete Last ")
print("9. Count Node ")
print("10. Searching")
print("11.Sorting")
print("12. Exit")
print("---------------------------------------------------------")
choice=int(input("Enter your choice: "))
if(choice==1):
mylist.create()
elif(choice==2):
mylist.display()
elif(choice==3):
mylist.insertFirst()
elif(choice==4):
mylist.insertMiddle()
elif(choice==5):
mylist.insertLast()
elif(choice==6):
mylist.deleteFirst()
elif(choice==7):
mylist.deleteMiddle()
elif(choice==8):
mylist.deleteLast()
elif(choice==9):
mylist.countNode()
elif(choice==10):
mylist.searchData()
elif(choice==11):
mylist.sortList()
else:
break
Tags: dsa pythondsa python practicaldsa python programdsa python searching and sortingsearching algorithms in dsa pythonsearching and sorting algorithmssearching and sorting algorithms in dsa pythonsearching and sorting algorithms in pythonsearching and sorting in dsa pythonsorting algorithms in dsa python
DataFlair Team
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.