Algorithm Sedgewick Fourth Edition-1th chapter basic -2.1elementary Sortss-007 Merge sort (bottom-up)

Source: Internet
Author: User
Tags comparable

One

1.

2.

3.

Second, the Code

1  Packagealgorithms.mergesort22;2 3 Importalgorithms.util.StdIn;4 ImportAlgorithms.util.StdOut;5 6 /******************************************************************************7 * Compilation:javac Mergebu.java8 * Execution:java Mergebu < Input.txt9 * Dependencies:StdOut.java Stdin.javaTen * Data Files:Http://algs4.cs.princeton.edu/22mergesort/tiny.txt One  *                Http://algs4.cs.princeton.edu/22mergesort/words3.txt A  *    - * Sorts a sequence of strings from standard input using - * bottom-up mergesort. the  *    - *% more tiny.txt - * S O R T e X A M P L E -  * + *% java Mergebu < tiny.txt - * A e e L M O P R S T X [one string per line] +  *     A *% more words3.txt at * Bed bug dad Yes Zoo ... all bad yet -  *   - *% java Mergebu < words3.txt - * All bad bed bug dad ... yes yet zoo [one string per line] -  * -  ******************************************************************************/ in  - /** to * The <tt>MergeBU</tt> class provides static methods for sorting an + * array using bottom-up mergesort. - * <p> the * For additional documentation, see <a href= "http://algs4.cs.princeton.edu/21elementary">section 2.1</a> of * * <i>algorithms, 4th edition</i> by Robert Sedgewick and Kevin Wayne. $  *Panax Notoginseng  *  @authorRobert Sedgewick -  *  @authorKevin Wayne the  */ +  Public classMergebu { A  the     //This is class should not being instantiated. +     PrivateMergebu () {} -  $     //stably Merge A[lo. Mid] with A[mid+1..hi] using Aux[lo. Hi] $     Private Static voidMerge (comparable[] A, comparable[] aux,intLointMidinthi) { -  -         //copy to aux[] the          for(intK = lo; K <= Hi; k++) { -AUX[K] =A[k];Wuyi         } the  -         //merge back to a[] Wu         inti = lo, j = mid+1; -          for(intK = lo; K <= Hi; k++) { About             if(i > Mid) a[k] = aux[j++];//This copying is unneccessary $             Else if(J > Hi) a[k] = aux[i++]; -             Else if(Less (Aux[j], aux[i])) a[k] = aux[j++]; -             ElseA[k] = aux[i++]; -         } A  +     } the  -     /** $ * Rearranges the array in ascending order, using the natural order. the      * @parama The array to be sorted the      */ the      Public Static voidsort (comparable[] a) { the         intN =a.length; -comparable[] aux =NewComparable[n]; in          for(intn = 1; n < N; n = n+N) { the              for(inti = 0; i < n-n; i + = n+N) { the                 intLo =i; About                 intm = i+n-1; the                 inthi = Math.min (i+n+n-1, N-1); the merge (A, Aux, lo, M, hi); the             } +         } -         assertissorted (a); the     }Bayi  the   /*********************************************************************** the * Helper sorting functions. -     ***************************************************************************/ -      the     //Is v < w? the     Private Static BooleanLess (comparable V, comparable W) { the         returnV.compareto (W) < 0; the     } -  the    //Exchange A[i] and A[j] the     Private Static voidExch (object[] A,intIintj) { theObject swap =A[i];94A[i] =A[j]; theA[J] =swap; the     } the 98  About    /*************************************************************************** - * Check If array is sorted-useful for debugging.101     ***************************************************************************/102     Private Static Booleanissorted (comparable[] a) {103          for(inti = 1; i < a.length; i++)104             if(Less (A[i], a[i-1]))return false; the         return true;106     }107 108     //Print array to standard output109     Private Static voidShow (comparable[] a) { the          for(inti = 0; i < a.length; i++) {111 stdout.println (A[i]); the         }113     } the  the     /** the * Reads in a sequence of strings by standard input; bottom-up117 * mergesorts them, and prints them to standard output in ascending order.118      */119      Public Static voidMain (string[] args) { -String[] A =stdin.readallstrings ();121 Mergebu.sort (a);122 Show (a);123     }124}

Algorithm Sedgewick Fourth Edition-1th chapter basic -2.1elementary Sortss-007 Merge sort (bottom-up)

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.