Js: sorts an integer array by the number of factors, and then by the number size.

Source: Internet
Author: User

In a written test, I have to admit that this question is a good question and I have investigated many aspects. Js Array Operations, algorithms, logic capabilities, and the ability to prevent various pitfalls ......

It took about four hours in total. The function was tested one by one using firebug. It could not be imagined that it would be less than one hour in the test room, and debugging was not yet available ...... What can be written correctly is definitely a god. If the company encounters such a problem, it should accept it even if no other questions are written.


[Javascript]
/* Description: sorts several positive integers. The sorting method is to sort the values from large to small based on the number of factors, and to sort the values from large to small based on the same number of factors.
For example, the numbers of [6, 8, 12, 1, 9, 50] and 6 are 1, 2, 3, and 6, and the number of factors is 4. The number of factors in 8 is 1, 2, 4, and 4.
The result is [, 50, 6, 8, 9, 1]. For numbers with the same factors, such as 12 and 50, they are arranged in ascending order, that is, 50 and 12.
 
Note the characteristics of passing js parameters:
1. You can directly change the variables and array values that can be seen in the function.
2. You can directly change the array by passing it into the function (similar to a pointer)
3. The value of the variable is different. You cannot change the input value through the function (the same as that of the C language)
 
Summary:
1. Calculate the number of factors. There is only one factor for n = 1. If n is the number of workers, the number of workers is a factor. In other cases, there are two factors.
2. Find the I <= Math. floor (Math. sqrt (n) in the number of factors. Pay attention to the equal sign.
3. In del (arr, key), remove a number and move the pointer one by one (this function is not used later)
4. The names of local variables and global variables should not be the same.
*/
Var a = [6, 8,];
Showarray ();
Var sorted = new Array ();
SortFactorValue ();
Showarray ();
Function sortFactorValue () // sort by the number of factors
{
Var f = new Array (a. length );
GetFactor (f); // obtain the number of factors corresponding to the number
SortByFactor (f); // gets the order of the number of factors from large to small.
}
 
Function getFactor (f) // obtain the number of factors for each number
{
For (var I = 0; I <a. length; I ++)
{
F [I] = countFactor (a [I]);
}

}
 
Function countFactor (n) // obtain the number of factors of the number n
{
If (n = 1)
Return 1;
Var f = 2;
For (var I = 2; I <= Math. floor (Math. sqrt (n); I ++)
{If (n % I = 0)
{If (Math. floor (Math. sqrt (n) = Math. sqrt (n ))
{F + = 1 ;}
Else
{F + = 2 ;}
}
}
Return f;
}
 
Function sortByFactor (f) // sort the factor sequence and sequence from large to small by factor value
{

Var I = 0;
While (I! = F. length)
{Var start = I;
Var mf = Math. max. apply (Math, f. slice (I, f. length ));
I = findAndSwap (f, I, mf); // For array f, find the maximum number of factors starting from the I bit and switch it to the beginning, and increase I by 1. The first number is the maximum number of factors to be sorted.
SortByValue (start, I); // sort the numbers of the same factor by value. After sorting, all the numbers before I are sorted.
}
}
 
Function findAndSwap (f, I, mf) // starting from the I bit of array f, find the number with the value of mf, switch it to the beginning, and increase I by 1
{
For (var j = I; j <f. length; j ++)
{
If (f [j] = mf) // if the number of this factor is the largest, change it to the beginning and Add 1 to the flag I, all the numbers before I have been ranked by factor.
{
If (j = I) // if this number is the first number, you do not need to change it. Move the pointer directly.
{I ++ ;}
Else
{Var p; // if this number is not the first one, change it to the first one, move the pointer, and enter the next loop to compare the subsequent series.
P = f [j]; f [j] = f [I]; f [I] = p;
P = a [j]; a [j] = a [I]; a [I] = p;
I ++;
}
}
}
Return I;
}
 
Function sortByValue (s, e)
// Sort the sub-arrays from s to e in array.
// The idea of this function is the same as that of findAndSwap. It finds the maximum number and switches it to the first place. If the data structure is reasonable enough, the two functions can be merged.
{
While (s! = E)
{
Var max = Math. max. apply (Math, a. slice (s, e ));
For (var I = s; I <e; I ++)
{
If (a [I] = max)
{
If (I = s)
{S ++ ;}
Else
{
Var p;
P = a [I]; a [I] = a [s]; a [s] = p;
S ++;
}
}
}
}
}
/* Function factorList (f) // obtain the number of factors in the series from large to small.
// The idea is to first find the factor sequence and then traverse the array. Later, we found that the traversal was repeated, which was too troublesome.
{
Var fl = new Array ();
Var t = new Array ();
T = t. concat (f );
While (t. length)
{
Var mf = Math. max. apply (Math, t );

Fl. push (mf); // put the maximum number of factors into the Array
Del (t, mf); // Delete the maximum number of factors from the array
}
Return fl;
}*/
 
Function del (arr, key) // Delete All numbers whose values are key from array a. Note that the variable name cannot be the same as the public variable name.
{
For (var I = 0; I <arr. length; I ++)
{
If (arr [I] = key)
{Arr. splice (I, 1); // The position must be moved forward because a number is deleted.
I --;
}
}
}
Function showarray (a0) // output Array
{
For (var I = 0; I <a0.length; I ++)
{
Document. write (a0 [I] + "");
}
Document. write ("</br> ");
}

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.