JS Bubble Sort Selection Sort and Insertion Sort Example Analysis
This article mainly introduces the bubble sort, selection sort and insertion sort of JS sorting algorithm. It analyzes the concepts, principles and implementation methods of bubble sort, selection sort and insertion sort in the form of examples. Friends who need it can refer to it. I hope Can help everyone.
Bubble sorting:
Compare the sizes of two adjacent numbers in order for the data in the array.
If the previous data is greater than the later data, exchange the two numbers.
Time complexityO(n^2)
function bubble(array){ var temp; for(var i=0; i<arr.length; i++){ for(var j=0; j<arr.length; j++){ if(arr[j]>arr[j+1]){ temp = arr[j+1]; arr[j+1] =arr[j]; arr[j]=temp; } }console.log(arr); } }//冒泡排序
Selection sort:
First select the smallest data from the original array and exchange it with the data at position 1.
Then select the next smallest data from the remaining n-1 data and exchange it with the data at the second position.
Repeat until the last two data are exchanged.
Time complexityO(n^2)
function selectionSort(array){ var min,temp; for(var i=0; i<array.length-1; i++){ min=i; for(var j=i+1; j<array.length; j++){ if(array[j]<array[min]){ min=j; } } swap(array,min,i); } console.log(array); }//选择排序 function swap(array,i,j){ var temp =array[i]; array[i]=array[j]; array[j]=temp; }//两个数字交换
Insertion sort:
First compare the first two data from small to large.
Then compare the third data with the first two arranged data, and insert the third data into the appropriate position. And so on.
(Insertion sort has two loops. The outer loop moves the arrays one by one, and the inner loop compares the element selected by the outer loop with the number in front of it.)
Time complexity O(n^2)
function insertSort(arr){ var temp, j; for(var i=1; i<arr.length; i++){ temp =arr[i]; j=i; while(j>0 && arr[j-1]>temp){ arr[j]=arr[j-1]; j--; } arr[j]=temp; } }
Related recommendations:
Counting sorting and cardinality implemented by JS Sorting algorithm example_javascript skills
Detailed explanation of sorting algorithm
Detailed explanation of javascript array deduplication and quick sort algorithm examples
The above is the detailed content of JS Bubble Sort Selection Sort and Insertion Sort Example Analysis. 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

Does Wallpaper support family sharing? Unfortunately, it cannot be supported. Still, we have solutions. For example, you can purchase with a small account or download the software and wallpapers from a large account first, and then change to the small account. Simply launching the software is perfectly fine. Can wallpaperengine be family shared? Answer: Wallpaper does not currently support the family sharing function. 1. It is understood that WallpaperEngine does not seem to be suitable for family sharing environments. 2. In order to solve this problem, it is recommended that you consider purchasing a new account; 3. Or download the required software and wallpapers in the main account first, and then switch to other accounts. 4. Just open the software with a light click and it will be fine. 5. You can view the properties on the above web page"

WallpaperEngine is a software commonly used to set desktop wallpapers. Users can search for their favorite pictures in WallpaperEngine to generate desktop wallpapers. It also supports adding pictures from the computer to WallpaperEngine to set them as computer wallpapers. Let’s take a look at how wallpaperengine sets the lock screen wallpaper. Wallpaperengine setting lock screen wallpaper tutorial 1. First enter the software, then select installed, and click "Configure Wallpaper Options". 2. After selecting the wallpaper in separate settings, you need to click OK on the lower right. 3. Then click on the settings and preview above. 4. Next

iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

Users can download various wallpapers when using WallpaperEngine, and can also use dynamic wallpapers. Many users do not know whether there are viruses when watching videos on WallpaperEngine, but video files cannot be used as viruses. Is there any virus when watching movies on wallpaperengine? Answer: No. 1. Just video files cannot be used as viruses. 2. Just make sure to download videos from trusted sources and maintain computer security measures to avoid the risk of virus infection. 3. Application wallpapers are in apk format, and apk may carry Trojan viruses. 4. WallpaperEngine itself does not have viruses, but some application wallpapers in the creative workshop may have viruses.

Usage of MINUS in SQL and specific code examples In SQL, MINUS is an operator used to perform a difference operation between two result sets. It is used to delete the same rows from the first result set as in the second result set. The result set returned by the MINUS operator will contain rows that exist only in the first result set. The following uses specific code examples to demonstrate the usage of MINUS: Assume there are two tables - "table1" and "table2", their structures are as follows: Table name: table1 field

When using wallpaper, users can download various wallpapers they like for use. Many users do not know which folder the wallpapers are in. The wallpapers downloaded by users are stored in the content folder. Which folder is the wallpaper in? Answer: content folder. 1. Open File Explorer. 2. Click "This PC" on the left. 3. Find the "STEAM" folder. 4. Select "steamapps". 5. Click “workshop”. 6. Find the “content” folder.

Users can change their computer wallpapers when using WallpaperEngine. Many users don't know that WallpaperEngine consumes a lot of power. Dynamic wallpapers consume a little more power than static wallpapers, but not a lot. Does wallpaperengine consume a lot of power? Answer: Not much. 1. Dynamic wallpapers consume a little more power than static wallpapers, but not a lot. 2. Turning on dynamic wallpaper will increase the computer's power consumption and take away a small amount of memory usage. 3. Users do not need to worry about the serious power consumption of dynamic wallpapers.

How to check wallpaper subscription records? Many users have made a large number of subscriptions on this software, but may not know how to query these records. In fact, you only need to operate it in the browsing function area of the software. Where are wallpaperengine subscription records? Answer: In the browsing interface. 1. Please start the computer first and enter the wallpaper software. 2. Find the Browse tab icon in the upper left corner of the application and click it. 3. In the "Browse" interface, you will see an overview of various wallpapers and feeds. 4. Enter the keywords you want to search in the search box in the upper right corner. 5. Relying on the search results, you can find the source information of the wallpaper subscription. 6. Click on the corresponding feed to enter its web page. 7. Ordering
