A side question about the normal distribution of JavaScript arrays

Source: Internet
Author: User
Tags array length

In recent days, the cool weather in Shanghai 40° to find a job, the heart is happy not to, every interview is to sit there out of a half a day sweat to return to God, feel the world to my deep love, the interview process encountered several written tests, which have such a problem, because the actual work has not met, So watch out, the title is this:

There is an array of: var arr = [1,2,1,3,3,2,4,6,3], which is processed to form a normal distribution: [1,2,3,3,6,4,3,2,1].

About normal distribution You can view this link: http://baike.baidu.com/link?url= OWBFF2PYVH3S5STNYYRDAQCWJG5-FO4WRP1HYDR94WK9AUNSDUAGONMV4MTFHLLPRMKF2A2GZPTBFKJYYPIOJK, do not know how to make the editor Insert link is not available, Only such a hair, not bothered to see the classmate I simply explain it, actually see the array after processing can understand, is two small, middle large, reflected in the axis of the normal curve is bell type, both low, Middle high, left and right symmetry because its curve is bell-shaped, so people often called bell-shaped curve.

This is the last question of the interview, to do here time is more tense and the weather is hot thirsty hungry front desk sister too good (don't nonsense is because the algorithm is weak ... ), think about it and write the following code:

 var  arr = [1,2,1,3,3,2,4,6,3 ~ ( Span style= "color: #0000ff;" >function   var  temp = [], I = 0, l = Arr.length, Sortarr  = Arr.sort (func tion  (A, b) {return  a-b})//first array from small to large  [1, 1, 2, 2, 3, 3, 3, 4, 6] 
 for (; i<l;i++) {            if(i%2==0) {                temp[i/2] = Sortarr[i]    //subscript to the front            in even order else  {                temp[l-(i+1)/2] = Sortarr[i]//subscript is an odd-from-forward             }        }
Console.log (temp)//[1, 2, 3, 3, 6, 4, 3, 2, 1] looks perfect ha }) (arr)


Because it is a written test, oneself in the mind yy A will program, feel no big problem on the paper, and then the interviewer looked at the test, in the interview process did not mention this problem, so feel that this method is not in the process of the interview again asked, but came back on the way, I suddenly thought of a situation like this:

vararr = [1,2,3,4,5,6,7,8,9]//An array with a regular increment~(function(arr) {vartemp = [], i = 0, L =Arr.length, Sortarr= Arr.sort (function(A, B) {returnA-B}) for(; i<l;i++){            if(i%2==0) {Temp[i/2] = Sortarr[i]}Else{temp[l-(I+1)/2] = Sortarr[i]}} console.log (temp)//[1, 3, 5, 7, 9, 8, 6, 4, 2] the problem arises. }) (arr)

Yes, so that the left and right parts of this array is not symmetrical, with 9 as the center, the left side is 1+3+5+7=16, the right side is 2+4+6+8=20, obviously the left light weight, not a uniform normal distribution, with the increase of the array, the problem will become more and more serious.

Flax belt .... I am a bud Huaguduo don't do this to me ...

It seems that the front of the code is not used, can only rethink the solution, in fact, the core of the problem is to ensure that the left and right sides of the array are equal or roughly equal, whether it is an odd number of arrays or even number of numbers, the array can be divided into two parts (odd number of the maximum value can also be considered an even array Even if there are more than the same maximum value, from small to large sorting after the last one can be removed, or in accordance with the above method, subscript is even when placed to the left, for odd time to the right, in the left and the same time array growth, when the array length is equal, the left and right sides of the array of the comparison, Because it is arranged from small to large, so under normal circumstances, the right side will be greater than the left, and then the right side first with the last one to the left to achieve a balanced purpose, the code is as follows:

1 vararr = [1,2,3,4,5,6,7,8,9],2Sortarr = Arr.sort (function(A, B) {returnA-b}),3L =Arr.length,4Temp_left = [], temp_right = []5 6     functionSort (arr) {7         vari = 08          for(; i<l;i++){9             varEQ =Sortarr[i]Teni%2 = = 0?Temp_left.push (eq): Temp_right.unshift (eq) One             if(I > 1){ A                 if(Temp_left.length = = Temp_right.length &&!)Compare (Temp_left, temp_right)) { - Wrap (Temp_left,temp_right)//array is equal and the right side and greater than the left side are exchanged -                 } the             } -         } -         returnTemp_left.concat (temp_right) -     } +  -  +     //Array Summation A     functionsum (arr) { at         returnEval (Arr.join ("+"))); -     } -  -     //Array Comparison size -     functionCompare (ARR1,ARR2) { -         returnSUM (arr1) >=sum (ARR2) in     } -  to     //The last one on the left is the first one with the right . +  -     functionWrap (l,r) { the         varm =R.shift () * R.unshift (L.pop ()) $ L.push (m)Panax Notoginseng     } -  theConsole.log (sort (arr))//Get [1, 4, 6, 7, 9, 8, 5, 3, 2]

In this way the entire normal distribution is much more uniform, do a few sets of tests to see the effect:

arr = [1,333,444,555,66,7788,909/[1, 444, 909, 7788, 555, 333, $]= [ 168.6,177.5,174.2,189.3,167.2,177.6,167.8,175.5//[167.2, 174.2, 175.5, 189.3, 177.6, 177.5, 168.6, 167.8]

Looks good, in the process of Baidu Information also found an article, link is http://www.lai18.com/content/524601.html, seemingly in C + + completed, but see the final result of the article, not a uniform normal distribution, It's almost like my first procedure.

I do not how will C + +, nor run multiple sets of results to see, interested students can try to compare.

All the procedures in this article I only tested in chrome, if the other browser has a problem, I would like to leave a message to inform, in fact, this thing is not difficult, right when a record it, when necessary can be used.

A side question about the normal distribution of JavaScript arrays

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.