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 write the code for bubble sorting in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about how to write the code for bubble sorting in Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Bubbling sorting principle

Compare two adjacent elements. If the first is larger than the second, swap their positions (ascending, descending, and vice versa).

Each pair of adjacent elements is compared from the beginning of the list to the end of the list. In this way, the element with the highest value completes the first round of bubbles by swapping "bubbles" to the end of the list.

Repeat the previous step to continue comparing adjacent elements from the beginning of the list. There is no need to compare the elements that have been bubbling out (you can compare them all the way to the end, and the elements that have bubbled to the end do not need to be exchanged even if they are compared, and the steps can be reduced without comparison).

Continue to compare from the list, and each round of comparisons will have one element "bubbling" successfully. The number of elements to be compared decreases in each round, until only one element is left without bubbling (there is no pair of elements to compare), and the list is sorted.

Bubble sorting process

Take the one-dimensional array as an example:

Int [] array = new int [] {5pct 33pl 22pl 6je 11}; first round bubbles

Figure ① shows the column chart of the starting order of the data in the first round of "bubbling". As long as the condition is satisfied: "if the former element is larger than the latter, the order of position will be exchanged, otherwise it will not be exchanged."

Array [0] = 55 > array [1] = 33, the conditions are satisfied, and the order of the positions of the exchange elements is met, as shown in figure ②.

Array [1] = 55 > array [2] = 22, the conditions are satisfied, and the order of the positions of the exchange elements is met, as shown in figure ③.

Array [2] = 55

< array[3]=66,条件不满足,不交换元素的位置顺序,如图③所示; array[3]=66 >

Array [4] = 11, the conditions are met, and the position order of the exchange elements is met, as shown in figure ④.

The first round of "bubbling" demonstration is shown in the figure:

Second round of bubbling

Figure ④ shows the starting sequence bar chart of the data in the second round of Bubble.

Array [0] 33 > array [1] = 22, the condition is satisfied, and the position order of the exchange elements is met, as shown in figure ⑤.

Array [1] 33

< array[2]=55,条件不满足,不交换元素的位置顺序,如图⑤所示; array[2]55 >

Array [3] = 11, the conditions are met, and the position order of the exchange elements is met, as shown in figure ⑥.

The second round of "bubbling" demonstration is shown in the figure:

Third round of bubbling

Figure ⑥ shows the starting sequence bar chart of the data in the third round of "bubbling".

Array [0] = 22

< array[1]=33,条件不满足,不交换元素的位置顺序,如图⑥所示; array[1]=33 >

Array [2] = 11, the conditions are met, and the position order of the exchange elements is met, as shown in figure ⑦.

The third round of "bubbling" demonstration is shown in the figure:

Fourth round of bubbling

Figure ⑦ shows the starting sequence bar chart of the data in the fourth round of "bubbling".

Array [0] = 22 > array [1] = 11, exchange the position order of "22" and "11" when the condition is met, as shown in figure ⑧

The fourth round of "bubbling" demonstration is shown in the figure:

At this point, the process of bubbling sorting the array is complete!

Specific code implementation

BubbleSort class:

Public class BubbleSort {public static void sort (int array []) {/ / I indicates the number of "bubbles" and j represents the element index visited. / / in each round of "bubbling", j needs to "visit" the location of array.length-1 from the beginning of the list. For (int I = 0; I

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

Array [j + 1]) {int temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp;}

TestMain class

Import java.util.Arrays;public class TestMain {public static void main (String [] args) {int [] array = new int [] {55, 33, 22, 66, 11}; / / output array array System.out.print ("before sorting:"); System.out.println (Arrays.toString (array)) / call the sort method in the BubbleSort class to sort the array array BubbleSort.sort (array); / / output the bubbly sorted array array System.out.print (after sorting:); System.out.println (Arrays.toString (array));}}

The running results are as follows:

Before sorting: [55, 33, 22, 66, 11] after sorting: [11, 22, 33, 55, 66] these are all the contents of this article entitled "how to write the code for Java bubble sorting". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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