Java-Divide and conquer algorithm

Source: Internet
Author: User

Principle of divide-and-conquer algorithm

Divide-and-conquer algorithm is to divide a problem of size n into K small sub-problem, these sub-problems are independent and the same nature as the original problem, find out the solution of the sub-problem, can get the solution of the original problem

Pseudo-code implementation of divide-and-conquer algorithm

Merge algorithm Merge

1 MERGE (A, p, Q, R)2N1←q-p + 13N2←r-Q4Create arrays L[1‥n1 + 1] and R[1‥N2 + 1]5     forI←1to N16          DoL[i]←a[p + i-1]7     forJ←1To N28          DoR[j]←a[q +J]9L[N1 + 1]←∞TenR[N2 + 1]←∞ OneI←1 AJ←1 -    forK←p to R -         Do ifL[i]≤r[j] the Then A[k]←l[i] -I←i + 1 -              ElseA[k]←r[j] -J←j + 1

Divide and conquer algorithm: including "Divide and Conquer" and "merge"

1 merge-SORT (A, P, r)2  if p < r3then    Q←┕ (P + r)/2  ┙4         merge-SORT (A, p, q)5         Merge-sort (A, q + 1, R)6         MERGE (A, p, Q, R)

Third, the Java code implementation of the divide-and-conquer algorithm

1 Importjava.util.Arrays;2 ImportJava.util.Comparator;3 4 5  Public classMergeSort {6 7     /**8 * Define Merge Method Merge, MergeSort method9 * Implemented with generic methodsTen      */ One      Public Static<T>voidMerge (t[] T,intPintQintR, Comparator<?SuperT>c) A     { -T[] left =arrays.copyofrange (t, p, q); -T[] Right =Arrays.copyofrange (t, Q, R); the          -         intIndexleft = 0; -         intIndexright = 0; -          +          for(inti = P; I < R; i++) -         { +             //Note: Here only look at the algorithm, there is no tube termination condition, otherwise the value of Indexleft constantly go up, will certainly cross the border, because the whole program is from P to R, and A             //Indexleft and Indexright not necessarily which end first, the first end of the words can not be compared, you need to do a deal with the rest ...  at             //It means left to the head. -             if(Indexleft >=left.length) -             { -                  Break; -             } -             //It means right's head. in             if(Indexright >=right.length) -             { toSystem.arraycopy (left, indexleft, T, I, left.length-indexleft);  +                  Break; -             } the             if(C.compare (Left[indexleft], Right[indexright]) < 0) *             { $T[i] =Left[indexleft];Panax Notoginsengindexleft++; -             } the             Else +             { AT[i] =Right[indexright]; theindexright++; +             } -         } $     } $      -      Public Static<T>voidMergeSort (t[] T,intPintR, Comparator<?SuperT>c) -     { the         if(P+1 <R) -         {Wuyi             intQ = (p + r)/2; the mergesort (t, p, Q, c); - mergesort (t, Q, R, c); Wu merge (t, p, Q, R, c); -         } About     } $      -      Public Static<T>voidMergeSort (t[] T, comparator<?SuperT>c) -     { -MergeSort (T, 0, T.length, c); A     } +      the     /** -      * @paramargs $      */ the      Public Static voidMain (string[] args) { the         //TODO auto-generated Method Stub theinteger[] INTs =NewInteger[]{2, 0, 5, 23, 1, 4, 8, 56, 19}; theMergeSort (INTs,NewComparator<integer>(){ -              Public intCompare (integer O1, integer o2) { in                 returnO1-O2; the             } the         }); About          the          for(inti = 0; i < ints.length; i++) the         { theSystem.out.print (Ints[i] + ""); +         } - System.out.println (); the     }Bayi  the}

Iv. Analysis of Complexity

The time complexity of the merged part is O (N)

Java-Divide and conquer algorithm

Related Article

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.