Chapter 2 algorithm basics Question 2-1

Source: Internet
Author: User

Package chap02; import static org. junit. assert. *; import java. util. arrays; import java. util. random; import org. junit. test; /***** insert and sort small arrays in Merge Sorting ** @ author xiaojintao **/public class ques2_1 {/*** Merge Sorting Algorithm ** @ param *@ return */static void mergeSortWithInsertSort (int [], int start, int end, int k) {if (start <end-1) {insertSort (a, start, end); System. out. println ("diaoyongle");} int mid = (start + end)/2; mergeSortWithInsertSort (a, start, mid, k); mergeSortWithInsertSort (a, mid, end, k); merge (a, start, mid, end );}} /*** merge two sorted sequences ** @ param a * @ param B * @ return */protected static void merge (int [] n, int start, int mid, int end) {int [] l = Arrays. copyOfRange (n, start, mid); int [] r = Arrays. copyOfRange (n, mid, end); int I = 0; int j = 0; // j <mid-start int k = 0; // k <end-mid while (I <end-start) {if (j <mid-start & k <end-mid) {if (l [j] <r [k]) {n [I + start] = l [j]; j ++ ;} else {n [I + start] = r [k]; k ++ ;}} else if (k <end-mid) {n [I + start] = r [k]; k ++;} else if (j <mid-start) {n [I + start] = l [j]; j ++;} I ++;} // System. out. println (Arrays. toString (n);}/*** Insert the Sorting Algorithm, from start to end in the sequence (excluding end) insert sequence between start <end ** @ param n * @ return */static void insertSort (int [] n, int start, int end) {for (int j = start + 1; j <end; j ++) {int k = n [j]; int I = j-1; while (n [I]> k) {n [I + 1] = n [I]; I --; if (I = start-1) break ;} n [I + 1] = k ;}}}

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.