0% found this document useful (0 votes)
3 views5 pages

Data Structures Practical

The document contains Python programs demonstrating various data structures and algorithms including bubble sort, selection sort, insertion sort, linear search, binary search, stack operations, and queue operations. Each section includes code snippets and their corresponding outputs. The examples illustrate fundamental concepts in data structures and their manipulation in Python.

Uploaded by

Snehal Borole
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Data Structures Practical

The document contains Python programs demonstrating various data structures and algorithms including bubble sort, selection sort, insertion sort, linear search, binary search, stack operations, and queue operations. Each section includes code snippets and their corresponding outputs. The examples illustrate fundamental concepts in data structures and their manipulation in Python.

Uploaded by

Snehal Borole
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DATA STRUCTURES PRACTICAL

1. Write a program in python to sort the data using bubble sort

mylist = [64, 34, 25, 12, 22, 11, 90, 5]

n = len(mylist)
for i in range(n-1):
for j in range(n-i-1):
if mylist[j] > mylist[j+1]:
mylist[j], mylist[j+1] = mylist[j+1], mylist[j]
print(mylist)

OUTPUT

[5, 11, 12, 22, 25, 34, 64, 90]

2. Write a program in python to sort the data using selection sort

mylist = [64, 34, 25, 5, 22, 11, 90, 12]

n = len(mylist)
for i in range(n-1):
min_index = i
for j in range(i+1, n):
if mylist[j] < mylist[min_index]:
min_index = j
min_value = [Link](min_index)
[Link](i, min_value)

print(mylist)

OUTPUT

[5, 11, 12, 22, 25, 34, 64, 90]


3. Write a program in python to sort the data using Insertion sort

mylist = [64, 34, 25, 12, 22, 11, 90, 5]

n = len(mylist)
for i in range(1,n):
insert_index = i
current_value = mylist[i]
for j in range(i-1, -1, -1):
if mylist[j] > current_value:
mylist[j+1] = mylist[j]
insert_index = j
else:
break
mylist[insert_index] = current_value

print(mylist)

OUTPUT

[5, 11, 12, 22, 25, 34, 64, 90]

4. Write a program in python to Find the Element using Linear Search

def linearSearch(arr, targetVal):


for i in range(len(arr)):
if arr[i] == targetVal:
return i
return -1

mylist = [3, 7, 2, 9, 5, 1, 8, 4, 6]
x=4

result = linearSearch(mylist, x)

if result != -1:
print("Found at index", result)
else:
print("Not found")

OUTPUT

Found at index 7
5. Write a program in python to Find the Element using Binary Search

def binarySearch(arr, targetVal):


left = 0
right = len(arr) - 1

while left <= right:


mid = (left + right) // 2

if arr[mid] == targetVal:
return mid

if arr[mid] < targetVal:


left = mid + 1
else:
right = mid - 1

return -1

mylist = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]


x = 11

result = binarySearch(mylist, x)

if result != -1:
print("Found at index", result)
else:
print("Not found")

OUTPUT

Found at index 5

6. Write a program in python to insert and delete the element from the stack

stack = []

# Push
[Link]('A')
[Link]('B')
[Link]('C')
print("Stack: ", stack)

# Peek
topElement = stack[-1]
print("Peek: ", topElement)

# Pop
poppedElement = [Link]()
print("Pop: ", poppedElement)

# Stack after Pop


print("Stack after Pop: ", stack)

# isEmpty
isEmpty = not bool(stack)
print("isEmpty: ", isEmpty)

# Size
print("Size: ",len(stack))

OUTPUT
Stack: ['A', 'B', 'C']
Peek: C
Pop: C
Stack after Pop: ['A', 'B']
isEmpty: False
Size: 2

7. Write a program in python to insert and delete the element from the Queue

queue = []

# Enqueue
[Link]('A')
[Link]('B')
[Link]('C')
print("Queue: ", queue)

# Peek
frontElement = queue[0]
print("Peek: ", frontElement)
# Dequeue
poppedElement = [Link](0)
print("Dequeue: ", poppedElement)

print("Queue after Dequeue: ", queue)

# isEmpty
isEmpty = not bool(queue)
print("isEmpty: ", isEmpty)

# Size
print("Size: ", len(queue))

OUTPUT
Queue: ['A', 'B', 'C']
Peek: A
Dequeue: A
Queue after Dequeue: ['B', 'C']
isEmpty: False
Size: 2

You might also like