Sort and array in JS algorithm detailed overview _javascript tips

Source: Internet
Author: User
Tags array length

In fact, in JS to achieve array ordering, using the Sort method in the array to achieve or relatively simple:

A, sorting

Simple implementation of array ordering

Copy Code code as follows:

var arr = [];
for (Var i=0;i<20;i++) {
Arr.push (Math.floor (Math.random () *100))
}
Arr.sort (function (a,b) {
Return a>b?1:-1;
})
Alert (arr)

You can't simply use the Sort method, by default the sort method is sorted in ASCII alphabetical order, rather than what we think is sorted by number size,

The sort () method can accept a method as a parameter, and this method has two parameters. Represents each of the two array entries for each sort comparison. Sort () Sorts each compares two array entries to execute this parameter, and the two comparison array

The item is passed as an argument to this function. The order of two array items is exchanged when the function returns a value of 1, otherwise it is not exchanged.

Array ordering of algorithms

Copy Code code as follows:

var arr = [];
for (Var i=0;i<20;i++) {
Arr.push (Math.floor (Math.random () *100))
}
Generates an unordered array of arr
function sort (arr,start,end) {
Array length is 1
if (start = = end) {
return [Arr[start]]
}else if (start = = end-1) {
Array length is 2, sorted by numeric size
if (Arr[start]>arr[end]) {
return [Arr[end],arr[start]]
}else{
return [Arr[start],arr[end]]
}
}
Array length Half
var L = Math.floor ((start+end)/2);
Left array
var arrleft = sort (arr, start,l);
Right array
var arrright = sort (arr,l+1,end);
return results
var result = [];
Divided into two parts of the left and right two arrays is only the first number in the array, the value of the small is put into the result, and the small value removed, solid using the array of shift method. Once the left array or the right array appears, there is no data
The result array merges the concat with the array with the data and returns the results
while (arrleft.length>0 | | arrright.length>0) {
if (arrleft.length==0) {
result = Result.concat (arrright);
Break
}else if (arrright.length==0) {
result = Result.concat (arrleft);
Break
}
if (Arrleft[0]<arrright[0]) {
Result.push (Arrleft.shift ())
}else{
Result.push (Arrright.shift ());
}
}
return result;
}
var arrsort = sort (arr,0,arr.length-1);//parameter array, start position, end position

document.write (arr+ ' <br/> ' +arrsort);

Explanation: Array sorting is mainly used to split the array into two, until it is not, the last thing you can do is to tear down an array that can only be one or two, because the length of the array has an odd-even number of points, and after the last array, only one or two of them are sorted and returned, and the results are merged in one by one pairs. This method may be why people feel so complicated, have been using the first kind of, of course, but there is the word performance in the world, when the data after a few dozens of hundred, everyone's calculated results time is no difference, if the data is huge hundreds of millions of billions of We also have this self credit first method, in fact, the JS algorithm is divide and conquer, many problems divided into small to solve.

Second, the array to remove duplication

Simple method to remove repetition: First declare an empty array, insert the repeated array for loop, repeat skip the repeat insert

Copy Code code as follows:

var arr = [];
for (Var i=0;i<20;i++) {
Arr.push (parseint (Math.random () *10));
}
Array.prototype.indexOf = function (n) {
for (Var i=0;i<this.length;i++) {
if (this[i] = = N) {
return i;
}
}
return-1;
}
function Removedup (arr) {
var result = [];
for (Var i=0;i<arr.length;i++) {
if (Result.indexof (arr[i]) = = 1) {

Result.push (Arr[i]);
}
}
return result;
}
var arr2 = removedup (arr)
document.write (arr+ ' <br/> ' +arr2)

Algorithm array Remove duplicate
Copy Code code as follows:

var arr = [];
for (Var i=0;i<20;i++) {
Arr.push (parseint (Math.random () *10));
}
Array.prototype.indexOf = function (n) {
for (Var i=0;i<this.length;i++) {
if (this[i] = = N) {
return i;
}
}
return-1;
}
function Removedup (arr,s,e) {
if (s==e) {
The split is left with one
return [Arr[s]]
}else if (s==e-1) {
To optimize the remaining two, you don't have to split it.
if (Arr[s]==arr[e]) {
return [Arr[s]]
}else{
return [Arr[s],arr[e]];
}
}
The array is divided into two segments,
var L = Math.floor ((s+e)/2);
Left
var ARRL = removedup (arr,s,l);
Right
var arrr = removedup (arr,l+1,e);
Turns the left copy in first.
var result = ARRL;
Loop inserts the data that is not duplicated into the result
for (Var i=0;i<arrr.length;i++) {
if (Result.indexof (arrr[i]) = = 1) result.push (arrr[i))
}
return result; return results
}
var arrdup = removedup (arr, 0, arr.length-1);
document.write (arr+ ' <br/> ' +arrdup);

Explanation:The repeated array is cut, split to the last only one data or two arrays, the left side of the data into the results, the right repeated skip repeat insert, until the end of the loop, return the result can be

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.