JS sorting method (sort, bubble, select, insert) code aggregation _ basic knowledge

Source: Internet
Author: User
New technologies are constantly changing. mastering some foundations is a solid foundation for learning and updating technologies in the future. Recently, I have nothing to worry about. To learn more about the previously learned data structure, I used JavaScript to implement the Sorting Algorithm in the data structure. Recently I started to learn about the data structure.

I hope to stick to it.

Because the direction is the frontend, it is implemented using JavaScript.

// Sort var testArr1 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; var testArr2 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; var testArr3 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; testArr1.sort (); // sorting result: [15, 19, 2, 26, 27, 3, 36, 38, 4, 44, 46, 47, 48, 5, 50] testArr2.sort (function (a, B) {return a> B}); // sorting result: [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50] testArr3.sort (function (a, B) {return a-B}); // sorting result: [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]

// Sort buckets

Var testArr1 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; var testArr2 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; function bubbleSort1 (array) {for (I = array. length-1; I> 0; I --) {for (j = 0; j
 
  

// Select sorting

Var testArr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; function selectSort (array) {for (I = 0; I

// Insert sorting

Var testArr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; function insertSort (array) {for (var I = 0; I <array. length-1; I ++) {// note that I is smaller than the length of the array-1; otherwise, the array may be out of bounds, forming an endless loop var curElement = array [I + 1]; for (var j = I; j> = 0; j --) {if (curElement

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.