Professional Documents
Culture Documents
Selection Sort
Selection Sort
import random
import matplotlib.pyplot as plt
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
for _ in range(trials):
n = int(input("Enter number of elements: "))
array = [random.randint(1, 100) for _ in range(n)] # Generate random elements
start = time.time()
selection_sort(array)
end = time.time()
time_taken = end - start
print("Sorted Array:")
print(array)
N.append(n)
CPU.append(round(time_taken * 1000000, 2))
print("N CPU")
for i in range(trials):
print(N[i], CPU[i])
plt.plot(N, CPU)
plt.scatter(N, CPU, color="red", marker="*", s=50)
plt.xlabel('Array size - N')
plt.ylabel('CPU processing time')
plt.title('selection sort time efficiency')
plt.show()