Algorithm surface test--positive and negative alternation

Source: Internet
Author: User

To an array that contains positive and negative integers, which requires that the number in this array be rearranged so that its positive or negative alternation occurs. A negative number first appears, followed by a positive number, followed by a negative number. There is an extra side, just put it at the end.

such as [1,2,3,-4]->[-4,1,2,3]
[1,-3,2,-4,-5]->[-3,1,-4,2,-5]

Requirements: Use O (1) space
Q 1: What is the time complexity if you need to keep the sequence of positive and negative numbers in their original order?
Q 2: If you do not need keep the sequence of positive and negative numbers in their original order, How do you do it, what is the complexity of the time?


Thinking Analysis: The topic requires only the space complexity of O (1), that is, only with the help of a zero-point variable, think of the following ideas, sequentially traversing each element in the array, if the number in the even position is negative, the number in the odd position is positive and remains unchanged, if the number in the even position is positive, The first negative number is found in the next position of the position, and then the moving negative number is moved to the even-numbered position (with the zero-time variable), which corresponds to the negative numbers in the odd position for similar processing.

The difference between 1 and 2 is that the number in the even position is positive or the number in the odd position is negative, ask 1 because to maintain the original order, so to move the elements in turn, and ask 2 do not need to maintain the original order, so you can exchange directly.


Q 1 Code, TIME complexity O (n^2):
void sorta (int arr[], int n) {for (int i = 0; i < n; i++) {if (i 2 = = 0 && Arr[i] < 0) continue;if (i% 2 = = 1 && arr[i]>0) continue;if (i 2 = = 0 && arr[i]>0) {int J;int temp;for (j = i + 1; j < n && ; arr[j]>0; j + +); if (j = = N) break;else{temp = Arr[j];for (; j > i; j--) {arr[j] = arr[j-1];} Arr[i] = temp;}} if (i% 2 = = 1 && arr[i]<0) {int J;int temp;for (j = i + 1; j < n && arr[j]<0; J + +); if (j = = N) br Eak;else{temp = Arr[j];for (; j > i; j--) {arr[j] = arr[j-1];} Arr[i] = temp;}}}}
Q 2 Code, time complexity O (n):
void SORTB (int arr[], int n) {for (int i = 0; i < n; i++) {if (i 2 = = 0 && Arr[i] < 0) continue;if (i% 2 = = 1 && arr[i]>0) continue;if (i% 2 = 0 && arr[i]>0) {int J;int temp;for (j = i + 1; J < n&& arr[j]>0; j + +); if (j = = N) break;else{temp = arr[j];arr[j] = arr[i];arr[i] = temp;}} if (i% 2 = = 1 && arr[i]<0) {int J;int temp;for (j = i + 1; j < n && arr[j]<0; J + +); if (j = = N) br Eak;else{temp = Arr[j];arr[j] = arr[i];arr[i] = temp;}}}



Algorithm surface test--positive and negative alternation

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.