0% found this document useful (0 votes)
28 views3 pages

Exp-3 1

This document contains 4 questions and their solutions that demonstrate different searching and sorting algorithms in Python. Question 1 implements linear search on a list to find an element. Question 2 implements bubble sort to sort a list in ascending order. Question 3 performs a binary search to find an element in a sorted list. Question 4 implements selection sort to sort an array by iteratively finding the minimum element and swapping it into the correct position.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Exp-3 1

This document contains 4 questions and their solutions that demonstrate different searching and sorting algorithms in Python. Question 1 implements linear search on a list to find an element. Question 2 implements bubble sort to sort a list in ascending order. Question 3 performs a binary search to find an element in a sorted list. Question 4 implements selection sort to sort an array by iteratively finding the minimum element and swapping it into the correct position.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


WORKSHEET 3.1

Student Name: Aisheek Mazumder UID: 21BCS9741


Branch: CSE Section/Group: 21BCS-703-B
Semester: 4th Date of Performance: 13-04-2023
Subject Name: Programming in python lab Subject Code: 21-CSP-259

Aim: Programs to demonstrate the various searching and sorting


algorithms.
Q1. Write a python program to implement linear search

Program Code:
def search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = [10, 20, 80, 30, 60, 50, 110, 100, 130, 170]
x = 20;
if search(arr,x)== -1:
print("Element not found")
else:
print("Element found at index",search(arr,x))

Screenshot of Output:

Q2. Write a python program to implement bubble sort

Program Code:
def bubble_sort(list1):
for i in range(0,len(list1)-1):
for j in range(len(list1)-1):
if(list1[j]>list1[j+1]):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
temp = list1[j]
list1[j] = list1[j+1]
list1[j+1] = temp
return list1

list1 = [5, 3, 8, 6, 7, 2]
print("The unsorted list is: ", list1)
print("The sorted list is: ", bubble_sort(list1))

Screenshot of Output:

Q3. Write a python program to implement binary search

Program Code:
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid - 1
else:
return mid
return -1

list1 = [12, 24, 32, 39, 45, 50, 54]


n = 45
result = binary_search(list1, n)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list1")
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Screenshot of Output:

Q4. Write a python program to implement selection sort

Program Code:
def selection_sort(array):
length = len(array)
for i in range(length-1):
minIndex = i
for j in range(i+1, length):
if array[j]<array[minIndex]:
minIndex = j
array[i], array[minIndex] = array[minIndex], array[i]

return array

array = [21,6,9,33,3]
print("The sorted array is: ", selection_sort(array))

Screenshot of Output:

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