#----------------------------------- Selection sort using for loop ---------------------------
count =0
array = [] # array =[2,3,5,7,6,4,3,4,6,7] can store n no.of elements
n = int(input("Enter the no. of elements : ")) # total no. of elements in the list length of list
# getting inputs from user
print()
for i in range(n):
print(i+1,end=".")
nums = int(input("Enter the number : "))
array.append(nums)
for i in range(n):
min = i
for j in range(i + 1, n):
# select the minimum element in every iteration
if array[j] < array[min]:
min = j
# swapping the elements to sort the array
(array[i], array[min]) = (array[min], array[i])
count+=1
print('The array after sorting in selection sort is:',array)
print("iterated for ",count)
#----------------------------------- Selection sort using while loop -------------------------
count =0
array = []
n = int(input("Enter the no. of elements : ")) # total no. of elements in the list length of list
# getting inputs from user
print()
for l in range(n):
print(l+1,end=".")
nums = int(input("Enter the number : "))
array.append(nums)
i = 0
while i < n - 1:
# Find the minimum element in the remaining unsorted array
min = i
j = i + 1
while j < n:
if array[j] < array[min]:
min = j
j += 1
# with the first element Swap the found minimum element
array[i], array[min] = array[min], array[i]
# Move to the next element in the array
i += 1
count+=1
print("Array after selection sort : ",array)
print("Iterated for ",count, "times")
#----------------------------------- Selection sort without duplicate ------------------------
count =0
array = []
n = int(input("Enter the no. of elements : ")) # total no. of elements in the list length of list
# getting inputs from user
print()
for l in range(n):
print(l+1,end=".")
nums = int(input("Enter the number : "))
array.append(nums)
i = 0
while i < n - 1:
# Find the minimum element in the remaining unsorted array
min = i
j = i + 1
while j < n:
if array[j] == array[j+1]:
array.pop(i)
if array[j] < array[min]:
min_index = j
j += 1
# with the first element Swap the found minimum element
array[i], array[min] = array[min], array[i]
# Move to the next element in the array
i += 1
count+=1
print("Array after selection sort : ",array)
print("Iterated for ",count, "times")