Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Bubble sort in Python:

Bubble sort is a simple and popular sorting algorithm that is used to sort a list of elements in ascending or descending order. It is an iterative algorithm that works by repeatedly swapping adjacent elements in a list that are in the wrong order until the list is sorted. In this blog post, we will discuss the implementation of the Bubble Sort in Python.

Algorithm of Bubble sort in Python:

The Bubble Sort Algorithm works as follows:

  1. Start with the first element in the list.
  2. Compare the first two elements. If the first element is greater than the second element, swap them.
  3. Move to the next pair of elements, compare them, and swap if necessary.
  4. Continue this process until the end of the list is reached.
  5. Repeat the above steps for each element in the list, except the last one.
  6. Repeat the entire process until the list is sorted.

Python Implementation

Now let’s take a look at the implementation of the Bubble sort algorithm in Python.

def bubble_sort(arr):
    n = len(arr)
    # Traverse through all elements in the array
    for i in range(n):
        # Last i elements are already sorted
        for j in range(0, n-i-1):
            # Swap if the element found is greater than the next element
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]

In the above implementation, we have defined a function bubble_sort that takes an array as its argument. We first get the length of the array and then iterate through each element of the array using a for loop.

Within the for loop, we use another for loop to iterate through all the elements of the array except the last i elements. This is because the last i elements are already sorted. Within the inner for loop, we check if the current element is greater than the next element. If it is, we swap them.

The above code will sort the array in ascending order. If you want to sort the array in descending order, you can modify the comparison condition to check if the current element is less than the next element.

def bubble_sort(arr):
    n = len(arr)
    # Traverse through all elements in the array
    for i in range(n):
        # Last i elements are already sorted
        for j in range(0, n-i-1):
            # Swap if the element found is less than the next element
            if arr[j] 

Now let’s test our implementation using a sample array.

def bubble_sort(arr):
    n = len(arr)
    # Traverse through all elements in the array
    for i in range(n):
        # Last i elements are already sorted
        for j in range(0, n-i-1):
            # Swap if the element found is less than the next element
            if arr[j] 

Sorted array: [11, 12, 22, 25, 34, 64, 90]

Conclusion:

In this blog post, we discussed the Bubble sort algorithm and its implementation in Python. Bubble sort is a simple sorting algorithm that works well for small lists of elements. However, it is not very efficient for larger lists and is generally not used in practice. Nonetheless, understanding the Bubble sort algorithm is important for learning more advanced sorting algorithms.

The post Bubble sort in Python: appeared first on Artificial Intelligence.



This post first appeared on Learn Python, please read the originial post: here

Share the post

Bubble sort in Python:

×

Subscribe to Learn Python

Get updates delivered right to your inbox!

Thank you for your subscription

×