Python Program for Bubble Sort

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this article, we will explore a Python program that implements the Bubble Sort algorithm, a simple sorting algorithm used to arrange elements in ascending order. Sorting is a fundamental operation in computer science and is essential for various applications. The program demonstrates the step-by-step process of sorting an array using the Bubble Sort technique.

Prerequisites

  • Fundamental Python Knowledge (Variables, Data Types, Loops, Syntax)
  • Familiarity with Basic Array Concepts (Declaration, Manipulation, Iteration)

Topic Explanation

Bubble Sort Algorithm

The program begins by taking user input for the limit of the array and then prompts the user to enter elements. It uses the Bubble Sort algorithm to iteratively compare adjacent elements and swap them if they are in the wrong order. Until the entire array is sorted, these steps are repeated. The article explains the logic behind the Bubble Sort algorithm and its step-by-step execution.

Furthermore, the article delves into the underlying logic behind the Bubble Sort algorithm and meticulously outlines its step-by-step execution. Readers gain a comprehensive understanding of how this sorting method operates, making it a valuable resource for learning about sorting algorithms and enhancing one’s problem-solving skills in Python.

Program Code:

# Program for Bubble sort in Python

# Import the array module to use arrays
import array as ar

# Create an empty integer array
myar = ar.array('i', [])

# Get the limit of the array from the user
n = int(input("Enter limit of array"))

# Ask the user to add components to the array.
print("Enter elements in array")
for i in range(n):
    x = int(input())
    myar.append(x)

# Perform Bubble Sort on the array
for i in range(n):
    for j in range(n - i - 1):
        # Compare adjacent elements and swap if they are in the wrong order
        if myar[j] > myar[j + 1]:
            temp = myar[j]
            myar[j] = myar[j + 1]
            myar[j + 1] = temp

# Print the sorted array
print("Sorted Array")
print(myar)

Output:

Enter limit of array5
Enter elements in array
3
1
9
5
2
Sorted Array
array(‘i’, [1, 2, 3, 5, 9])

Code Explanation:

1. Import array module to support dynamic arrays
2. Create an empty integer array
3. Take size of array input from user
4. Take array elements as input from user in a loop
5. Append each element to the array
6. Outer loop goes from 0 to n-1

  • Inner loop goes from 0 to n-1-i
  • Compare adjacent elements at index j and j+1
  • If element at j is greater than element at j+1
  • Swap the elements using a temporary variable

7. After inner loop completes one iteration, largest element reached at end
8. Print the sorted array

Summary

In summary, this Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array. Understanding sorting algorithms is fundamental in computer science and provides a solid foundation for more advanced algorithms and data structures. Proficiency in sorting techniques equips individuals to efficiently tackle computational challenges and forms the basis for exploring more advanced problem-solving strategies within the field of programming and computer science.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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