Selection sort in Python

#----------------------------------- 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")



Join our team, where career growth knows no limits.

For More Details

Our internship program guarantees personalized attention for optimal career development in the tech industry.

+91 97879 68375

-> You should bring your own laptop during the internship

Who can apply?

Internships are open for students who: B.E / B.Tech / M.E / M.Tech / B.Sc / M.Sc / M.C.A / B.C.A / Diploma.

  • Ongoing Students Only
  • days, 5days/ 10days or Based on Student Preference
  • Limited 1 to 10 students per slot
  • Registration fees : 1000 INR
  • Slots are filling quickly only fewer slots are available