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 the Bubble sorting of Java

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

Share

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

This article mainly introduces the Java bubble sort how to achieve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java bubble sort how to achieve the article will have a harvest, let's take a look at it.

Bubble sorting Bubble Sort is a simple sorting algorithm in the field of computational science. Bubble sort moves the small elements forward or the large ones backward, comparing the knowledge between the two adjacent elements. If two elements are equal, they will not be swapped; if they are not adjacent, even if the first two are adjacent after swapping, they will not be swapped. Therefore, bubble sorting is a stable sorting algorithm.

I. the steps of bubbling sorting

1. Compare the two adjacent elements, and if the first is larger than the second, then exchange the two.

2. Do the same work for each pair of adjacent elements, from the first pair to the last pair. Therefore, the last digit must be the largest number.

3. Repeat the above steps for all elements, except for the last one, that is, length-1 times. The algorithm ends until there is no pair of numbers to compare.

Second, code examples

Public class BubbleSort {public static void bubbleSort (int [] array) {boolean flag = false;// records the exchange of values for (int I = 0; I) for whether or not a sorting process takes place.

< array.length-1; i++) { for (int j = 0; j < array.length-1-i; j++) { //相等就不进行交换,保证算法的稳定性。 if(array[j] >

Array [juni1]) {int temp = array [j]; array [j] = array [juni1]; array [juni1] = temp; flag = true }} if (! flag) {/ / if no exchange occurs, the whole array is ordered break;}

Third, debug command debugging

1. Break point and click before the number of rows that need the breakpoint.

2. Right-click Debug mode to run.

3. The F8 shortcut key executes the code in turn.

This is the end of the article on "how to achieve the bubbling sorting of Java". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve the bubble sorting of Java". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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