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

Samad - 3.1

Uploaded by

Abdul Samad khan
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)
3 views4 pages

Samad - 3.1

Uploaded by

Abdul Samad khan
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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 3.1

Student Name: Abdul Samad Khan UID: 21BCS1843


Branch: CSE Section/Group: 604-A
Semester: 4th Date of Performance: 03-05-2023
Subject Name: Programming in Python Lab Subject Code: 21CSP-259

AIM: Program to implement various kinds of searching and sorting algorithms

SOURCE CODE AND IMPLEMENTATION:

1. Python program to implement linear search.


def linear_Search(list1, n, key):
for i in range(0, n):
if (list1[i] == key):
return i
return -1
list1 = [1 ,3, 5, 4, 7, 9]
key = 7 n
= len(list1)
res = linear_Search(list1, n,
key) if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2. Python program to implement bubble sort.


def bubbleSort(nlist): for passnum
in range(len(nlist)-1,0,-1): for i
in range(passnum): if
nlist[i]>nlist[i+1]: temp =
nlist[i] nlist[i] = nlist[i+1]
nlist[i+1] = temp

nlist = [14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)

3. Python program to implement binary search without recursion.


def binary_searchiter(arr,
n):
low = 0 high =
len(arr) - 1 mid =
0 while low <=
high:
mid = (high + low)
// 2 if arr[mid] < n:
low = mid + 1 elif
arr[mid] > n:
high = mid - 1 else:
return mid return -1
arr = [4, 7, 9, 16, 20, 25]
n=7

r = binary_searchiter(arr,
n) if r != -1: \

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

print("Element found at index", str(r))


else: print("Element is not present in
array")

4. Python program to implement selection sort.


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))

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Abdul Samad Khan 21BCS1843

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