Home Web Front-end JS Tutorial Example analysis of basic commonly used sorting algorithms in JavaScript

Example analysis of basic commonly used sorting algorithms in JavaScript

Sep 28, 2017 am 10:21 AM
javascript js algorithm

This article mainly introduces the basic commonly used sorting algorithms in javascript in detail, which has certain reference value. Interested friends can refer to it

Note: Most of the content is copied from the Internet, and the code is Handwrite it yourself. It is just a review of the past and new knowledge, not originality.

1. Bubble Sort

(1) Algorithm description

Bubble sort is a simple sorting algorithm. It repeatedly walks through the sequence to be sorted, comparing elements two at a time and swapping them if they are in the wrong order. The work of visiting the array is repeated until no more exchanges are needed, which means that the array has been sorted. The name of this algorithm comes from the fact that smaller elements will slowly "float" to the top of the array through swapping.

(2) Algorithm description and implementation

The specific algorithm is described as follows:

<1>. Compare adjacent elements. If the first one is larger than the second one, swap them both; <2>. Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so that the last element It should be the largest number; <3>. Repeat the above steps for all elements, except the last one; <4>. Repeat steps 1~3 until the sorting is completed.

JavaScript code implementation:

Improved bubble sorting: Set an iconic variable pos to record the last exchanged position in each sorting pass . Since the records after the pos position have been swapped in place, only the pos position needs to be scanned during the next sorting pass.

The improved algorithm is as follows:

In traditional bubble sorting, each sorting operation can only find a maximum or minimum value. We consider using The method of performing forward and reverse bubbling in each sorting pass can obtain two final values ​​(the largest and the smallest) at one time, thus reducing the number of sorting passes by almost half.

The improved algorithm is:

The running time of the three algorithms is:

It can be seen from the running results that the time complexity is lower and the time consumption is shorter. You can try it yourself. It is best to write the three algorithms in one file when running, otherwise errors will occur due to browsers and other reasons.

Dynamic diagram demonstration of bubble sorting:

(3) Algorithm analysis

Best case: T(n) = O (n)

When the input data is already in positive sequence

Worst case: T(n) = O(n2)

When the input data is in reverse sequence

Average case: T(n) = O(n2)

2. Selection Sort

The most stable sorting algorithm One, because no matter what data is entered, the time complexity is O(n²)...so when using it, the smaller the data size, the better. The only advantage may be that it does not occupy additional memory space. Theoretically speaking, selection sort may also be the most common sorting method that most people think of when sorting.

(1) Introduction to the algorithm

Selection-sort is a simple and intuitive sorting algorithm. How it works: First, find the smallest (large) element in the unsorted sequence and store it at the starting position of the sorted sequence. Then, continue to find the smallest (large) element from the remaining unsorted elements, and then put it into the sorted sequence. the end of. And so on until all elements are sorted.

(2) Algorithm description and implementation

Direct selection sorting of n records can obtain ordered results through n-1 direct selection sorting. The specific algorithm is described as follows:

<1>. Initial state: the unordered area is R[1..n], the ordered area is empty; <2>. The i-th sorting (i=1 ,2,3…n-1) At the beginning, the current ordered area and unordered area are R[1..i-1] and R(i..n) respectively. This sorting operation selects the record R[k] with the smallest key from the current disordered area, and exchanges it with the first record R in the disordered area, so that R[1..i] and R[i+1 ..n) respectively become a new ordered area with the number of records increased by 1 and a new unordered area with the number of records reduced by 1; <3>.n-1 pass ends, and the array is ordered.

Javascript code implementation:

(3) Algorithm analysis

Best case: T (n) = O(n2) Worst case: T(n) = O(n2) Average case: T(n) = O(n2)

The above is the detailed content of Example analysis of basic commonly used sorting algorithms in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance Mar 26, 2024 pm 12:41 PM

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

Explore the underlying principles and algorithm selection of the C++sort function

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

Improved detection algorithm: for target detection in high-resolution optical remote sensing images

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Mar 22, 2024 pm 10:10 PM

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities

PHP algorithm analysis: efficient method to find missing numbers in an array PHP algorithm analysis: efficient method to find missing numbers in an array Mar 02, 2024 am 08:39 AM

PHP algorithm analysis: efficient method to find missing numbers in an array

Application of algorithms in the construction of 58 portrait platform Application of algorithms in the construction of 58 portrait platform May 09, 2024 am 09:01 AM

Application of algorithms in the construction of 58 portrait platform

See all articles