Python implements Hill sorting

Hill sort

Shell Sort is a type of insertion sort. Also known as shrinking incremental sort, it is a more efficient and improved version of the direct insertion sort algorithm. Hill sort is an unstable sorting algorithm. This method is due to DL. Shell was named after it was proposed in 1959. Hill sorting is to group the records by a certain increment of the subscript, and use the direct insertion sorting algorithm to sort each group; as the increment gradually decreases, each group contains more and more keywords. When the increment is reduced to 1, The entire file is just grouped together, and the algorithm terminates.

Hill sort process

The basic idea of ​​Hill sort is: list the arrays in a table and insert the columns separately, repeat this process, but use longer columns each time (the step size is longer, the number of columns is less) to perform . Finally, the entire table has only one column. Converting the array to a table is to better understand the algorithm, which itself uses the array for sorting.

For example, given a set of numbers [ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ], if we start sorting with a step size of 5, we can Tables to better describe the algorithms, so they should look like this (the vertical elements are the steps):

explain and implement

'''
Divide the array [13, 14, 94, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10, 33] into multiple unordered arrays with a step size of 3. It is listed as follows:
13      82     94     45      25       33       |
  14      25     65      27      39             |
    94      59     23      73       10          |
                                               \ /
                                                .
Each step of the outer loop (for i in range(gap, n)) goes down,
The inner loop (for j in range(i, 0, -gap)) only performs insertion sorting of one data per unordered array


Optimal time complexity: varies according to the sequence of steps
Worst time complexity: O(n2)
Stability: Unstable
'''

def ShellSort(alist):
    '''Hill sort, an improved version of the insertion sort algorithm'''
    n = len (alist)
    # initial step size
    gap = n // 2

    # change the step size
    while gap > 0:
        # Instead of inserting each unordered array in turn, inserting each unordered array at the same time
        for i in range(gap, n):
            # gap+1, gap+2, gap+3, gap+4, ..., n-1
            # Insertion sort of a data in an unordered array
            for j in range(i, 0, -gap):
                if alist[j] < alist[j-gap]:
                    alist[j], alist[j-gap] = alist[j-gap], alist[j]
                else:
                    break

        # for i in range(gap, n):
        #     j = i
        # # Insertion sort
        #     while j >= gap and alist[j - gap] > alist[j]:
        #         alist[j - gap], alist[j] = alist[j], alist[j - gap]
        #         j -= gap

        gap //= 2


if __name__ == '__main__':
    alist = [13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10]
    print(alist)
    shell_sort = ShellSort(alist)
    print(alist)

operation result:

/home/longhui/Desktop/Data Structures and Algorithms/venv/bin/python /home/longhui/Desktop/Data Structures and Algorithms/venv/MyCode/6_sorting_and_searching/shell_sort.py
[13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10]
[10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94]

Process finished with exit code 0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325578073&siteId=291194637