Bubble Sort

Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

When to use bubble sort:

?Bubble sort is easy to understand and implement, making it suitable for small datasets or educational purposes.

? It is also useful when the input list is mostly sorted or nearly sorted, as it performs relatively well in this case.

When not to use bubble sort:

?Bubble sort has a time complexity of O(n^2), making it inefficient for large datasets. For large datasets, other more efficient sorting algorithms like quicksort, mergesort, or heapsort should be used.

?It is not suitable for sorting large arrays or lists due to its poor performance compared to other sorting algorithms.

Bubble sort example in Python,

def bubble_sort(arr):
    n = len(arr)    
    for i in range(n):
        print('i = ' + str(i))
        for j in range(0, n-i-1):
            print('j = ' + str(j))
            # Traverse the array from 0 to 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]        

I hope you enjoyed this post. Thank you.

要查看或添加评论,请登录

Anjana Silva的更多文章

  • Measurable Software Engineering Best Practices vs. Software Development Life Cycle

    Measurable Software Engineering Best Practices vs. Software Development Life Cycle

    Software engineering is a wonderful ocean to swim in as long as you understand which direction to swim, which tide to…

  • Top 10 critical Windows Server 2008 vulnerabilities

    Top 10 critical Windows Server 2008 vulnerabilities

    Microsoft has officially ended their support for Windows 2008 server on January, 2020. However, there are still a…

  • Kubernetes Security Checklist

    Kubernetes Security Checklist

    The following list provides a basic list of Kubernetes security checklist. The following is not an exhaustive list, and…

  • Devin & You

    Devin & You

    As a programmer, whether you are experienced or not, are you worried about Devin taking over your job? The short answer…

    6 条评论
  • Service-based Architecture

    Service-based Architecture

    This is a continuation of my previous two articles related to software architecture. If you haven't read those yet…

  • Issue Board Simplified

    Issue Board Simplified

    Over the past few years, I have been working closely with a few software development teams and on several different…

  • Practical Multithreading

    Practical Multithreading

    Imagine a kitchen with multiple chefs. Each chef can work on preparing a variety of different dishes at the same time.

    2 条评论
  • Micro-frontend Architecture

    Micro-frontend Architecture

    This a continuation of my yesterday's post about microservices -https://www.linkedin.

    6 条评论
  • Achieving optimum scalability using microservices architecture

    Achieving optimum scalability using microservices architecture

    Microservices architecture contains highly specialised, independent, easily maintainable/scalable modules or services…

  • Sorting Algorithms

    Sorting Algorithms

    In programming, several sorting algorithms are commonly used, each with its own advantages and disadvantages depending…

社区洞察

其他会员也浏览了