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 use java Bubble sorting algorithm

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

Share

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

This article mainly explains the "java bubble sorting algorithm how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java bubble sorting algorithm how to use" it!

Bubble sorting is a simple sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence is repeated until there is no need for swapping, that is, the sequence has been sorted. The algorithm gets its name because smaller elements slowly "float" to the top of the sequence by swapping.

1.1 algorithm description

Compare adjacent elements. If the first one is bigger than the second, exchange them for two.

Do the same for each pair of adjacent elements, from the first pair to the last pair, so that the last element should be the largest number.

Repeat the above steps for all elements except the last one

Repeat steps 1-3 until the sorting is complete.

1Candle *

2 * @ description: bubble sort

3 * Bubble sorting only manipulates two adjacent data. Each bubbling operation compares the two adjacent elements to see if they meet the size requirements.

4 * if you are not satisfied, let them swap. A bubble moves at least one element to where it should be, repeating it n times

5 * the sorting of n pieces of data is completed.

6 * /

7public class BubbleSort {

8 public void bubbleSort (Integer [] arr, int n) {

9 if (n

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