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

Sorting and Searching Algorithms in Python

The document provides implementations of three sorting and searching algorithms: Selection Sort, Merge Sort, and Linear Search. Each algorithm is accompanied by a Python program that demonstrates its functionality with sample inputs and outputs. The Selection Sort and Merge Sort algorithms sort a list of numbers, while the Linear Search algorithm finds the index of a specified element in an array.

Uploaded by

arun1402200514
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)
5 views3 pages

Sorting and Searching Algorithms in Python

The document provides implementations of three sorting and searching algorithms: Selection Sort, Merge Sort, and Linear Search. Each algorithm is accompanied by a Python program that demonstrates its functionality with sample inputs and outputs. The Selection Sort and Merge Sort algorithms sort a list of numbers, while the Linear Search algorithm finds the index of a specified element in an array.

Uploaded by

arun1402200514
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

IMPLEMENT THE SELECTION SORT

Program:

def Selection_Sort(array):
for i in range(0, len(array) - 1):
smallest = i
for j in range(i + 1, len(array)):
if array[j] < array[smallest]:
smallest = j
array[i], array[smallest] = array[smallest], array[i]

array = input('Enter the list of numbers: ').split()


array = [int(x) for x in array]
Selection_Sort(array)
print('List after sorting is : ', end='')
print(array)

Output:

Enter the list of numbers: 2 4 1 3 5


List after sorting is : [1, 2, 3, 4, 5]
IMPLEMENT THE MERGE SORT
Program:
def Merge_Sort(array):
if len(array) > 1:
mid = len(array)//2
Left = array[:mid]
Right = array[mid:]
Merge_Sort(Left)
Merge_Sort(Right)
i=j=k=0
while i < len(Left) and j < len(Right):
if Left[i] < Right[j]:
array[k] = Left[i]
i += 1
else:
array[k] = Right[j]
j += 1
k += 1
while i < len(Left):
array[k] = Left[i]
i += 1
k += 1
while j < len(Right):
array[k] = Right[j]
j += 1
k += 1
def printarray(array):
for i in range(len(array)):
print(array[i], end=" ")
print()
if __name__ == '__main__':
array = [7, 2, 5, 6, 3, 1, 8, 4]
print("Orignal Array is: ", array)
Merge_Sort(array)
print("Sorted array is: ")
printarray(array)

Output:
Orignal Array is: [7, 2, 5, 6, 3, 1, 8, 4]
Sorted array is: 1 2 3 4 5 6 7 8
IMPLEMENT THE LINEAR SEARCH

Program:
def linear_search(arr, a, b):
for i in range(0, a):
if (arr[i] == b):
return i
return -1
arr = [9, 7, 5, 3, 1]
print("The array given is ", arr)
b=5
print("Element to be found is ", b)
a = len(arr)
index = linear_search(arr, a, b)
if(index == -1):
print("Element is not in the list")
else:
print("Index of the element is: ", index)

Output:
The array given is [9, 7, 5, 3, 1]
Element to be found is 5
Index of the element is: 2

You might also like