Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- def e_cmp(element_1: int, element_2: int, descend: bool = False) -> bool:
- """
- Compare the value of the two input value.
- :rtype: bool
- :param element_1:
- :param element_2:
- :param descend:
- :return:
- """
- if descend:
- return element_1 > element_2
- else:
- return element_1 <= element_2
- def merge(list1: list, list2: list, descend: bool = False) -> list:
- """
- Sort 2 lists by merging in ascending (descend == True)
- or descending (descend == False) order.
- :rtype: list
- :param list1:
- :param list2:
- :param descend:
- :return:
- """
- iter1, iter2 = 0, 0
- result = list()
- while (iter1 < len(list1)) & (iter2 < len(list2)):
- if e_cmp(list1[iter1], list2[iter2], descend):
- result.append(list1[iter1])
- iter1 += 1
- else:
- result.append(list2[iter2])
- iter2 += 1
- if list1:
- result.extend(list1[iter1:])
- if list2:
- result.extend(list2[iter2:])
- return result
- def merge_split(origin_list: list, descend: bool = False) -> list:
- """
- Construct the correct call architecture of
- merge in ascending (descend == true)
- or descending (descend != 1) order.
- :rtype: list
- :param origin_list:
- :param descend:
- :return:
- """
- if len(origin_list) <= 1:
- return origin_list
- tmp_list = list()
- new_list = list()
- for e in origin_list:
- tmp_list.append(e)
- if len(tmp_list) >= 2:
- new_list.append(merge(tmp_list[0], tmp_list[1], descend))
- tmp_list = list()
- if tmp_list:
- new_list.append(merge(tmp_list[0], [], descend))
- return merge_split(new_list, descend)
- def merge_sort(origin_list: list, descend: bool = False) -> list:
- """
- Split the array into as many array that the original array
- contain then call the mergeSplit function.
- :rtype: list
- :param origin_list:
- :param descend:
- :return:
- """
- result = list()
- for e in origin_list:
- result.append([e])
- return merge_split(result, descend)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement