Recursive--Two-point lookup Method--merge sort

Source: Internet
Author: User

PS: What is recursive, binary lookup, merge sort.

Recursive ordering Everyone is not strange, recursive simple is that they do not achieve the purpose of the call itself at the same time, a large problem layer into a similar problem with the original solution, recursive need to have boundary conditions, recursive forward segment and recursive return segment. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.

Binary Lookup is also known as binary lookup (binary search), which is a highly efficient method of finding. However, the binary lookup requires that the linear table must take a sequential storage structure, and that the elements in the table are ordered alphabetically by keyword.

Merge Sort ( Merge-sort) is an efficient sorting algorithm based on merging operations, which is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.

If you want to know more can go to Baidu encyclopedia to check. Here is a simple example

1, two-point search method of thinking

The dichotomy is to find an array binary, and then binary until the data location is found, or there is no data location. For example, 1-100, you choose a value of 23, then the range is written (the index is similar to the wording)

The first binary was 1-50, 51-100, after finding 23<50, was in 1-50.

The second time is 1-25, 26-50, after looking for 23<25, is in 1-25.

..........

Conditions of Use
    • Must be ordered data
    • Ascending and descending start angle and end angle are written in the opposite direction
/** * Method Description: Binary Lookup method * **/public static int twoquerymethod (int[] data,int query) {int start=0;//start corner int end=data.length-1;//knot Beam angle int Moddle;while (true) {moddle= (End+start)/2;if (data[moddle] = = query) {return moddle;} Start Corner  >  last corner label   not found else if (Start > End) {return data.length;} else{//Middle value is greater than lookup value if (Data[moddle] > query) {end = moddle-1;} Else{start = Moddle + 1;}}}

The above is written in the usual while loop, with the following recursive notation.

2: Recursive---binary search method

Use recursion to cancel the use of a while loop

/** * Recursion replaces while loop *  * **///Descending lookup public static int Diguimethod (int[] data,int search,int start,int end) {//Get intermediate value corner mark int mo Ddle= (start+end)/2;if (data[moddle] = = Search) {return moddle;} else if (Start > End) {return data.length;} else{//below is the descending if (data[moddle]< search) {return diguimethod (data,search,start,moddle-1);} Else{return Diguimethod (data, search, moddle+1, end);}}} Search in ascending order public static int BinarySearch (int[] arr, int data, int beginindex, int endIndex) {int midindex = (Beginindex + Endi Ndex)/2;if (Data < Arr[beginindex] | | data > ARR[ENDINDEX] | | beginindex > EndIndex) {return-1;}  if (Data < Arr[midindex]) {return BinarySearch (arr, data, Beginindex, midIndex-1);} else if (Data > Arr[midindex]) {return BinarySearch (arr, data, Midindex + 1, endIndex);} else {return midindex;}}
Efficiency

Common binary search and recursive binary search are all O (LOGN) But the data shows the recursive binary search introduction but a little bit slower.

Extension--Divide and conquer algorithm

The basic idea of divide-and-conquer algorithm is to decompose a problem of size n into a small sub-problem of K, which is independent and the same nature as the original problem. The solution of the problem can be obtained by finding out the solution of the sub-problems. That is, a sub-goal to complete the program algorithm, simple problem can be achieved by dichotomy.

Divide and conquer algorithm--basic idea

When we solve some problems, because these problems to deal with a considerable amount of data, or the process is quite complex, so that the direct solution in time is quite long, or simply can not be directly obtained. For this kind of problem, we often first decompose it into several sub-problems, find out the solution of these sub-problems, then find the appropriate method, combine them into the solution of the whole problem. If these sub-problems are larger, difficult to solve, they can be divided into several smaller sub-problems, and so on, until the solution can be directly solved. This is the basic idea of the divide-and-conquer strategy.

3: Merge sort

Merge sort (Merge-sort) is an efficient sorting algorithm based on merging operation, which is a very typical application of divide-and-conquer method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.

Conditions for merging sorting, advantages of using
    • by two different ordered arrays, compared to each other by comparison of the size of the order
    • Divides an unordered array into n data, compares each data itself once, then compares and merges with the next array, and so on.
3.1: Two A, B different (ordered) arrays are merged into a C array, and the result C is orderly.
public static void Mergetwo (int[] arr1,int[] arr2,int[] mergearr) {int aindex=0,bindex=0,mindex=0;//two arrays have data when while ( Aindex < arr1.length && Bindex < arr2.length) {if (Arr1[aindex]<arr2[bindex]) {mergearr[mindex++] = arr1 [aindex++];} else{mergearr[mindex++] = arr2[bindex++];}} If the length of the two array is equal, the method below does not execute, and if a is longer than B, the first while is taken. Two arrays one without data while (Aindex < arr1.length) {mergearr[mindex++] = arr1[aindex++];} while (Bindex < arr2.length) {mergearr[mindex++]=arr2[bindex++];} Traversal result for (int i=0;i<mergearr.length;i++) {System.out.println (mergearr[i]);}}

Use

The main method uses int[] arr1={12,14,15,16};int[] arr2={8,22,56,90,100};int [] merge=new int[9];mergetwo (arr1, arr2, merge);
3.2: Merge algorithm--sort a without array
/** * An array is sorted internally *  * **/public static void Mergeone (int arr[],int startint,int stopint,int[] cArr) {//If the range is 1 then return directly. if (stopint==startint) {return;} Else{int middle= (Startint+stopint)/2;//began separating the arrays---dichotomy mergeone (arr, startint, Middle,carr); Mergeone (arr, middle+1, Stopint,carr); Mergetwosort (Arr,startint,middle,stopint,carr);}} public static void Mergetwosort (int arr[],int start,int mid,int end,int[] cArr) {int left=start;//left sequence start corner mark Int right=mid+1 ///Right sequence start corner label int cindex=0;//Temporary array//When both sides have value when executing while (left <= mid&& end>=right) {//Compare two array element size, such as: A:left=0 start to 3 , the length is 4,b:right=4 start, the length is 4,if (Arr[left]<arr[right]) {carr[cindex++]=arr[left++];} else{carr[cindex++]=arr[right++];}} Executes while (LEFT<=MID) {carr[cindex++]=arr[left++] when the right array has no elements;} Executes while (right<=end) {carr[cindex++]=arr[right++] When the left array has no elements;} Add all temporary arrays to the original array cindex=0;//pointer modified to 0while (Start<=end) {arr[start++]=carr[cindex++];}}

Use

The Mian method uses//an array internal sort int[] arr3={12,4,34,5,6,45,9};int[] carr=new int[7];mergeone (ARR3, 0, 6, CARR); System.out.println ("Sort inside an array"); for (int i=0;i<carr.length;i++) {System.out.println (carr[i]);
Merge efficiency

O (log2\n) is a logarithm of base N of 2.

Recursive--Two-point lookup Method--merge sort

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.