A few days ago, the Bubble Sorting was regarded as the selected sorting by mistake. Fortunately, Boyou corrected it in time. Let's talk about the difference between the two today.
Bubble Sorting
It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted.
The Code is as follows:
[]nums = {5,4,3,2,1 ( i = 0; i < nums.length; i++ ( j = 0; j < nums.length-i-1; j++ (nums[j] > nums[j+1 temp = nums[j] = nums[j+1 nums[j+1] = ( x = 0;x < nums.length;x++ System.out.print(nums[x]+"," System.out.print("\n" }
The output after each round of comparison is as follows:
4,3,2,1,5 3,2,1,4,5 2,1,3,4,5 1,2,3,4,5 1,2,3,4,5,
The algorithm flow of Bubble Sorting can be clearly understood from the output.
Select sort
Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged.
The Code is as follows:
[]nums = {5,4,3,2,1 ( i = 0; i < nums.length; i++ ( j = i+1; j < nums.length; j++ (nums[i] > temp = nums[i] = nums[j] = ( x = 0;x < nums.length;x++ System.out.print(nums[x]+"," System.out.print("\n" }
From the code, we can see that nums [I] is compared with every element in the array during each round of comparison.
The output after each round of comparison is as follows:
1,5,4,3,2 1,2,5,4,3 1,2,3,5,4 1,2,3,4,5 1,2,3,4,5,
From the output results, it is easy to see the difference between it and the bubble sort algorithm.
Thank you very much ~ Or I may never know that I was wrong. If any errors occur, please kindly advise me!