0% found this document useful (0 votes)
9 views

DSA Programs

Uploaded by

saitejakasani091
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DSA Programs

Uploaded by

saitejakasani091
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

5/3/23, 1:05 PM python - Jupyter Notebook

LINEAR SEARCH
In [2]:

def search(lis,key):
for i in range(len(lis)-1):
if lis[i]==key:
return i
return -1

num_list = [98,34,3,12,84,45]
key = int(input('Enter the number to find: '))
index = search(num_list,key)
if index == -1:
print('Element is not found.')
else:
print('Element is found at: ',index)

Enter the number to find: 12


Element is found at: 3

BINARY SEARCH
In [3]:

def binary_search(lis,key):
start = 0
end = len(lis)-1
mid = 0
while start<=end:
mid = (start+end)//2
if lis[mid]>key:
end = mid-1
elif lis[mid]<key:
start = mid+1
else:
return mid
return -1

num_list = [98,34,3,12,84,45]
key = int(input('Enter the number to find: '))
num_list.sort()
index = binary_search(num_list,key)
if index == -1:
print('Element is not found.')
else:
print('Element is found at: ',index)

Enter the number to find: 25


Element is not found.

BUBBLE SORT

localhost:8888/notebooks/sri/python.ipynb 1/9
5/3/23, 1:05 PM python - Jupyter Notebook

In [9]:

def bubble_sort(lis):
size = len(lis)
for i in range(size):
swap = False
for j in range(0,size-i-1):
if lis[j]>lis[j+1]:
lis[j],lis[j+1]=lis[j+1],lis[j]
swap = True
if not swap:
break
return lis

lis = [98,34,3,12,84,45]
print('unsorted list: ',lis)
sort = bubble_sort(lis)
print('sorted list: ',sort)

unsorted list: [98, 34, 3, 12, 84, 45]


sorted list: [3, 12, 34, 45, 84, 98]

SELECTION SORT
In [10]:

def selection_sort(lis):
for i in range(len(lis)):
min_index = i
for j in range(i+1,len(lis)):
if lis[j]<lis[min_index]:
min_index=j
lis[min_index],lis[i]=lis[i],lis[min_index]
return lis

lis = [98,34,3,12,84,45]
print('unsorted list: ',lis)
selection_sort(lis)
print('sorted list: ',lis)

unsorted list: [98, 34, 3, 12, 84, 45]


sorted list: [3, 12, 34, 45, 84, 98]

MERGE SORT

localhost:8888/notebooks/sri/python.ipynb 2/9
5/3/23, 1:05 PM python - Jupyter Notebook

In [11]:

def merge_sort(l):
if len(l)<=1:
return l
else:
mid = len(l)//2
left_list=l[:mid]
right_list = l[mid:]
left_sort = merge_sort(left_list)
right_sort = merge_sort(right_list)
result = []
i = j = 0
while i <len(left_sort) and j<len(right_sort):
if left_sort[i]<right_sort[j]:
result.append(left_sort[i])
i+=1
else:
result.append(right_sort[j])
j+=1

result.extend(left_sort[i:])
result.extend(right_sort[j:])
return result

lis = [98,34,3,12,84,45]
print('unsorted list: ',lis)
sort = merge_sort(lis)
print('sorted list: ',sort)

unsorted list: [98, 34, 3, 12, 84, 45]


sorted list: [3, 12, 34, 45, 84, 98]

QUICK SORT

localhost:8888/notebooks/sri/python.ipynb 3/9
5/3/23, 1:05 PM python - Jupyter Notebook

In [18]:

def quick_sort(l,start,end):
if start<end:
pi = partition(l,start,end)
quick_sort(l,start,pi-1)
quick_sort(l,pi+1,end)
return l
def partition(l,start,end):
pivot_index = start
pivot = l[pivot_index]
while start<end:
while start<len(l) and l[start]<= pivot:
start +=1
while l[end] > pivot :
end -= 1
if start<=end:
l[start],l[end]=l[end],l[start]
if end!=pivot_index:
l[pivot_index],l[end]=l[end],l[pivot_index]
return end

lis = [98,34,3,12,84,45]
print('unsorted list: ',lis)
sort = quick_sort(lis,0,len(lis)-1)
print('sorted list: ',sort)

unsorted list: [98, 34, 3, 12, 84, 45]


sorted list: [3, 12, 34, 45, 84, 98]

STACK IMPLEMENTATION

localhost:8888/notebooks/sri/python.ipynb 4/9
5/3/23, 1:05 PM python - Jupyter Notebook

In [14]:

class stack:
def __init__(self):
self.stk = []

def display(self):
print(self.stk)

def is_empty(self):
return self.stk==[]

def push(self,data):
self.stk.append(data)
return

def pop(self):
if self.is_empty():
print("stack is empty....")
else:
ele = self.stk.pop()
print(ele)

def peek(self):
top = self.stk[-1]
print(top)

s = stack()
s.push(3)
s.push(4)
s.push(5)
s.push(6)
s.peek()
s.pop()
s.peek()
s.display()
s.pop()

6
6
5
[3, 4, 5]
5

QUEUE IMPLEMENTATION

localhost:8888/notebooks/sri/python.ipynb 5/9
5/3/23, 1:05 PM python - Jupyter Notebook

In [15]:

class queue:
def __init__(self):
self.que = []

def display(self):
print(self.que)

def is_empty(self):
return self.que==[]

def enqueue(self,data):
self.que.append(data)
return

def dequeue(self):
if self.is_empty():
print("Queue is empty.....")
else:
ele = self.que.pop(0)
print(ele)

def size(self):
size = len(self.que)
print(size)

q = queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
q.dequeue()

[1, 2, 3, 4, 5]
1
2

LINKED LIST

localhost:8888/notebooks/sri/python.ipynb 6/9
5/3/23, 1:05 PM python - Jupyter Notebook

localhost:8888/notebooks/sri/python.ipynb 7/9
5/3/23, 1:05 PM python - Jupyter Notebook

In [16]:

class node:
def __init__(self,data=None):
self.data = data
self.next = None

class linked_list:
def __init__(self):
self.head = None

def list_print(self):
print_val = self.head
while print_val is not None:
print(print_val.data)
print_val = print_val.next

def add_begin(self,add):
new_node = node(add)
new_node.next = self.head
self.head = new_node

def add_end(self,add):
new_node = node(add)
if self.head == None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node

def add_bw(self,prev,new):
if prev is None:
return
new_node = node(new)
new_node.next = prev.next
prev.next = new_node

def delete(self,pos):
if self.head == None:
return

temp = self.head
if pos == 0:
self.head = temp.next
temp = None
return

for i in range(pos-1):
temp = temp.next
if temp is None:
break

if temp is None:
return

if temp.next is None:
return

nex = temp.next.next
localhost:8888/notebooks/sri/python.ipynb 8/9
5/3/23, 1:05 PM python - Jupyter Notebook

temp.next = None
temp.next = nex

ele1 = linked_list()
ele1.head = node(111)
ele2 = node(112)
ele1.head.next = ele2
ele3 = node(113)
ele2.next = ele3
ele1.add_begin(114)
ele1.add_bw(ele2,115)
ele1.list_print()
114
111
112
115
113

localhost:8888/notebooks/sri/python.ipynb 9/9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy