Make the negative number before the positive number, without changing the original order

Source: Internet
Author: User
1/* 2 does not change the sequence of positive and negative numbers, so that the negative number is before the positive number, requires O (N), time complexity, O (1) space complexity 3 the actual situation, it is likely not done, you can use a method similar to the fast partition method, but it cannot guarantee the order. One method to ensure the order is to use flip, for example, 4, 2, 3, 4,-1,-2,-5, -6 ------ flip to 2, 3, 4,-1,-2,-5,-6, 3 ----- flip to-1,-2,-5,-6, 2, 3, the best case is O (n), and the worst case is O (n2) 5 method 2. The insertion sorting method is adopted. The time complexity is directly O (n2), and method 3 is used, space for time, scan in order. If it is a negative number, it is placed in an array, a positive number is placed in another array, and then 6 is copied back, O (N), space is also O (N) 7 */8 # include <iostream> 9 Using namespace STD; 10 # include <string> 11 12/* 13 method 1, using the interval flip method, 2, 3, 4,-1, -2, 3,-5,-614 */15 16 void rotatefull (int * a, int begin, int end) 17 {18 while (begin <End) 19 {20 swap (A [begin], a [end]); 21 begin ++; 22 end --; 23} 24} 25 void rotate (int * a, int begin, int part, int end) // For example 3,-5,-6, part = 0, begin = 0, end = 226 {27 if (a = NULL) 28 return; 29 rotatefull (A, begin, part); 30 rotatefull (A, part + 1, end); 31 rotatefull (A, begin, end ); 32} 33 34 void mainrotate (int * a, int begin, int end) 35 {36 int I = E Nd; // I recursively probe 37 Int J = end; // J is the end of the negative number of each flip. 38 While (I> = 0) 39 {40 while (I> = 0 & A [I] <0) 41 I --; 42 if (I <0) // 43 return; 44 int part = I; 45 while (I> = 0 & A [I]> 0) 46 I --; 47 rotate (, I + 1, part, J); // here, even if I is <0, it should be flipped because I + 1 starts to flip 48 J = J-(Part-I ); 49} 50} 51 52/* 53 insert sorting method: O (n2), also interesting, originally thought it was very easy, in fact, we still need to move 54 */55 void mainrotate2 (int * a, int N) 56 {57 if (a = NULL) 58 return; 59 for (INT I = 1; I <n; I ++) 60 {61 int TMP = A [I]; 62 Int J = I-1; 63 while (TMP <0 & A [J]> 0 & J> = 0) 64 {65 [J + 1] = A [J]; 66 J --; 67} 68 A [J + 1] = TMP; 69} 70} 71 72/* 73 change to a floating point number, in fact, it also increases the space, which is equivalent to changing the space time. 74 clever thinking, such as: 3, 4,-1,-3, 5, 2,-7, 6, 1 ------- to-1.1,-2.3,-3.7 and 1.3, 2.4, 3.5, 4.2, 5.6, 6.1, the change is very good, it is to record two num_po and num_ne respectively, and then it can change 75, but there are major defects, for example, if it is 12, how do you know whether to divide by 10 or 100 to 0.12? So it's just an idea, but it's not feasible. 76 */77 78 79 int main () 80 {81 int A [] = {,-5,-6, 9,-12, 15}; 82 int n = 7; 83 mainrotate (A, 0, n-1); 84 // mainrotate2 (A, n); 85 for (INT I = 0; I <n; I ++) 86 {87 cout <A [I] <Endl; 88} 89 system ("pause"); 90}

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.