Merge sort algorithm to sort the list

class Solution:
     DEF merge_sort (Self, alist): 
        n- = len (alist)   # calculating the length of the array 
        IF n-<=. 1 :
             return alist 
        MID = n-2 // 
        left_arr = self.merge_sort (alist [: MID])   # demolition left half array 
        right_arr = self.merge_sort (alist [MID:])   # array right split 
        left_point, right_point = 0, 0 
        Result = []   # obtain the final array of 
        the while left_point <len (left_arr) and right_point < len (right_arr):
             IF left_arr[left_point] < right_arr[right_point]:
                result.append(left_arr[left_point])
                left_point += 1
            else:
                result.append(right_arr[right_point])
                right_point += 1

        result += left_arr[left_point:]
        result += right_arr[right_point:]
        return result


if __name__ == '__main__':
    alist = [1, 8, 6, 2, 5, 4, 8, 3, 9]
    s = Solution()
    res = s.merge_sort(alist)
    print(res)

Guess you like

Origin www.cnblogs.com/chenshifang/p/12071288.html