Javascript quick sorting function code _ javascript tips-js tutorial

Source: Internet
Author: User
The main principle is the principle of fast sorting: Find the benchmark, create two Arrays for storage, and recursive core code:

The Code is as follows:


Function quickSort (arr ){
// If the array has only one number, it will be returned directly;
If (arr. length <1 ){
Return arr;
}
// Locate the index value of the number in the middle. If it is a floating point, it is rounded down.
Var centerIndex = Math. floor (arr. length/2 );
// Locate the value of this number based on the index value of this intermediate number;
Var centerNum = arr. splice (centerIndex, 1 );
// Store the number on the left
Var arrLeft = [];
// Store the number on the right
Var arrRight = [];
For (I = 0; iif (arr [I] ArrLeft. push (arr [I])
} Else if (arr [I]> centerNum ){
ArrRight. push (arr [I])
}
}
Return quickSort (arrLeft). concat (centerNum, quickSort (arrRight ));
};
Var arrSort = [, 27];
Var arr1 = quickSort (arrSort );
Console. log (arr1 );



The main principle is: the principle of fast sorting: Find the benchmark, create two Arrays for storage, recursive

Benchmark: Find a number in the middle of the array;

Create two Arrays for storage: use this benchmark to store the left and right values in two new arrays defined respectively;

Recursion: Calls itself within a function;

Here I sum up the following:
1. A judgment is required and a value is returned; otherwise, it is an endless loop;
2. When you call yourself internally, the passed parameter is a variable defined internally. This variable is associated with the parameter obtained during initial transmission;
3. recursion can be used to perform the same job;

This is the first variable to execute the function: the number in the middle is 40; the condition smaller than 40 in the loop is stored in arrLeft, and the value greater than 40 is stored in arrRight. For example

Second function call
When return quickSort (arrLeft). concat (centerNum, quickSort (arrRight) is executed ));
QuickSort (arrLeft) will call the function. The passed parameters are [, 27].
The number in the middle is 2. If the number is smaller than 2, the left arrLeft is put. If the number is greater than 2, the right arrRight is put.

Finally, call quickSort (arrRight)

You can call yourself in the same loop. If the length of the input parameter is smaller than 1, the input parameter is returned.

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.