On the third day of the algorithm refresher heap sort and bucket sort

Today took part in the written test, emmmmmmm, continued hard efforts.

Good Habit: clear thinking can make the code rapid prototyping. Appropriate comment to make code more readable possess. Prepare the right tools to do a chart to explain or action figure.

1, bucket sort

"" "
Ideas: This is an array of integers, to find the maximum value, setting (max + 1) th empty bucket to start running the original array corresponding tub (index = num) was then removed from the bucket.
" " "

def bucket_sort_func(arr):
    if len(arr) < 2:
        return arr
    max_arr = max(arr)
    # 放空桶
    bucket = [0 for _ in range(max_arr+1)]
    for num in arr:
        bucket[num] += 1
    sort_arr = []
    for i in range(len(bucket)):
        if bucket[i] != 0:
            for ele in range(bucket[i]):
                sort_arr.append(i)
    return sort_arr

# 放回原数组
def bucket_sort(arr):
    res = bucket_sort_func(arr)
    for i in range(len(arr)):
        arr[i] = res[i]

2, heapsort

"" "
Ideas: first understand what is the big heap root, a root process large pile of construction, the repair process a large root heap
'" "

def heap_sort(arr):
    if len(arr) < 2:
        return arr
    for i in range(len(arr)):
        heap_insert(arr, i)
    size = len(arr)
    size -= 1
    arr[0], arr[size] = arr[size], arr[0]

    while size > 0:
        heapify(arr, 0, size)
        size -= 1
        arr[0], arr[size] = arr[size], arr[0]


def heap_insert(arr, index):
    # 构造大根堆
    # 出现一个BUG,最后一个数没有排好,问题见以下描述。必须要int,整除不可以!
    # index = 0
    # a = int((index - 1) / 2)
    # b = (index - 1) // 2
    # print(a, b)   打印结果 a=0,b=-1
    # while arr[index] > arr[((index-1)//2)]:
    #     arr[index], arr[((index-1)//2)] = arr[((index-1)//2)], arr[index]
    #     index = ((index-1)//2)
    while arr[index] > arr[int((index-1)/2)]:
        arr[index], arr[int((index-1)/2)] = arr[int((index-1)/2)], arr[index]
        index = int((index-1)/2)


def heapify(arr, index, size):
    # 调节大根堆
    left = index*2 + 1
    while left < size:
        if (left+1) < size and arr[left+1] > arr[left]:
            largest = left + 1
        else:
            largest = left
        if arr[largest] > arr[index]:
            largest = largest
        else:
            largest = index
        if largest == index:
            break
        arr[largest], arr[index] = arr[index], arr[largest]
        index = largest
        left = index*2 + 1

3, bucket sort expand

A Title: Given an array of integers, if required, after sorting, the difference between two adjacent maximum number of required time complexity O (N), and the request can not be compared with a non-sort based.
"" "
Ideas: The max and min discharge empty bucket, the bucket which can include determining whether the Boolean value is empty, put to the minimum, maximum, to start running into the array, updating the parameters inside the tub
" ""

# 待后续补充

Topic two: find the median of a given array.
"" "
Idea: a large root heap, a little heap root, to maintain a
balance! " ""

# 待后续补充

Guess you like

Origin www.cnblogs.com/burry/p/12664076.html