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

Unit - I-3

The document provides Python implementations for linear search, binary search, selection sort, and insertion sort algorithms, along with examples of their usage. It also includes a function to find the first n prime numbers, demonstrating how to check for prime numbers and count them. Each section includes code snippets and sample outputs to illustrate the functionality of the algorithms.

Uploaded by

ayes1606
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)
16 views5 pages

Unit - I-3

The document provides Python implementations for linear search, binary search, selection sort, and insertion sort algorithms, along with examples of their usage. It also includes a function to find the first n prime numbers, demonstrating how to check for prime numbers and count them. Each section includes code snippets and sample outputs to illustrate the functionality of the algorithms.

Uploaded by

ayes1606
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
You are on page 1/ 5

5.

Linear search and Binary search

Linear search

# Linear Search in Python

def linear_Search(list1, n, key):

# Searching list1 sequentially

for i in range(0, n):

if (list1[i] == key):

return i

return -1

list1 = [1 ,3, 5, 4, 7, 9]

print(f" The list of numbers {list1}")

key=int(input("Enter the element to find from list"))

n = len(list1)

res = linear_Search(list1, n, key)

if(res == -1):

print("Element not found")

else:

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

Output :

The list of numbers [1, 3, 5, 4, 7, 9]

Enter the element to find from list 4

Element found at index: 3

The list of numbers [1, 3, 5, 4, 7, 9]

Enter the element to find from list 10

Element not found

5 b) Binary Search

# Iterative Binary Search Function method Python Implementation

# It returns index of n in given list1 if present,


# else returns -1

def binary_search(list1, n):

low = 0

high = len(list1) - 1

mid = 0

while low <= high:

# for get integer result

mid = (high + low) // 2

# Check if n is present at mid

if list1[mid] < n:

low = mid + 1

# If n is greater, compare to the right of mid

elif list1[mid] > n:

high = mid - 1

# If n is smaller, compared to the left of mid

else:

return mid

# element was not present in the list, return -1

return -1

# Initial list1

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

print(f"The list of numbers {list1}")


n=int(input('Enter the element to find from list'))

# Function call

result = binary_search(list1, n)

if result != -1:

print("Element is present at index", str(result))

else:

print("Element is not present in list1")

output:

The list of numbers [12, 24, 32, 39, 45, 50, 54]

Enter the element to find from list 39

Element is present at index 3

The list of numbers [12, 24, 32, 39, 45, 50, 54]

Enter the element to find from list 70

Element is not present in list1

6. Selection sort, Insertion sort

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

output :

The sorted array is: [3, 6, 9, 21, 33]

b) Insertion Sort

def insertion_sort(list1):

# Outer loop to traverse through 1 to len(list1)

for i in range(1, len(list1)):

value = list1[i]

# Move elements of list1[0..i-1], that are greater than value, to one position ahead

# of their current position

j=i-1

while j >= 0 and value < list1[j]:

list1[j + 1] = list1[j]

j -= 1

list1[j + 1] = value

return list1

# Driver code to test above

list1 = [10, 5, 13, 8, 2]

print("The unsorted list is:", list1)

print("The sorted list1 is:", insertion_sort(list1))

output :

The unsorted list is: [10, 5, 13, 8, 2]

The sorted list1 is: [2, 5, 8, 10, 13]

7. First n prime numbers

prime=0

def primenum(x):

if x>=2:

for y in range(2,x):

if not(x%y):
return False

else:

return False

return True

for i in range(int(input("How many numbers you wish to check:\n "))):

if primenum(i):

prime+=1

print(i,end="\t")

print("We found "+str(prime)+ " prime numbers.")

output 1 :

How many numbers you wish to check:

30

2 3 5 7 11 13 17 19 23 29

We found 10 prime numbers.

output 2 :

How many numbers you wish to check:

20

2 3 5 7 11 13 17 19

We found 8 prime numbers.

You might also like