Scala Merge Sort parsing

Source: Internet
Author: User

First, the source code

def Msort[t] (Xs:list[t]) (LT: (t,t) = Boolen): list[t]={val n = xs.length  /2if (  n = = 0) xselse{def merge (xs: List[t],ys:list[t]): list[t] = (XS, ys) match{case (Nil, ys) = Yscase (xs, Nil) and Xscase (x:: xs1,y:: ys1) =>if (LT ( x, y) x::merge (xs,ys1) Else y:: Merge (XS,YS1)}val (fst,snd) = xs Splitat nmerge (Msort (FST) (LT), Msort (snd) (LT))}}

Source code from Coursera "Scala functional Programming Principle"

Where the second parameter of the Msort function is--lt: (t,t) and Boolen, it is a custom predicate comparison method, which is used to compare the size of the object of type T and is not discussed here.

Ii. examples

List element: ls:list[int]={7,4,15,43,9}

Msort ({7,4,15,43,9}) Merge (Msort ({7,4,15}), Msort ({43,9})) for Msort ({7,4,15}) for recursive =>merge (Msort ({7,4}), Msort ({15} ) for Msort ({7,4}) recursive =>merge (Msort ({7}), Msort ({4})) =>merge (7,4) for Msort ({43,9}) for recursive =>merge (Msort ({43}), Msort ({9})) =>merge (43,9) Merge (merge (Msort ({7,4}), Msort ({{)}), merge (Msort ({}), Msort ({9}))) Merge (merge ( Merge (Msort ({7}), Msort ({4})), merge (43,9)) Merge (merge (merge (7,4), +), merge (43,9))//This is a series of split processes


Diagram of the split:


The above is a split process, followed by a 22 merge of the list:


After each merge, an ordered list is formed, so the problem is turned into a sequence of two ordered lists. Review the merge function, which borrows msort to decompose the parameters. That is to say, the two list parameters of the merge function must be ordered- in the merge function, it is the process of merging two lists with sequential tables.


Three, the principle

1. Split the list to be sorted into a list containing a single element

2. The split list element list 22 is merged and sorted until it is merged into a single list.


Iv. input and output of recursive functions

1. Msort function: Pass in a list to be sorted and output a sorted list.

2.merge function: Pass in two ordered lists and output an ordered list.


V. Interpretation of recursion

The recursion that appears in the program has these places:

The parameters of the 1.merge function use Msort to perform a split recursive operation.

2.merge sorts two parameters using the merge function. recursion Here is more like a function of reuse--the ability to merge two sequential tables.

Recursive exit Criteria:

1.msort function: The list to be sorted has only one element or is an empty list

2.merge function: One of two parameters is an empty list

Handling of recursive parts:

Msort using the Merge function to return an ordered list

Merge merges two ordered tables using the merge function itself

Obviously, recursion is handled by the merge function, which means that the merge can achieve the condition of its own exit recursion--Always call the merge function to form, with only one list in the argument. So, the Msort function here simply provides a split function- the auxiliary merge function, which folds a list of 50% two lists.

So far, the point is that the merge function merges two sequential table methods by extracting the smallest element and merging the remaining elements to reduce the recursive merge process for the merge function for {4,7,15} and {9,43}, such as:


This enables the process of sequencing two sequential tables. The summary of the merge sort is two points:

1. Use Msort to treat sorted lists for splitting until a list with a single element is formed.

2. Merge the two ordered tables using merge until a list is formed.

Copyright notice: Feel free to comment.

Scala Merge Sort parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.