Home >Java >javaTutorial >The difference between selection sort and bubble sort
Bubble sort:
The basic concept of bubble sort (BubbleSort) is: compare two adjacent numbers in sequence, put the decimal in front and the large number in the back. That is, in the first pass: first compare the first and second numbers, put the decimal first and the large number last.
Then compare the 2nd number and the 3rd number, put the decimal in front, and the large number in the back, and continue like this until the last two numbers are compared, and the decimal is placed Before, put the big numbers behind. This is the end of the first trip, leaving the largest number at the end. In the second pass: still start the comparison from the first pair of numbers (because it may be due to the exchange of the second number and the third number that the first number is no longer smaller than the second number), put the decimal in the front and middle, and put the large in the middle. After the numbers are played, the comparison is continued until the second-to-last number (the first-to-last position is already the largest). At the end of the second pass, a new maximum number is obtained at the second-to-last position (actually, in the entire sequence, the second largest number). Continue like this and repeat the above process until the sorting is finally completed. (Recommended learning: Java Video Tutorial)
Selection sorting:
The first time starts from the subscript 0, the number with the subscript 0 and Compare the next n-1 values; find the smallest or largest value and place it at the position with subscript 0; start the second comparison from subscript 1; query the remaining maximum or minimum value; place it at subscript 1 position; and so on; until the sorting is completed
Example:
public class MaoPao { public static void main(String[] args) { int arr[]={23,12,46,24,87,65,18,14,43,434,65,76}; int k=0; //冒泡排序 for(int i=0;i<arr.length-1;i++){ for(int j=0;j<arr.length-1;j++){ if(arr[j]<arr[j+1]){ int t=arr[j]; arr[j]=arr[j+1]; arr[j+1]=t; k++; } System.out.print("i="+i+"的第j="+j+"次交换\t"); for(int d=0;d<arr.length;d++){ System.out.print(arr[d]+"\t"); } System.out.println(); } } System.out.println("交换的次数为"+k); //选择排序 int l=0; for(int i=0;i<arr.length-1;i++){ for(int j=i+1;j<arr.length-1;j++){ if(arr[i]<arr[j]){ int t=arr[i]; arr[i]=arr[j]; arr[j]=t; l++; } System.out.print("i="+i+"的第j="+j+"次交换\t"); for(int d=0;d<arr.length;d++){ System.out.print(arr[d]+"\t"); } System.out.println(); } } for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+"\t"); } System.out.println("交换的次数为"+l); } }
It’s time to summarize their differences
(1) Bubble sorting compares two numbers in adjacent positions, while selection sorting compares in order to find the maximum or minimum value;
(2) Each round of bubble sorting After comparison, if the position is wrong, you need to change the position. Each round of selection sorting only needs to change the position;
(3) Bubble sorting is to find the position through numbers, and selection sorting is to find the position at a given position. Number;
For more Java-related technical articles, please visit the Java Development Tutorial column to learn!
The above is the detailed content of The difference between selection sort and bubble sort. For more information, please follow other related articles on the PHP Chinese website!