Algorithm data structure interview sharing (vi) array sorting problem (2)-counting sort

Source: Internet
Author: User

Array sorting problems (2)

Yesterday we left a topic "give you an integer array, where the number appears in between [0-100], can you use the most optimized method to help me sort?"

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

This is a sort of algorithm problem, we have learned a lot of sorting algorithms. Not the same, given an extra condition, each number in the array is between 1-100. If we take the traditional sorting algorithm, we can't seem to use this condition. In the interview if you find that conditions are not used, basically we give the algorithm may not be optimal, or we did not solve its most primitive needs. For example {50, 46, 50, 0, 100,0} In this array, we can see at a glance that 0 has two, 46 has one, 50 has two, 100 have one, and we stitch them together, we get {0, 0, 46, 50, 50,100}.

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

In the above analysis we can summarize, we manually to sort out the time involved in two important steps. 1: Statistics 0-100 The number of occurrences of each number: 2: From 0-100 of the order according to the number of times they appear splicing. So now we need to solve the problem how easy to count. Declare a number with a length of 101, assuming that 50 appears once, we add 1 to the position of the array labeled 50. All counted, we'll scan the number and write the results back.
We now look at its complexity, we scanned the original array once, and scanned the count array once, so our complexity is O (n). Here we also found that we have a principle in this problem, with space to change time.

3. Explain your algorithm and how to implement the Count part: Scan the original array and find the corresponding position in the index array
int[] indexArray = new int[100];foreach(var e in inputArray){  indexArray[e] ++;}
Merging array parts
int count = 0;for(int index = 0; index < indexArray.Length; index++){if(indexArray[index] > 0 ) //说明index的数字出现过,我们需要拼接起来,出现了几次我们就加几个数 {  inputArray[count] = index;  count ++;   }}
4. When writing the code, remember, be sure to explain what you're doing.
那我们就直接上代码啦。
 ///<summary>///For array, the value in this array is between 0-100////</summary>//<param Name= "Array" ></param> public static void Indexsort (int[] array) {int[] Indexarray =             New INT[101];             for (var index = 0; index < indexarray.length; index++) {Indexarray[index] = 0;             } foreach (var e in array) {indexarray[e]++;             } int count = 0;                 for (int index = 0; index < indexarray.length; index++) {if (Indexarray[index] > 0)                     {for (int elementcount = 0; Elementcount < Indexarray[index]; elementcount++)                     {array[count++] = index; }                 }             }              }  

We have found that the above code can actually be optimized, will reflect your basic skills oh. You can talk to the interviewer if you want to put in a push. The default value for int is 0, so we don't need to scan it to assign a default value to it again. So this piece of code is superfluous:

[csharp] view plain copyfor (var index = 0; index < indexArray.Length; index++)  {     indexArray[index] = 0;  }  

Let's test this method:

[csharp] view plain copystatic void Main(string[] args)    {        int[] array = new int[] { 100, 8, 0, 7, 0, 34 };        IndexSort(array);        foreach(var e in array)        {            Console.Write(e + " " );        }    }  
5. The results we get:

It's done, huh. If you have any questions about the above algorithm, or better solution, welcome to communicate.

    • 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.

List, sort topics to learn more about resources.

Follow our public number and get regular push. Pay attention to the course, there will be a regular discount.

Algorithm data structure interview sharing (vi) array sorting problem (2)-counting sort

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.