Insertion Sort

Insertion Sort

Insertion sort is a simple sorting algorithm that builds the final sorted array one element at a time. It iterates through the input list, removing one element at a time and then finding the place where it belongs in the sorted list and inserting it there. It repeats this process until the whole list is sorted.

When to use insertion sort

? Insertion sort is efficient for small datasets or nearly sorted lists.

? It is an in-place sorting algorithm, meaning it doesn't require extra space, making it suitable for memory-constrained environments.

When not to use insertion sort

? Insertion sort has an average and worst-case 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 datasets with a large number of elements or when a faster sorting algorithm is required.

A simple insertion sort example in Python,

def insertion_sort(arr):
    # Traverse through 1 to len(arr)
    for i in range(1, len(arr)):
        key = arr[i]
        # Move elements of arr[0..i-1] that are greater than key
        # to one position ahead of their current position
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key

# Example usage:
arr = [64, 34, 25, 12, 22, 11, 90]
insertion_sort(arr)
print("Sorted array is:", arr)        

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…

社区洞察

其他会员也浏览了