Huawei interview questions about the sorting of 1 to n Natural Numbers

Source: Internet
Author: User

Article Source: http://blog.csdn.net/hongyuan19/archive/2007/11/16/1887656.aspx

When I visited a forum online, I found that a so-called expert posted such a question, saying that it was Huawei's interview question, as shown below:
There are n natural numbers (1--n) of varying sizes. Sort them in ascending order.
Program Algorithm required: the time complexity is O (n), and the space complexity is O (1 ).
After some analysis, the expert said that the question was actually very simple. The answer is as follows:

Void sort (int e [], int N)
...{
Int I;
Int t;/** // * Temporary Variable: space complexity O (1 )*/

For (I = 1; I <n + 1; I ++)/** // * time complexity O (n )*/
...{
T = E [E [I];/** // * elements whose subscript is E [I]. The value after sorting is E [I] */
E [E [I] = E [I];
E [I] = T;
}
}

I thought it was not good. I used his algorithm to write a program and tried it. After sorting the Array {5, 4, 3, 1, 2}, it was {2, 1, 3, 4, 5 }, I was confused. I thought about it carefully. Although this expert's analysis is correct, some things are ignored in implementation. In the above algorithm, each assignment only ensures that E [E [I] is correct, however, it cannot be ensured that E [I] is correct. After the I value is changed to I + 1, the assignment is over, so the problem will occur.
If you have been thinking for a long time and have not come up with the correct ideas, it seems that Huawei is still somewhat difficult.
I checked the results on the Internet, which made me very depressed. Many people directly posted the above solution, and did not care about the correctness of the content. It is estimated that the solution was directly pasted without a brain, what makes me unable to bear most is some of B's masters.
The topic "How do college students of computer science learn professional courses from an interview question from Huawei?" is explained in the grim employment situation, I also came up with a very confident sentence, "a simple sorting question, how many readers can be tested! ", Then, I will analyze the question and finally talk about how to study professional courses through this question. I also suggest working on the basic knowledge and doing more exercises, more computers, and more diligent thinking, let's take a look at how other people solve the same problem and educate people from a master's perspective.
As a result, I hope to become a programming expert. I have patiently read the general theory of this cloth, and then I will look at the solution he gave as follows:

Void sort (int e [], int N)
...{
Int I;
Int t;/** // * Temporary Variable: space complexity O (1 )*/

For (I = 1; I <n + 1; I ++)/** // * time complexity O (n )*/
...{
T = E [E [I>;/** // * elements whose subscript is E [I]. The value after sorting is E [I] */
E [E [I> = E [I];
E [I] = T;
}
}

Void main ()
...{
# Define max 10
Int I, a [Max + 1];
 
Printf ("input the number from 1 to % d:", max );
For (I = 1; I <MAX + 1; I ++)
...{
Scanf ("% d", & A [I]);
}

Sort (A, max );

Printf ("==== sort over === ");
For (I = 1; I <MAX + 1; I ++)
...{
Printf ("% d", a [I]);
}

Printf ("");
System ("pause ");
}
Why is it the same as the top algorithm? The above algorithm is not correct! I was too lazy to say anything, but I still couldn't help it.
I wonder if I have personally verified my algorithm while I suggest "multi-host? I don't know if I should read this question very easily while suggesting "Diligent Thinking", but I still think carefully? It is self-righteous and wrong to educate new people in such an irresponsible attitude! If I did not think about it carefully, I wrote it down. Isn't it ......
After chatting, I went on to talk about this question. Although this cainiao knows that the algorithm above is incorrect, I cannot think of any good improvement methods. Fortunately, I went to another forum on Baidu, answers provided in this forum are as follows:

Int CNT = 0; // auxiliary variable, not an algorithm component

Void sort (INT arr [], int N)

...{

Int t;/** // * Temporary Variable: space complexity O (1 )*/

// It can be proved that this algorithm must correctly arrange a number for each exchange, and there are only N exchanges at most.

// It is embodied in the CNT value, so although it is a dual loop, it is still time complexity O (N)

For (INT I = 1; I <= N; I ++)

...{

While (ARR [I]! = I)

...{

T = arr [arr [I];

Arr [arr [I] = arr [I];

Arr [I] = T;

++ CNT;

}

}

}

I posted a post on csdn and asked someone to answer the questions for me. The calculated results are correct. According to the explanation given, the complexity should also meet the requirements, that's the answer to this question. If you have any new insights, please do not hesitate to give me some advice.
Finally, BS the so-called self-righteous masters, and those who only care about posting posts...

Related Article

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.