Sorting analysis and parity ordering in arrays-algorithm data structure interview sharing (iv)

Source: Internet
Author: User

Sorting analysis and parity ordering in arrays

We have previously learned about general sorting methods in textbooks, such as bubbling, quick-line, inserting, merging. The time complexity is O (N), and O (Nlogn), and O (N2). Today we are looking at some sort of specific situation here, and whether all sorts are based on size, and sometimes the size range to be sorted is known, we look at two typical examples:

    • Give you an array of integers, I want to put the odd number in front, even in the back, that is, none of the even numbers are in front of the odd number.
    • Give you an array of integers, which appear in between [0-100], you can use the most optimized method to help me sort

Let's look at the first question today. or according to the previous six-part song.

1. Make sure that we understand the problem and try an example to make sure that you understand it correctly.

The problem has been refined, and no one even row in the odd number of front. For example {1,2,3,4,5} will be queued as {1,3,5,4,2}. There is, of course, one more question to confirm, do we care about its size? That is, the odd and even parts are required in ascending order? Here we can tell you that we can ignore this, and this problem only cares about the odd couple.

2. Think about what you can do to solve the problem, which one will you choose, and why?

To get this problem, the first thing we think about is how to judge the odd even. But this is not really the point. A very simple way out, I declare two arrays, one to save the odd number, the other to save an even number, and then make a merge of two arrays and return it. This method is feasible, and when we pick up the odd couple, we just need to scan the array again, but we waste the storage space, at least we need to declare another two temporary arrays. Furthermore, we cannot know in advance how long an array of odd and even numbers should be. Is there a better way?

Let's look at the previous example to see if we can find anything strange. {1,2,3,4,5} from the trip, in fact, 1 is not moving, 2 is determined to move backwards, where to put? The exact location doesn't matter, but make sure it doesn't have an odd number behind it. Again from the back forward, 5 is odd, OK to go forward. So far, if we can exchange 2 and 5 problems, it's a perfect solution. Yes, we just have to find the even number from the back, then find the odd number and then swap it out. Find the next. If we take this approach, we can scan the array again to solve the problem.

3. Explain your algorithm and how to implement it

From the above analysis, we just need to set a two temporary variable. One from the back, one from the back forward, the condition of termination is that they meet. Then define a temporary variable, which is good for swapping.

4. When writing the code, remember, be sure to explain what you're doing.

Then we'll go straight to the code.

public static void Switch (int[] array) {if (array! = NULL && array.                  Length > 1) {int begin = 0; int end = array.                  Length-1;                          while (begin < end) {if (Array[begin]% 2 = = 0)//even {                              If (Array[end]% 2 = = 1)//odd {//swap                              int temp = Array[end];                              Array[end] = Array[begin];                              Array[begin] = temp;                              begin++;                          end--;                          } else {end--;                      }} else {begin++;  }                  }              }        }   

There is one place in the code above that can be optimized, and when we find the even number that we want to swap before, and then determine if there is an odd number behind, we always move the back pointer forward. So end--can be moved out, and the outside else will be deleted.

5. Workthrough6. Repair defects Let's test this method together.
static void Main(string[] args)      {          int[] array = new int[] { 1, 2, 3, 4, 5 };          Switch(array);          foreach (var a in array)          {              Console.WriteLine(a);          }      

It's done, huh. You are welcome to pay attention to my public number, and my series of special courses

    • Video Tutorials
    • data structures and algorithms
    • The classical algorithm face question guidance
    • Sort by Topic explanation
    • List of topics explained

Everyone has a better solution, also welcome to discuss Kazakhstan.

Sorting analysis and parity ordering in arrays-algorithm data structure interview sharing (iv)

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.