There is no need for an integer exchange for extra space and a sorting algorithm whose time complexity is O (n) and the space complexity is O (1 ).

Source: Internet
Author: User

Once you have read the question for a long time, you must have guessed it was a title party.

It is said that the recruitment questions of a company, a Bt programmer pursuing time and space limitations, may flash his head at a time to develop these so-called Nb algorithms, and then proudly present them to the Technical interviewers, asking them to use them to test whether the heads of our common programmers are shining. However, the development of the network may make their idea completely frustrated. Originally, some high school students can understand the algorithm (it seems I am not exaggerating). With the spread of the network, everyone on the Earth knows it. So at the end of the day, you can only test those who have never read a blog like me :). Therefore, I prefer to classify this question as an intelligence test.

Many foreign experts are questioning the accuracy of the intelligence test. Today, when knowledge can be quickly obtained, as long as you have done it once, the second test is no longer an intelligence test, but a memory test. So it's strange why the interviewer has an incredible liking for intelligence testing, which we call "Intelligence Testing Preferences ".

Well, let's get down to the truth. After reading the ggmm of this blog, you don't have to go through the brain when you interview this question. You can blurt out, write it out, or take it out (coding, what is that ?).

// Do not use the temp integer exchange, just a little mathematical trick :)
Void notempswap (Int & A, Int & B)
{
A = A + B;
B = A-B;
A = A-B;
}

The time complexity is O (n) and the space complexity is O (1) integer sorting algorithm is actually a prerequisite. The interviewer generally asks:

Given 1000000 numbers, these numbers are all 0 ~ Between 65535, design an algorithm to sort these numbers?

The following niuabilitysort () function is an implementation of the algorithm thought that the time complexity is O (n) and the space complexity is O (1:

// Send 100000 ~... Sort by integers. The values of these integers are from 0 to 0 ~ Between max_num
Const int max_num= 65535;
Int base_array [max_num];

// Sort_array is the array to be sorted, and length is the length of this array
Void niuabilitysort (int * sort_array, int length)
{
For (INT I = 0; I {
// Initialize the base array first
Base_array [I] = 0;
}

For (INT I = 0; I <length; I ++)
{
Base_array [sort_array [I] ++;
}
// Update the sorting result to sort_array
Int J = 0;
For (INT I = 0; I <max_num; I ++)
{
If (base_array [I]! = 0)
{
For (int K = 0; k {
Sort_array [J] = I;
J ++;
}
}
}
}

The idea of this algorithm is to use an array (base_array here) to record the number of times each number appears in the array to be sorted when the number range is limited, then scan the base_array array again and update sort_array by the number of times. The update value is the current base value of base_array.

The space of base_array is a constant, so the space complexity is O (1 ). Perform Two traversal times and scan sort_array once. The time complexity is O (n) and base_array is scanned once. The time complexity is O (1 ), so the last time complexity is O (n ).

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.