python-data structure and algorithm-DAY_5-common sorting code implementation

1. Bubble sorting
As one of the common sorting methods, the principle of bubble sorting is very simple
. 1. Start from a set of data with a subscript index of 0, and compare it with the next bit of data each time. If it is larger, the position of the two data is exchanged, and the process is repeated for all elements in order to achieve a loop.
2. After the first cycle would put the end of the most maximum, so the overall cycle the next time, just repeat the above steps to the second n-2 position
3. repeated until the sorting is done
most
bad time complexity O (N²)
Optimal Time complexity O(N)

def bubble_sort(alist):
    for j in range(len(alist) - 1):
        count = 0
        for i in range(0, len(alist) - 1 - j):
            if alist[i] > alist[i + 1]:
                alist[i], alist[i + 1] = alist[i + 1], alist[i]
                count += 1
                if 0 == count:
                    return
    return alist


if __name__ == '__main__':
    a = [1, 3, 5, 7, 2]
    print(a)
    print(bubble_sort(a))

[1, 3, 5, 7, 2]
[1, 2, 3, 5, 7]

2. Select sorting
The principle of selecting sorting is as follows:
1. Starting from one end of the array, each cycle selects the smallest data in the current traversal and puts it into the leftmost end of the entire group of data
2. Repeat the above steps until the sorting is completed at the
worst time complexity And the optimal time complexity are both O(N²)

def select_sort(alist):
    n = len(alist)
    for j in range(n - 1):
        min_index = j
        for i in range(1 + j, n):
            if alist[min_index] > alist[i]:
                min_index = i
            alist[j], alist[min_index] = alist[min_index], alist[j]
    return alist


if __name__ == '__main__':
    a = [1, 3, 2, 7, 2]
    print(a)
    print(select_sort(a))


[1, 3, 2, 7, 2]
[1, 2, 2, 3, 7]

3. Insertion sorting method.
Into this principle
1. Starting from the second element from the left of an array, use this element to compare with the left element, if the value is smaller, exchange position with the comparison value. After changing the position, repeat the above steps until one cycle is completed
. 2. The starting position is moved one bit to the right, and the cycle is repeated until all the array contents are traversed.

Optimal time complexity O(N)
Worst time complexity O(N²)

def insert_sort(alist):
    n = len(alist)
    for j in range(1, n):
        i = j
        while i > 0:
            if alist[i] < alist[i - 1]:
                alist[i], alist[i - 1] = alist[i - 1], alist[i]
            i -= 1
    return alist


if __name__ == '__main__':
    a = [1, 3, 2, 7, 2]
    print(a)
    print(insert_sort(a))

[1, 3, 2, 7, 2]
[1, 2, 2, 3, 7]

4. Hill sorting
Hill sorting is a specialization of insertion sort, he will use the step size operation.
1. step 2 preferably one sub-data length (rounding), so that the position of the array in each section are arranged
2. continue to take a half step, until the step is 1, the end of the sort
most
bad time complexity O (N²)

def shell_sort(alist):
    n = len(alist)
    gap = n // 2
    while gap > 0:
        for j in range(gap, n):
            i = j
            while i > 0:

                if alist[i] < alist[i - gap]:
                    alist[i], alist[i - gap] = alist[i - gap], alist[i]
                i -= gap
        gap //= 2
    return alist


if __name__ == '__main__':
    a = [1, 9, 3, 5, 7, 2]
    print(a)
    print(shell_sort(a))


[1, 9, 3, 5, 7, 2]
[1, 2, 3, 5, 7, 9]

Guess you like

Origin blog.csdn.net/soulproficiency/article/details/104530961
Recommended