Baidu: In the space complexity of O (1) in the range of an array in a sequence of sequential arrays to merge and sort

Source: Internet
Author: User

First, the topic understanding

Title: Array al[0,mid-1] and al[mid,num-1] are each ordered, two sub-ordered segments of the array al[0,num-1] to merge, get al[0,num-1] overall order. Requires a space complexity of O (1). Note: The al[i] element is supported by the ' < ' operator.

The first chapter of the data structure tells the order table merging, but then it is merged into the new table, the condition is while (i<len1| | J<LEN2), and then put the A1 or A2 array (only one, because the other must have been completely inserted into the C array, which is why the while condition is "or") after the element, if the data structure of the teacher is responsible enough to mention this situation, or to speak; Many of the bat's face questions come from textbooks.

Second, the implementation of the algorithm

Set two pointers left and right, the initial state points to the first element of the two sorted array, and then compare A[left] and a[right] size, and if a[left]<=a[right], then the element position in the array does not change, and then go further forward. If A[left]>a[right], indicates that there are elements in the first half of the element that are larger than the second half, then we move the small element of the second half to the first half of the paragraph. But before we move, we have to leave space for this element. This has the action of moving the element. For example, {1,3,5,7,2,4,6,8,10} such a subsequence, we found that the second half of the 2 is less than the first half of the 3, then we put 2 into the TEMP variable temp, then the {3,5,7} to move back one position, and then put the vacated position into the value of temp. The overall loop here is while (Left<right&&right<len).

To do this, I really think of the insertion sort, but combined with ACM, I think the insertion sort needs to move elements, so that the time complexity may be higher, not AC (ACM has too much influence on me), I immediately ruled out the idea, hey, only need to consider the space complexity.

Package A;public class Test1 {static int[] a = {1,3,5,7,9,0,2,4,6,8};p ublic static void Main (string[] args) {//TODO auto- Generated method Stubsolve (a,4,9); for (int i:a) {System.out.print (i+ "");} System.out.println ();} private static void Solve (int[] A, int mid, int num) {//TODO auto-generated method Stubint i=0;int j=mid+1;/* * Original Condition I added I <=mid, no i<j, this is completely wrong, because we moved the element, so the first half of the paragraph can not be limited to mid; * I think i<j should think about it, just need this one is enough, if I have AC every day may remember this condition. */for (i=0,j=mid+1;i<j&&j<=num;) {if (A[i]<=a[j]) {i++;} else {int tempval = a[j];for (int k=j-1; k>=i; k--) {a[k+1] = a[k];} A[i] = tempval;j++;/* * This self-addition condition cannot be placed behind if else, so if the if inside i++, and then i++ */i++;}}}

Baidu: In the space complexity of O (1) in the range of an array in a sequence of sequential arrays to merge and 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.