githubEdit

Sorting

circle-info

A comparison sort cannot perform better than O(n log n) on average.

circle-info

Non-comparison sorts can have better performance in certain situations, but are often limited by the range of values in the input data.

Binary insertion sort

def insertion_sort(arr):
    """
    The time complexity is O(n^2), less efficient than advance sorting algorithms like
    quick soer and mergesort.
    """
    for i in range(1, len(arr)):
        key=arr[i]
        j=i-1
        while j>=0 and arr[j]>key:
            arr[j+1]=arr[j]
            j-=1
        arr[j+1]=key
    return arr
    
    
# implementation of binary insertion with bisect
def binary_insertion_sort(arr):
    """
    bisect moudle provides a simple and efficient way to perform binary search
    on sorted sequences. bisect_left help find the index at which a value
    should be inserted into a sorted sequence, while maintaining the order of the sequence.
    """
    for i in range(1, len(arr)):
        x=arr[i]
        j=bisect.bisect_left(arr,x,0,i)
        # this is an effectively shifts all the elements in the range [j,i-1] one
        # position to the right, to make room for the new element that will be 
        # inserted at index 'j'
        arr[j+1:i+1]=arr[j:i]
        arr[j]=x
    return arr

Heap sort

circle-info

Worst-case time complexity is O(n log n) and O(1) for space complexity, it an efficient sorting algortihn for large inputs.

Insertion sort

O(n^2) but it still useful for small input sizes

Merge sort

Quick sort

References

Last updated