[Job] Sorting algorithm exercise (i)

Source: Internet
Author: User

Analysis: It is easy to think of not sorting, directly using an array to record each number of occurrences.

Time: O (n+m), N is the number of data, M is the maximum number, in order to be simple and directly set to 30000;

Space: O (M)

Code:

Analysis: In the array, fast row.

Time: Average O (Nlg n+k)

Space: O (N)

Code:

Analysis: According to the shortest point-to-line distance, in the shortest solution, all the lines must be perpendicular to the pipeline, that is, north-south direction. Therefore, the x-coordinate given does not work.

Now, the problem turns to:

Known n number y1,y2,..., yn, a number y0, so that the value of |y1-y0|+|y2-y0|+...+|yn-y0| is minimized.

Sort the y array first, and think of Y1<=y2<=y3<=...<=yn later.

If n is even, these numbers can be divided into N/2 groups: {y1,yn},{y2,y (n-1)},..., {y (N/2), Y (N+1-N/2)};

for {Y1,yn}, when Y1<=y0<=yn, there is |y1-y0|+|yn-y0|=yn-y1, if y0 outside this area, then |y1-y0|+|yn-y0|>yn-y1.

This formula is set for any group, and ultimately the sum of y (N/2) <=y0<=y (n/2+1) is minimized. In order to simplify the procedure, we can take the average of the two, that is, the median of the original data.

If n is odd, then after the N/2 group is divided, there is still a number Y (n/2+1), it is obvious that y0=y (n/2+1) can, is still to seek the median number.

In a comprehensive way, the Y array is sorted first, then the median is obtained, and the sum of the distance is output according to the requirements.

Time complexity: O (NLG N)

Space complexity: O (N)

Code:

Analysis: The value of n is very large, it is impossible to save the votes of everyone directly. Note that there are at most only 10000 votes, so the real get tickets will not exceed 10000 people, so it is good to run.

One scheme is discretization (the word is somewhat high-end, but very simple), the stored array into a record type, two fields are numbered and the number of votes. In this case, it takes an O (M) time for each number to be read (or an array of records sorted by number, then two points, so it won't be much faster) to find the location, and then process it, with a time complexity of O (M2), which may time out.

Another option is to sort the data that is being read in so that the same number is bound together. It can then be scanned in O (N) time, given numbers and the number of times they appear. Here is no storage, find more than half of the output, and immediately end the program (obviously there will not be two more than half).

Time complexity: O (MLG M)

Space complexity: O (M)

Code:

[Job] Sorting algorithm exercise (i)

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.