You are on page 1of 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 3.1

Student Name: Amleshwar Pandey UID: 22BCS80085


Branch: CSE Section/Group: 912 A
Semester:4th Date of Performance:22/04
Subject Name: Python Lab Subject Code: 21CSP 259

1. Aim: Program to implement various searching and sorting algorithms

1. Python program to implement linear search.


2. Python program to implement bubble sort.
3. Python program to implement binary search without recursion.
4. Python program to implement selection sort

Code chef Problem


Chef loves research! Now he is looking for subarray of maximal length with non-zero product.
Chef has an array A with N elements: A1, A2, ..., AN.
Subarray Aij of array A is elements from index i to index j: Ai, Ai+1, ..., Aj.
Product of subarray Aij is product of all its elements (from ith to jth).

Input
First line contains single integer N denoting the number of elements.
Second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.

Output
In a single line print single integer - the maximal length of subarray with non-zero product.

2. Source Code:

1. Python program to implement linear search.

def LinearSearch(array, n, k):

for j in range(0, n):


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if (array[j] == k):

return j

return -1

array = [1, 3, 5, 7, 9]

k=7
n = len(array)

result = LinearSearch(array, n, k)

if(result == -1):

print("Element not found")

else:

print("Element found at index: ", result)

2. Program to implement bubble sort

def bubbleSort(array):
# access each element
for a in range(len(array)):
# compare array elements
for b in range(0, len(array) - a - 1):
# compare
if array[b] > array[b + 1]:

# swapping elements
temp = array[b]
array[b] = array[b+1]
array[b+1] = temp

#driver code
array = [5, 4, 2, 1, 3]
print("The original array is: ", array)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
bubbleSort(array)
print('The Sorted Array is: ', array)

3. Python program to implement binary search without recursion.

def binary_search(my_list, elem):


low = 0
high = len(my_list) - 1
mid = 0

while low <= high:

mid = (high + low) // 2


if my_list[mid] < elem:
low = mid + 1

elif my_list[mid] > elem:


high = mid - 1

else:
return mid

return -1

my_list = [ 1, 9, 11, 21, 34, 54, 67, 90 ]


elem_to_search = 1
print("The list is")
print(my_list)

my_result = binary_search(my_list, elem_to_search)

if my_result != -1:
print("Element found at index ", str(my_result))
else:
print("Element not found!") def binary_search(my_list, elem):
low = 0
high = len(my_list) - 1
mid = 0

while low <= high:

mid = (high + low) // 2


if my_list[mid] < elem:
low = mid + 1

elif my_list[mid] > elem:


high = mid - 1

else:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
return mid

return -1

my_list = [ 1, 9, 11, 21, 34, 54, 67, 90 ]


elem_to_search = 1
print("The list is")
print(my_list)

my_result = binary_search(my_list, elem_to_search)

if my_result != -1:
print("Element found at index ", str(my_result))
else:
print("Element not found!")

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

5. Codechef

if len(arr) == 1:
if arr[0] == 0:
count = 0
else:
count += 1
al.append(count)
else:
for j in range(n):
if arr[j] > 0:
count += 1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if arr[j] == 0:
al.append(count)
count = 0
continue
if j == n - 1:
al.append(count)

max_val = -sys.maxsize - 1
for j in range(len(al)):
if al[j] > max_val:
max_val = al[j]

print(max_val)

3. Screenshot of Outputs:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like