Algorithm 18 sorts array elements so that all odd numbers are placed before all even numbers.

Source: Internet
Author: User

[Subject] enter an integer array and adjust the number of the array so that all odd numbers are located in the first half of the array, and all even numbers are located in the second half of the array. The time complexity is O (n ).

[Thinking Road 1] First, we do not consider the time complexity requirements. Intuitive consideration: we only need to scan the entire array from start to end. If an odd number is reached, we should put it at the first position. If an even number is reached, put it to the last position. In this case, OK. Then we will analyze thisAlgorithmEfficiency problem: first, we need a temporary variable to save the elements to be moved, so the space complexity is O (1 ). in time, for the I element, it is an odd number and an even number of probability each is 1/2, so it has a probability of 1/2 moving forward (I-1) position, there is a probability of 1/2 to move (n-I) positions, so the average is to move (n-1)/2 location, that is to say, for any element to move (n-1) on average) /Two other elements, so the number of times all elements need to be moved is n (n-1)/2, so the time complexity of this algorithm is O (n ).

[Thinking 2] this question only requires an odd number to be placed in front of an even number. Therefore, we do not need to analyze one element or one element. Can we consider the first and last pointers of each element? Yes! We are defining two pointers, pointing to the first element and the last element of the array respectively, and then judging. If the first element is an even number and the second element is an odd number, then we will switch, otherwise, move the pointer directly to the next (previous) element. When the two pointers meet, the traversal is completed. In this way, our time complexity is O (n ). Based on this idea, we can easily write the followingCode:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 // Rearrange the array so that all odd numbers are in the first half of the array.
6 // All the even numbers are located in the second half of the array.
7 Void Reorderoddeven ( Int Numbers [], Int Length)
8 {
9 If (Numbers = NULL | length <= 0 )
10 Return ;
11
12 // Pointer at the beginning and end, traversing the two ends in the middle
13 Int * Start = & numbers [0 ];
14 Int * End = & numbers [length- 1 ];
15
16 While (Start <End)
17 {
18 // The first pointer points to an odd number and the pointer moves backward.
19 If (* Start % 2 = 1 )
20 {
21 Start ++;
22 Continue ;
23 }
24
25 // The tail Pointer Points to an even number, and the pointer moves forward.
26 If (* End % 2 = 0 )
27 {
28 End --;
29 Continue ;
30 }
31
32 // The first pointer points to an even number, and the last Pointer Points to an odd number in exchange;
33 Int Temp = * start;
34 * Start = * end;
35 * End = temp;
36 }
37
38 }
39
40 Int Main ()
41 {
42 Cout < " Enter your arraylength: " <Endl;
43 Int N = 0 ;
44 Cin> N;
45
46 Cout < " Enter your array elements: " <Endl;
47 Int * Array = New Int [N];
48 For ( Int I = 0 ; I <n; ++ I)
49 {
50 Cin> array [I];
51 }
52
53 Cout < " The orginal array is: " <Endl;
54 For (I = 0 ; I <n; ++ I)
55 {
56 Cout <array [I] < " " ;
57 }
58 Cout <Endl;
59
60 Reorderoddeven (array, N );
61
62 Cout < " The resorted array is: " <Endl;
63 For (I = 0 ; I <n; ++ I)
64 {
65 Cout <array [I] < " " ;
66 }
67 Cout <Endl;
68
69 Return 0 ;
70 }

The running result is as follows:

Reflection: This question is very simple. I can write the above Code simply by thinking a little bit. However, there are still many small details that I have not noticed. He Haitao's bloggers have to fully consider it, this code is poorly reusable. If all negative numbers are placed before all positive numbers, we need to re-modify some code in the function. He separated the function in his blog, the algorithm of rearranging arrays is separated from the standard of rearranging, so that code can be reused easily as long as the standard of rearranging is modified. It seems that my level is still quite poor. Continue to work hard!

 

 

References:

ProgramMember interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/25411174200741295930898/

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.


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.