I have an agreement with bubble sorting!
1. initial bubble sorting
It was an interview that I attended the School Electronics Association. There is a problem in it, which requires the use of bubble sorting to arrange the data. Let me have a look. Silly, because I didn't learn it. In fact, many people have learned it, but my own learning method is wrong. I didn't work out this problem, and I didn't enter the Electronics Association. Later in my study, I always had doubts about this bubble sort, and never dared to face it. But now, I want to share with you. In fact, bubble sorting is very simple!
2. example solution
- Define a one-dimensional array, which stores int type data. It is required to output these data from small to large.
Do you think this question is very simple? Let's analyze it carefully!
Suppose that the data in a one-dimensional array is defined as follows: the large box is a one-dimensional array, and the small box is the stored element.
{1,5,7,4,6,2,8,3}
Should the output sequence be: 1,2,3,4,5,6,7,8
What should we do?
3. bubble sorting problem solving ideas
-
Bubble sorting consists of two for loops. The outer loop, which I call round, compares the number of elements. The inner loop, which I call times, is a pairwise comparison between elements. Cycle from inside to outside, first cycle times, number of cycles.
The first loop is a loop of all elements
- In the first round of cyclic comparison, we have to cycle 7 times, including the ratio of the 0th element to the 1st element, the ratio of the 1st element to the 2nd element, and so on. If the value on the left is smaller than that on the right, no exchange is required. Otherwise, the larger value is changed to the right. Finally, select the maximum value and put it on the far right.
- Then enter the second round of the cycle. At this time, we can ignore the rightmost maximum value and only compare the first seven elements. Then we need to cycle 6 times. The law of circulation is the same. Select the second largest element and put it in the second place on the right.
- When all the loops are finished, the elements are arranged from small to large!
be careful! be careful! be careful!
Why should I use such big words? Or three times! Because it's really important.
-
Q: what is the cycle end condition of the wheel?
Answer: the array elements start from 0, so we should note that the end condition of the round loop is less than the array length minus 1
-
Q: how do you know how many elements have been photographed? Is it OK to subtract the number of ordered elements from the length of the loop?
Answer: the number of cycles is the number of cycles. The loop end condition inside is, of course, the length of the array minus 1 and then minus the number of ordered elements!
-
Q: can I exchange elements directly?
A: of course not. Computers are not so smart. We need to define a third party.
4. source code display
- When the array is defined at the beginning:
public class Demon { public static void main(String[] args) { int[] arrys = new int[]{1,5,7,4,6,2,8,3}; /** *Bubble sorting makes the elements output from small to large. */ int temp = 0;//Third party appearance for(int i = 0;i < 8 - 1;i ++){//Wheel cycle for(int j = 0;j < 8 - i -1; j++){//Secondary cycle if(arrys[j] > arrys[j+1]){ temp = arrys[j]; arrys[j] = arrys[j+1]; arrys[j+1] = temp; } } } /** *Output array */ for(int i = 0;i < 8;i ++){ System.out.print(arrys[i] + " "); } } }
That's it!
5. bubble sorting advanced
What if I don't know the defined array length and want to package bubble sorting into a method?
so easy!
Direct source code:
public class Demon2 { public static void main(String[] args) { int[] arrys = new int[]{1,3,2,4,5}; PrintArrys(Rankarry(arrys));//Call method to sort elements } /** * Print array elements * @param arr */ public static void PrintArrys(int[] arr){ for (int i : arr) { System.out.println("The value of the array element is" + i); } } /** * Bubble sort from small to large * @param arrys * @return */ public static int[] Rankarry(int[] arrys){//Pass an array into the method int temp = 0;//The legendary third party for (int i = 0;i < arrys.length - 1; i++) {//Array name Length is the length of the array for (int j = 0;j < arrys.length - i -1;j ++){ if(arrys[j] > arrys[j+1] ){//If you want to sort from large to small, you only need to change the greater than sign to the less than sign temp = arrys[j]; arrys[j] = arrys[j+1]; arrys[j+1] = temp; } } } return arrys; } }
6. summary
- A careful understanding of how bubble sorting works will be of great help to future learning.
- Learn to bubble sort can be forced!
Hope that sharing is helpful to you and can keep going with bubble sorting