Chapter 2 algorithm basic questions 2-4 (Reverse Order)

Source: Internet
Author: User

1 package chap02; 2 3 import static org. junit. assert. *; 4 5 import java. util. arrays; 6 7 import org. junit. test; 8 9 public class ques2_4 {10/** 11 * reverse order, print all reverse pairs in a sequence and output 12*13 * @ author xiaojintao 14*15 */16 static void printReverseOrder (int [] n) {17 int I = 0; 18 int j; 19 while (I <n. length-1) {20 for (j = I + 1; j <n. length; j ++) {21 if (n [I]> n [j]) {22 System. out. println ("<" + n [I] + "," + n [j] + ">"); 23} 24} 25 I ++; 26} 27} 28 29 @ Test 30 public void testName () throws Exception {31 int [] a = {2}; 32 printReverseOrder (); 33} 34 35/** 36 * modify the merge order, print all reverse logarithm 37*38 * @ param n 39 */40 static void printReverseByMergeSort (int [] n) {41 int start = 0; 42 int end = n. length; 43 mergeSort (n, start, end); 44; 45} 46 47 @ Test 48 public void test () throws Exception {49 int [] a = {2, 3, 8, 6, 1, 5, 4, 0}; 50 printReverseByMergeSort (); 51} 52 53/** 54 * the modified Merge Sorting Algorithm is used to output 55*56 * @ param a 57 * @ return 58 */59 protected static void mergeSort (int [], int start, int end) {60 if (start <end-1) {61 int mid = (start + end)/2; 62 mergeSort (a, start, mid ); 63 mergeSort (a, mid, end); 64 merge (a, start, mid, end ); 65} 66} 67 68/** 69 * output reverse order to 70*71 * @ param a 72 * @ param B 73 * @ return 74 */75 protected static void merge (int [] n, int start, int mid, int end) {76 int [] l = Arrays. copyOfRange (n, start, mid); 77 int [] r = Arrays. copyOfRange (n, mid, end); 78 int I = 0; 79 int j = 0; // j <mid-start 80 int k = 0; // k <end-mid 81 while (I <end-start) {82 if (j <mid-start & k <end-mid) {83 if (l [j] <r [k]) {84 n [I + start] = l [j]; 85 j ++; 86} else {87 System. out. println ("<" + l [j] + "," + r [k] + ">"); 88 n [I + start] = r [k]; 89 k ++; 90} 91} else if (k <end-mid) {92 n [I + start] = r [k]; 93 k ++; 94} else if (j <mid-start) {95 n [I + start] = l [j]; 96 j ++; 97} 98 I ++; 99} 100} 101}

 

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.