Summary of some common JavaScrip algorithms: javascrip Algorithms

Source: Internet
Author: User

Summary of some common JavaScrip algorithms: javascrip Algorithms

The following describes some common javascript algorithms. If you need them, you can refer to them. Of course, these algorithms are not only applicable to javascript, but also to other languages.

1. linear search:

Simple, entry-level Algorithm

// A is an array, and x is the function linearSearch (A, x) {for (var index = 0; index <. length; index ++) {if (A [index] = x) {return index ;}} return-1 ;}

Ii. Binary Search:

It is also called semi-query. It is suitable for Linear Structures with sorted orders.

// A is an array sorted in ascending order, and x is the element to be queried. // return the subscript function binarySearch (A, x) of the target element {var low = 0, high =. length-1; while (low <= high) {var mid = Math. floor (low + high)/2); // take the following integer if (x = A [mid]) {return mid;} if (x <A [mid]) {high = mid-1 ;}else {low = mid + 1 ;}} return-1 ;}

Iii. Bubble Sorting:

// Bubble Sorting function bubbleSort (A) {for (var I = 0; I <. length; I ++) {var sorted = true; // note: the inner loop is inverted for (var j =. length-1; j> I; j --) {if (A [j] <A [j-1]) {swap (A, j, j-1 ); sorted = false ;}} if (sorted) {return ;}}}

Iv. Insert sorting:

// Insert sorting // assuming that the elements before the current element have already sorted out their order, first empty their positions, // then move the elements that are later than themselves in turn, until A "pitfall" is empty, // Insert the target element into the "pitfall" function insertSort (A) {for (var index = 1; index <. length; index ++) {var x = A [index]; for (var j = index-1; j> = 0 & A [j]> x; j --) {A [j + 1] = A [j];} if (A [j + 1]! = X) {A [j + 1] = x; println (A) ;}} return ;}

V. String inversion:

// String inversion (for example, ABC-> CBA) function inverse (s) {var arr = s. split (''); var index = 0, j = arr. length-1; while (index <j) {var t = arr [index]; arr [index] = arr [j]; arr [j] = t; index ++; j --;} return arr. join ('');}

The above content briefly introduces the common algorithm Summary of JavaScrip. I hope this article will help you.

Articles you may be interested in:
  • Js version A * pathfinding Algorithm
  • Common sorting algorithms in Javascript
  • A * game Path Algorithm arrangement implemented by js + ajax
  • New JavaScript blog-style calendar Control Algorithm
  • Common JS encryption algorithm code
  • A simple JavaScript date calculation algorithm
  • Javascript & | an alternative use technique for arithmetic operations
  • Summary of several common sorting algorithms in JavaScript
  • Summary of various algorithms for generating guids using JavaScript
  • Detailed overview of sorting and array deduplication in js Algorithms
  • Algorithms and examples for parsing four arithmetic expressions in javascript
  • MD5 encryption algorithms implemented in Java, JavaScript, Oracle, and MySQL
  • Js exchange sorting Bubble Sorting Algorithm (Javascript Version)

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.