


Example analysis of basic commonly used sorting algorithms in JavaScript
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Written above & the author’s personal understanding: At present, in the entire autonomous driving system, the perception module plays a vital role. The autonomous vehicle driving on the road can only obtain accurate perception results through the perception module. The downstream regulation and control module in the autonomous driving system makes timely and correct judgments and behavioral decisions. Currently, cars with autonomous driving functions are usually equipped with a variety of data information sensors including surround-view camera sensors, lidar sensors, and millimeter-wave radar sensors to collect information in different modalities to achieve accurate perception tasks. The BEV perception algorithm based on pure vision is favored by the industry because of its low hardware cost and easy deployment, and its output results can be easily applied to various downstream tasks.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

The convergence of artificial intelligence (AI) and law enforcement opens up new possibilities for crime prevention and detection. The predictive capabilities of artificial intelligence are widely used in systems such as CrimeGPT (Crime Prediction Technology) to predict criminal activities. This article explores the potential of artificial intelligence in crime prediction, its current applications, the challenges it faces, and the possible ethical implications of the technology. Artificial Intelligence and Crime Prediction: The Basics CrimeGPT uses machine learning algorithms to analyze large data sets, identifying patterns that can predict where and when crimes are likely to occur. These data sets include historical crime statistics, demographic information, economic indicators, weather patterns, and more. By identifying trends that human analysts might miss, artificial intelligence can empower law enforcement agencies

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images

1. Background of the Construction of 58 Portraits Platform First of all, I would like to share with you the background of the construction of the 58 Portrait Platform. 1. The traditional thinking of the traditional profiling platform is no longer enough. Building a user profiling platform relies on data warehouse modeling capabilities to integrate data from multiple business lines to build accurate user portraits; it also requires data mining to understand user behavior, interests and needs, and provide algorithms. side capabilities; finally, it also needs to have data platform capabilities to efficiently store, query and share user profile data and provide profile services. The main difference between a self-built business profiling platform and a middle-office profiling platform is that the self-built profiling platform serves a single business line and can be customized on demand; the mid-office platform serves multiple business lines, has complex modeling, and provides more general capabilities. 2.58 User portraits of the background of Zhongtai portrait construction

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
