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

Os Assignment

The document contains details of 8 sorting algorithms experiments: 1. Linear search 2. Binary search 3. Insertion sort 4. Selection sort 5. Bubble sort 6. Quick sort 7. Merge sort 8. Heap sort For each experiment, the aim, input, and output is provided. The inputs include code implementations of the sorting algorithms and sample test arrays. The outputs show the results of applying the algorithms to the test arrays.

Uploaded by

slasherftw27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Os Assignment

The document contains details of 8 sorting algorithms experiments: 1. Linear search 2. Binary search 3. Insertion sort 4. Selection sort 5. Bubble sort 6. Quick sort 7. Merge sort 8. Heap sort For each experiment, the aim, input, and output is provided. The inputs include code implementations of the sorting algorithms and sample test arrays. The outputs show the results of applying the algorithms to the test arrays.

Uploaded by

slasherftw27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

INDEX

S. Page
Experiment Date Remark
No. No.

1 Implementa on of Linear Search

2 Implementa on of Binary Search

3 Implementa on Of Inser on Sort

4 Implementa on of Selec on Sort

5 Implementa on of Bubble Sort

6 Implementa on of Quick Sort

7 Implementa on of Merge Sort

8 Implementa on of Heap Sort


EXPERIMENT 1
Aim:
Implementation Linear Search
INPUT:
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1

# Driver code
arr = [2, 3, 4, 10, 40]
x = 10

# Function call
result = linear_search(arr, x)

if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")

OUTPUT:
Element is present at index 3
EXPERIMENT 2
Aim:
Implementation Binary Search

INPUT:

def binary_search(array, target):

low = 0

high = len(array) - 1

while low <= high:

mid = (low + high) // 2

if array[mid] == target:

return mid

elif array[mid] < target:

low = mid + 1

else:

high = mid - 1

return -1

array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

target = 7

result = binary_search(array, target)

if result != -1:

print("The target element is present at index", str(result))

else:

print("The target element is not present in the array")

OUTPUT:

The target element is present at index 3


EXPEIMENT 3
Aim:
Implementation of insertion sort

INPUT:
def insertion_sort(arr):
for i in range(1, len(arr)):
current_item = arr[i]
j=i-1
while j >= 0 and arr[j] > current_item:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = current_item
return arr

arr = [5, 3, 2, 1, 4]
print(insertion_sort(arr))

OUTPUT:

[1, 2, 3, 4, 5]
EXPERIMENT 4
Aim:
Implementation of selection sort

INPUT:
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j

arr[i], arr[min_index] = arr[min_index], arr[i]

return arr

arr = [64, 34, 25, 12, 22, 11, 90]


print("Unsorted Array")
print(arr)

print("Sorted Array in Ascending Order:")


print(selection_sort(arr))

OUTPUT:

Unsorted Array

[64, 34, 25, 12, 22, 11, 90]

Sorted Array in Ascending Order:

[11, 12, 22, 25, 34, 64, 90]


EXPRIMENT 5
Aim:
Implementation of bubble sort

INPUT:
import os

def bubble_sort(arr):
"""Sorts the given array in ascending order using the bubble sort algorithm. Args: arr: The array to be sorted.
Returns: None. The given array is sorted in-place. """

for i in range(len(arr) - 1):


for j in range(len(arr) - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

if __name__ == "__main__":
arr = [5, 3, 2, 1, 4]
bubble_sort(arr)
print(arr)

OUTPUT:

[1, 2, 3, 4, 5]
EXPERIMENT 6
Aim:
Implementation of quick sort

INPUT:

def quicksort(arr, lo, hi):

if lo < hi:

p = partition(arr, lo, hi)

quicksort(arr, lo, p-1)

quicksort(arr, p+1, hi)

def partition(arr, lo, hi):

pivot = arr[hi]

i = lo - 1

for j in range(lo, hi):

if arr[j] <= pivot:

i += 1

arr[i], arr[j] = arr[j], arr[i]

arr[i+1], arr[hi] = arr[hi], arr[i+1]

return i+1

arr = [10, 7, 8, 9, 1, 5]

n = len(arr)

quicksort(arr, 0, n-1)

print("Sorted array:", arr)

OUTPUT:

Sorted array: [1, 5, 7, 8, 9, 10]


EXPERIMENT 7
Aim:
Implementation of Merge sort

INPUT:

def mergeSort(arr):

if len(arr) > 1:

mid = len(arr)//2

sub_array1 = arr[:mid]

sub_array2 = arr[mid:

mergeSort(sub_array1)

mergeSort(sub_array2)

i=j=k=0

while i < len(sub_array1) and j < len(sub_array2):

if sub_array1[i] < sub_array2[j]:

arr[k] = sub_array1[i]

i += 1

else:

arr[k] = sub_array2[j]

j += 1

k += 1

while i < len(sub_array1):

arr[k] = sub_array1[i]

i += 1

k += 1

while j < len(sub_array2):

arr[k] = sub_array2[j]
j += 1

k += 1

arr = [10, 9, 2, 4, 6, 13]

mergeSort(arr)

print(arr)

OUTPUT:

[2, 4, 6, 9, 10, 13]


EXPERIMENT 8
Aim:
Implementation of Heap sort

INPUT:

def heapify(arr, n, i):

largest = i

l=2*i+1

r=2*i+2

if l < n and arr[i] < arr[l]:

largest = l

if r < n and arr[largest] < arr[r]:

largest = r

if largest != i:

(arr[i], arr[largest]) = (arr[largest], arr[i])

heapify(arr, n, largest)

def heapSort(arr):

n = len(arr)

for i in range(n // 2, -1, -1):

heapify(arr, n, i)

for i in range(n - 1, 0, -1):

(arr[i], arr[0]) = (arr[0], arr[i])

heapify(arr, i, 0)

arr = [12, 11, 13, 5, 6, 7, ]

heapSort(arr)

n = len(arr)

print('Sorted array is')


for i in range(n):

print(arr[i])

OUTPUT:

Sorted array is

11

12

13

You might also like