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 realize Bubble sorting and selective sorting by JavaScript

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "JavaScript how to achieve bubble sorting and selection sorting", detailed content, clear steps, and proper handling of details. I hope that this "JavaScript how to achieve bubble sorting and selection sorting" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

1. Bubble sorting algorithm

The bubbling algorithm, as its name implies, can treat each element in the sequence to be sorted as bubbles, assuming that the size of the bubbles is represented by the numerical value of the elements, so that the largest bubble (the largest element number) will rise first. This process is called bubbling. The key of the bubbling algorithm is to move the largest elements of the unsorted part back in turn, and form an ordered sequence at the end of the sequence from small to large. The bubble sort is shown in the following figure:

Schematic diagram of bubble sorting algorithm

The schematic diagram of the bubble sorting algorithm is shown above, in which each row represents a sort, and the sorting purpose is to find the maximum value. The maximum value is taken from the sequence to be sorted and placed in the red ball area, which represents the sequence that has been sorted. From the picture above, we can see that the elements bubbling out in each sorting are (17pr 12pr 9pr 5pr 1). The final sequence is (1, 5, 9, 12, 17).

2. Select sorting algorithm

Selective sorting refers to finding the smallest value in an unsorted sequence and putting it into a sequenced sequence until the number of elements in the unsorted sequence is zero. That is, all the elements are placed in a sequence that has been arranged in order. The key of this algorithm is to find the lightest (minimum numerical) element in the unsorted sequence and put it into the sorted sequence. Select the sorting algorithm as shown in the following figure:

Select sort diagram

The selection sorting diagram is shown in the figure above, and the key to the selection is to find the smallest value and put it into the sequence that has been sorted. In the image above, the unsorted (to be sorted) collection is the yellow part, and the sorted part is the green background part. Each behavior is sorted once, and the sorting purpose is to find the smallest element. As can be seen from the picture above, the lowest values selected are (1, 5, 9, 12, 17).

Bubble sorting implementation

JavaScript bubble sorting mainly uses JavaScript array digital objects to store the sequence to be sorted, traverses the array through circular statements, compares it with the following elements from the first element of the sequence to be sorted, and swaps if it is greater than the following elements, so after a traversal, the largest element will run to the end of the array array. The implementation code is described as follows:

Var arr1= [9, var, 1, 4, 13, 7, 8, 20, 23, 15]; var wlen1=arr1.length; var count1=0;// records the total number of execution times for (var item0witi).

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

Internet Technology

Wechat

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

12
Report