Algorithm (algorithms) version 4th practice 2.2.11 (2)

Source: Internet
Author: User
Tags comparable

Key Code:

    Private Static voidSort (comparable[] input,intLointHi) {                if(lo >= Hi)//just one entry in array            return; intMid = lo + (HI-LO)/2;        Sort (input, lo, mid); Sort (input, Mid+1, hi); if(!less (input[mid+1],input[mid]))//input[mid+1] >= input[mid]            return;            Merge (input, lo, mid, hi); }

Overall:

 packagecom.qiusongde;Importedu.princeton.cs.algs4.In;Importedu.princeton.cs.algs4.StdOut; public classMergeskipmerge {Private Staticcomparable[] aux;  public Static voidsort (comparable[] Input) {intN =input.length; Aux=Newcomparable[n]; Sort (input,0, N-1); }        Private Static voidSort (comparable[] input,intLointHi) {                if(lo >= Hi)//just one entry in array            return; intMid = lo + (HI-LO)/2;        Sort (input, lo, mid); Sort (input, Mid+1, hi); if(!less (input[mid+1],input[mid]))//input[mid+1] >= input[mid]            return;            Merge (input, lo, mid, hi); }        Private Static voidMerge (comparable[] input,intLointMidintHi) {                //copy input[lo,hi] to aux[lo,hi]         for(inti = lo; I <= hi; i++) {aux[i]=input[i]; }                inti =lo; intj = Mid + 1;  for(intK = lo; K <= hi; k++) {            if(i >Mid) input[k]= aux[j++]; Else if(j >Hi) input[k]= aux[i++]; Else if(less (aux[j], aux[i])) input[k]= aux[j++]; Elseinput[k]= aux[i++]; } stdout.printf ("merge (input,%4d,%4d,%4d)", lo, mid, hi); Show (input);//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 merge (input,0, 0, 1) e M r G e S O r T E X A M P L e merge (input,2, 2, 3) e M G r e S O r T E X A M P L e merge (input,0, 1, 3) e G M r e S O r T E X A M P L e merge (input,4, 5, 7) e G M r e O r S T e X A M P L e merge (input,0, 3, 7) e e G M O r r S T e X A M P L e merge (input,8, 8, 9) e e G M O r r S E T X A M P L e merge (input,10, 10, 11) e e G M O r r S E T A X M P L e merge (input,8, 9, 11) e e G M O r r S A e T X M P L e merge (input,14, 14, 15) e e G M O r r S A e T X M P E L merge (input,12, 13, 15) e e G M O r r S A e T X E L M P merge (input,8, 11, 15) e e G m O r r S A e e L M P T X merge (input,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 20000 random doubles3.6s mergefasterm are 3.3s Mergeuseinsert are 3.2s mergeskipmerge is 3.4s

Algorithm (algorithms) version 4th practice 2.2.11 (2)

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.