0% found this document useful (0 votes)
16 views

Linear Search, Binary Search Sorting Etc

The document discusses several sorting algorithms: linear search, binary search, selection sort, insertion sort, and bubble sort. Pseudocode is provided to demonstrate how each algorithm works.

Uploaded by

Rakesh S
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)
16 views

Linear Search, Binary Search Sorting Etc

The document discusses several sorting algorithms: linear search, binary search, selection sort, insertion sort, and bubble sort. Pseudocode is provided to demonstrate how each algorithm works.

Uploaded by

Rakesh S
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/ 10

Linear Search

Source Code:
n=int(input("enter number of terms"))
i=0
b=[]
while i<n:
a=int(input('enter values'))
b.append(a)
i=i+1
a=int(input("enter an element"))
k=0
p=0
for i in range(0,n,1):
if (b[i]==a):
k=1
p=i
if(k==1):
print("element is found in ",p)
else:
print("element is not found")
Binary Search
Source Code:
n=int(input("enter number of terms"))
i=0
b=[]
while i<n:
a=int(input("enter value"))
b.append(a)
i=i+1
print (b)
s=0
ans=int(input("enter element"))
m=n//2
for i in range(0,m,1):
if b[m]==ans:
s=1
break
elif ans>b[m]:
m=m+1
elif ans<b[m]:
m=m-1
if s==1:
print ("element found")
else:
print ("element not found")
Source Code:
A = [64, 25, 12, 22, 11]
for i in range(len(A)):
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i]
print ("Sorted array")
for i in range(len(A)):
print(A[i])
Source Code:
def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print (arr[i])
Bubble Sort
Source Code:
n=int(input("enter number of terms"))
i=0
b=list()
while i<n:
a=int(input('enter values'))
b.append(a)
i=i+1
print(b)
t=0
for i in range(n):
for j in range(0, n-i-1):
if b[j] > b[j+1] :
b[j], b[j+1] = b[j+1], b[j]
print(b)

You might also like