Algorithm (algorithms) version 4th Practice 2.2.11 (3)

Source: Internet
Author: User
Tags comparable

Key code implementations:

 Public Static voidsort (comparable[] input) {intN =input.length; Aux= Input.clone ();//must be clone (the same as input)//stdout.println ("Input:" + input + "aux:" + aux);//For TestSort (aux, input, 0, N-1); }        //Take input from source, sort it and put output in destination//source and destination are the same at the beginning    Private Static voidSort (comparable[] source, comparable[] destination,intLointhi) {                if(Lo >= hi)//just one entry in array            return; intMid = lo + (Hi-lo)/2; Sort (destination, source, Lo, mid);//First get input from destination, sort it and put output in sourceSort (destination, source, mid+1, HI); Merge (source, Destination, lo, Mid, hi);//merge sorted source to destination            }        Private Static voidMerge (comparable[] source, comparable[] destination,intLointMidinthi) {                inti =Lo; intj = Mid + 1;  for(intK = lo; K <= Hi; k++) {            if(I >mid) Destination[k]= source[j++]; Else if(J >hi) destination[k]= source[i++]; Else if(Less (Source[j], source[i])) Destination[k]= source[j++]; ElseDestination[k]= source[i++]; }//stdout.println ("Source:" + source + "Destination:" + destination);//stdout.printf ("merge (source, destination,%4d,%4d,%4d)", Lo, Mid, hi);//Show (destination);//For Test            }

Test Case:

 PackageCom.qiusongde;Importedu.princeton.cs.algs4.In;ImportEdu.princeton.cs.algs4.StdOut; Public classmergeavoidcopy {Private Staticcomparable[] aux;  Public Static voidsort (comparable[] input) {intN =input.length; Aux= Input.clone ();//must be clone (the same as input)//stdout.println ("Input:" + input + "aux:" + aux);//For TestSort (aux, input, 0, N-1); }        //Take input from source, sort it and put output in destination//source and destination are the same in start    Private Static voidSort (comparable[] source, comparable[] destination,intLointhi) {                if(Lo >= hi)//just one entry in array            return; intMid = lo + (Hi-lo)/2; Sort (destination, source, Lo, mid);//First get input from destination, sort it and put output in sourceSort (destination, source, mid+1, HI); Merge (source, Destination, lo, Mid, hi);//merge sorted source to destination            }        Private Static voidMerge (comparable[] source, comparable[] destination,intLointMidinthi) {                inti =Lo; intj = Mid + 1;  for(intK = lo; K <= Hi; k++) {            if(I >mid) Destination[k]= source[j++]; Else if(J >hi) destination[k]= source[i++]; Else if(Less (Source[j], source[i])) Destination[k]= source[j++]; ElseDestination[k]= source[i++]; }//stdout.println ("Source:" + source + "Destination:" + destination);//stdout.printf ("merge (source, destination,%4d,%4d,%4d)", Lo, Mid, hi);//Show (destination);//For Test            }        Private Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) < 0; }        Private Static voidShow (comparable[] a) {//print the array, on a single line.         for(inti = 0; i < a.length; i++) {stdout.print (A[i]+ " ");            } stdout.println (); }         Public Static Booleanissorted (comparable[] a) { for(inti = 1; i < a.length; i++) {            if(Less (A[i], a[i-1]))                return false; }                return true; }         Public Static voidMain (string[] args) {//Read strings from standard input, sort them, and print.string[] Input =in.readstrings ();//Show (input);//For Testsort (input); assertissorted (input);//Show (input);//For Test            }}

Test results:

m e r G E S O r T e X A m P L e input:[ljava.lang.string;@1b6d3586 Aux:[ljava.lang.string;@4554617csource:[ljava.lang. string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (source, destination,0, 0, 1) e M r G e S O r T E X A M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,2, 2, 3) e M G r e S O r T E X A M P L e source:[ljava.lang.string;@4554617c destination:[ljava.lang.string;@1b6d3586merge (sou RCE, Destination,0, 1, 3) e G M r e S O r T E X A M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,4, 4, 5) e M G r e S O r T E X A M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,6, 6, 7) e M G r e S O r T E X A M P L e source:[ljava.lang.string;@4554617c destination:[ljava.lang.string;@1b6d3586merge (sou RCE, Destination,4, 5, 7) e G M r e O r S T e X A M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,0, 3, 7) e e G M O r r S T e X A M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,8, 8, 9) e e G M O r r S E T X A M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,10, 10, 11) e e G M O r r S E T A X M P L e source:[ljava.lang.string;@4554617c destination:[ljava.lang.string;@1b6d3586merge (sou RCE, Destination,8, 9, 11) e G M r e O r S A e T X M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,12, 12, 13) e e G M O r r S E T A X M P L e source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,14, 14, 15) e e G M O r r S E T A X M P E L source:[ljava.lang.string;@4554617c destination:[ljava.lang.string;@1b6d3586merge (sou RCE, Destination,12, 13, 15) e G m r e O r S A E T X E L M P source:[ljava.lang.string;@1b6d3586 destination:[ljava.lang.string;@4554617cmerge (sou RCE, Destination,8, 11, 15) e e G m O r r S A e e L M P T X source:[ljava.lang.string;@4554617c destination:[ljava.lang.string;@1b6d3586merge (sou RCE, Destination,0, 7, 15) A e e e g l m m O p r r S T x A e e e g L m m O p r r S t x

Performance comparison:

For 20000random doubles 3333 3.0s

Algorithm (algorithms) version 4th Practice 2.2.11 (3)

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.