Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand JavaScript Bubble sorting and selective sorting

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to understand JavaScript bubble sorting and selection sorting", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to understand JavaScript bubble sorting and selection sorting"!

JavaScript Bubble sorting and selection sorting

Bubbling sort

Principle:

Compare two adjacent elements and swap the high-value elements to the right to the far right. Note that the core is adjacent.

Train of thought:

Compare the two adjacent numbers in turn, putting the smaller number in front and the larger number in the back. The largest number in the array will be at the bottom of the first round.

The second round: then among the remaining numbers in the array, the adjacent numbers are compared from the first number in turn, with the largest number at the bottom.

Repeat the steps until the sorting is complete.

Note: by the end of the penultimate round, there is still one number left in the last round, which must be the smallest, so there is no need to sort. That is, using only the length of the sorted array minus one round (arr.length-1)

Algorithm visualization:

The code is as follows:

Function ismaopao (arr) {/ / controls the number of comparison rounds for (var I = 0; I)

< arr.length - 1; i++) { //冒泡排序,两两交换,从头开始做比较(大数下沉) for (var j = 0; j < arr.length - 1 - i; j++) { //arr.length-1-i,因为前面的判断已经找到最大的值,就不需要与找到的大数做比较了 if (arr[j] >

Arr [j + 1]) {var a; a = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = a;} return arr } console.log (ismaopao ([6, 3, 4, 5, 2, 1])

The results are as follows:

Select sort

Train of thought:

Assume that the number of the first position of the array is the smallest, then compare it with each subsequent number, and swap the corresponding subscript for the value as long as a smaller one is found, note that it is the subscript. After the first round of searching, you can lock to the position of the minimum value (that is, find the subscript) and then swap the value.

The second round assumes that the number of the second position is the smallest, regardless of the first value of the array (because the first round is already the smallest), then swap the subscript with the following minimum value, lock it and then swap the value.

Repeat the steps until the sorting is complete.

Note: by the end of the penultimate round, there is still one number left in the last round, which must be larger than the previous number, so there is no need to sort. That is, using only the length of the sorted array minus one round (arr.length-1)

Algorithm visualization:

The code is as follows:

There is no package, you can package it yourself.

/ / Select sort, less than bubble sort times var arr = [5, 3, 4, 2, 1] var min = 0; / / define a subscript for (var I = 0; I) whose Min is an array

< arr.length - 1; i++) { min = i; for (var j = i + 1; j < arr.length; j++) { if (arr[min] >

Arr [j]) {min = j; / / exchange subscript, that is, exchange position}} var a = 0 / / now the value of min is the subscript corresponding to the minimum value of the array, / / and then exchanged with the value in the array corresponding to the subscript I, which changes with each round a = arr [min]; arr [min] = arr [I]; arr [I] = a;} console.log (arr)

The results are as follows:

At this point, I believe you have a deeper understanding of "how to understand JavaScript bubble sorting and selection sorting". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report