In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how Java achieves bubble sorting and optimization. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
What is Bubble sorting
Bubble sorting refers to repeatedly visiting the column of elements to be sorted, comparing two adjacent elements in turn, and swapping them if the order is wrong, such as from small to large. The work of visiting an element is repeated until there are no adjacent elements to swap, that is, the element column has been sorted. The algorithm gets its name because smaller elements slowly float to the top of the sequence (ascending or descending order) by exchange, just as carbon dioxide bubbles in carbonated drinks eventually float to the top, hence the name "bubbling sort".
Train of thought analysis
As an example, {5pr 3pr 9je 7J 1} requires that the elements of the sorted array are sorted from small to large. Compare the two adjacent numbers (blue) in turn, putting the smaller number in front and the larger number in the back. Each round of sorting can get the maximum number of participants in the comparison (red)
First round of sorting
5 / if the number is greater than the adjacent number, change places.
3,5,9,7,1
3,5,9,7,1
3,5,7,9,1
3 / the results of the first round of sorting
The second round of sorting
3,5,7,1,9
3,5,7,1,9
3,5,7,1,9
The result of the second round of sorting.
The third round of sorting
3,5,1,7,9
3,5,1,7,9
The result of the third round of sorting
The fourth round of sorting
3,1,5,7,9
The result of the fourth round ranking of 1pence, 3pence, 5jorn, 7pr, 9 / /
The code implements public class bubble {public static void main (String [] args) {int [] array = {5 int 3, 9, 7, 1}; bubbleSort (array);} / * * Bubble sorting * / public static void bubbleSort (int [] array) {int temp; / / length-1 sorting for (int I = 0; I)
< array.length-1; i++) { //数组中没有元素或者只有一个元素就无需排序 if(array==null || array.length < 2 ){ return; } //每进行一次排序后参与比较的数量减一 for (int j = 0; j < array.length - 1 - i; j++) { if (array[j]>Array [juni1]) {/ / interchange element position temp = array [j]; array [j] = Array [j + 1]; array [juni1] = temp;}} System.out.println (the result of the "th" + (iTun1) + "round sort is" + Arrays.toString (array)) } return;}} output the result of the first round of sorting is [3, 5, 7, 1, 9] the result of the second round of sorting is [3, 5, 1, 7, 9] the result of the third round of sorting is [3, 1, 5, 7, 9] the result of the fourth round of sorting is [1, 3, 5, 7, 9] Process finished with exit code 0 code optimization
According to the above algorithm, it is found that it is necessary to sort the array with length n for 1 round to calculate the final result, but not all arrays need nmer1 to wait for the final sorting result. For example, {1mem2, 3pr 5, 4} We find that this array only needs to be sorted once to get the results, so how to optimize the above code? You only need to judge whether there is an element swap position in a round of sorting to determine whether the sorting is complete. If there is no interchange of element positions after a round of sorting, the sorting is complete.
Public class bubblePlus {public static void main (String [] args) {int [] array = {1 int temp; (int I = 0; I); bubboSort (array);} / * * Bubble sort * / public static void bubboSort (int [] array) {int temp; / / determine whether there are elements to exchange boolean flag = false; for (int I = 0; I)
< array.length-1; i++) { if(array==null || array.length < 2 ){ return; } //每进行一次排序后参与比较的数量减一 for (int j = 0; j < array.length - 1 - i; j++) { if (array[j]>Array [juni1]) {/ / position exchange is changed to true flag = true; temp = array [j]; array [j] = Array [j + 1]; array [juni1] = temp }} if (! flag) {/ / position exchange does not occur, indicating that the sorting has been completed break } else {/ / position changes need to reset flag to false to facilitate the next round of judgment System.out.println (the result of the "first" + (item1) + "round is" + Arrays.toString (array)); flag = false;}} return }} optimized result output the result of the first round of sorting is [1, 2, 3, 4, 5] Process finished with exit code 0 Thank you for reading! This is the end of this article on "how to achieve bubble sorting and optimization in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.