Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement javascript Bubble sorting

Shulou Source: shulou.com Published: 2022-06-03 07:09:58 11月15日 Update

Today, I will talk to you about how to achieve javascript bubble sorting, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Compare all adjacent elements and swap them if the first is larger than the second.

2. After one round, the last number is the largest.

3. Sorting can be done by executing nMel 1 round.

4. The time complexity has two nested loops, O (n ^ 2).

Example

Array.prototype.bubbleSort = function () {for (let I = 0; I)

< this.length - 1; i += 1) { for (let j = 0; j < this.length - 1 - i; j += 1) { if (this[j] >

This [j + 1]) {const temp = this [j]; this [j] = this [j + 1]; this [j + 1] = temp;}; const arr = [5,4,3,2,1]; arr.bubbleSort (); after reading the above, do you have any further understanding of how to implement javascript bubble sorting? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Sort content complex maximum two elements can be passed complexity examples numbers time more knowledge articles industry information information channels channels one round into one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei NVidia Docker Shulou Technology