Python Bubble Sort using Array Module

By | March 20, 2020

Python Bubble Sort using Array Module

# Write a Python program
# to input n numbers in
# Python array and sort them using
# bubble sort
# Perfect Python Programming Tutorials
# Author : www.EasyCodebook.com (c)

import array
 
def bubbleSort(arr):
    n = len(arr)
 
    for i in range(n):
 
        for j in range(0, n-i-1):
 
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]
 

# main
# Define array of integer numbers
a = array.array('i', [])

n = int(input('Enter  size of Array='))
# input n numbers in arry
for i in range(n):
    item= int(input("Enter number in array:"))
    a.append(item)

# call bubbleSort() function for sorting array
bubbleSort(a)

# print sorted array
print ("Sorted array is:")
for i in a:
    print (i, '\t')

Output

  Enter  size of Array=5
Enter number in array:67
Enter number in array:345
Enter number in array:6
Enter number in array:-1
Enter number in array:916
Sorted array is:
-1 	
6 	
67 	
345 	
916 	

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *