Bubble Sort in Python

#-------------------------------- Bubble Sort using for loop -----------------------------

count =0 # initializing count as 0
array = [] #initializing empty array
n = int(input("Enter the no. of elements : "))  # total no. of elements in the list 

# getting inputs from user

for i in range(n):
	print(i+1,end=".") 
	nums = int(input("Enter the number : ")) # gets the value one by one
	array.append(nums) # add element at the end of the array

for i in range(n):
		for j in range(0, n - i - 1):
			#  if the element found Swap the elements
			
			if array[j] > array[j + 1]:
				array[j], array[j + 1] = array[j + 1], array[j]
				count+=1
				
print("Sorted Array")
print(array)
print("iterated for ",count,"times")
#-------------------------------- Bubble Sort using while loop -----------------------------


count =0 # initializing count as 0
array = [] #initializing empty array
n = int(input("Enter the no. of elements : "))  # total no. of elements in the list 

# getting inputs from user

for m in range(n):
	print(m+1,end=".") 
	nums = int(input("Enter the number : ")) # gets the value one by one
	array.append(nums) # add element at the end of the array
i = 0
while (i < n):
        j = 0
        while (j < n - i - 1):

            #  if the element found is greater Swap
           
            if (array[j] > array[j + 1]):
                array[j], array[j + 1] = array[j + 1], array[j]
            j += 1
        i += 1
        count+=1
print("Sorted elements : ",array)
print("\nIterated for",count,"times")
#-------------------------------- Bubble Sort using without dupliate  ------------------------

count =0 # initializing count as 0
array = [] #initializing empty array
n = int(input("Enter the no. of elements : "))  # total no. of elements in the list 

# getting inputs from user

for m in range(n):
	print(m+1,end=".") 
	nums = int(input("Enter the number : ")) # gets the value one by one
	array.append(nums) # add element at the end of the array
i = 0
while (i < n):
        j = 0
        while (j < n - i - 1):
            
            if array[j] == array[j+1]:
                  array.pop(i)
                               
                               
            if ( array[j] > array[j + 1] ):
                array[j], array[j + 1] = array[j + 1], array[j]
            j += 1
        i += 1
        count+=1
print("Sorted elements : ",array)
print("\nIterated 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