Sorting Analysis and Parity Sorting in Arrays - Algorithm Data Structure Interview Sharing (4)

Sorting Analysis and Parity Sorting in Arrays


We have learned general sorting methods in textbooks before, such as bubbling, quicksort, insertion, and merge. The time complexity is O(N), O(Nlogn), and O(N2). Today we look at sorting in some specific cases. Whether all sorting is based on size, and sometimes the size range of the number to be sorted is known, let's look at two typical examples:

  • Given an array of integers, I want to sort the odd numbers first and the even numbers last, i.e. none of the even numbers appear before the odd numbers.
  • Give you an integer array, the numbers appearing in it are between [0-100], can you help me sort by the most optimized method?

Let's look at the first question today. Still in accordance with the previous six chapters.

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

The requirements have been refined in the title, and no even number is ranked in front of the odd number. For example {1, 2, 3, 4, 5} will be arranged as {1, 3, 5, 4, 2}. Of course, there is another question that needs to be confirmed, do we care about its size? That is, do both odd and even parts need to be in ascending order? I can tell you here that we can ignore this, and we only care about parity in this question.

2. Think about the ways you can solve the problem, which one would you choose and why?

When we get this question, the first thing we think about is how to judge the odd and even numbers. But that's not really the point of this question. A very simple method came out. I declare two arrays, one holds odd numbers and the other holds even numbers, and then merge the two arrays and return them. This method is feasible. When choosing parity, we only need to scan the array once, but we waste storage space. At least we need to declare two temporary arrays. Also, we don't know in advance how long the odd and even arrays should be. Is there a better way?

Let's look at the previous example again and see if we can find anything strange? {1, 2, 3, 4, 5} From front to back, in fact, 1 does not need to be moved, and 2 is sure to be moved back, where should it be placed? The exact location doesn't matter, but just make sure that there are no odd numbers after it. From the back to the front, 5 is an odd number, so be sure to put it forward. So far, if we can swap the 2 and 5 problems it's perfectly solved. That's right, we just need to find the even numbers from front to back, then find the odd numbers from back to front, and then swap. Find another one. If we take this approach, we can solve the problem by scanning the array once.

3. Explain your algorithm and how to implement it

From the above analysis, we only need to set two temporary variables. One from front to back, one from back to front, the condition of termination is that they meet. Then define a temporary variable to be responsible for the exchange.

4. When writing code, remember to explain what you are doing

Then we 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) // 偶数  
                    {  
                        if (array[end] % 2 == 1) //奇数  
                        {  
                            //交换  
                            int temp = array[end];  

                            array[end] = array[begin];  
                            array[begin] = temp;  

                            begin++;  
                            end--;  
                        }  
                        else  
                        {  
                            end--;  
                        }  
                    }  
                    else  
                    {  
                        begin++;  
                    }  
                }  
            }  
        }  

There is a place to optimize in the above code. When we find the even number to be swapped in front, and then determine whether there is an odd number in the back, we will always move the back pointer forward. So end -- can be removed, and the outside else is also deleted.

5. Workthrough

6. Fix bugs

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);  
        }  
    } 

You're done, ha. Welcome everyone to pay attention to my official account, as well as my series of special courses


If you have a better solution, please discuss.

Sorting Analysis and Parity Sorting in Arrays - Algorithm Data Structure Interview Sharing (4)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325367573&siteId=291194637