-
The following example is clearer and you can compare.
For example, the problem is: Sort 10 numbers from smallest to largest.
1: Choice method.
The so-called selection method is to first swap the smallest of the 10 numbers with a[0]; Swap the smallest numbers from a[1] to a[9] with a[1], and so on, and find the smallest of the unsorted numbers each time they are compared. So a total of 9 rounds were compared.
For example, if you use 4 numbers to sort, the first comparison is to swap the smallest number of the 4 numbers with a[0], and the second time: to swap the smallest number of the remaining 3 numbers with the smallest number except a[0] and a[1].
And so on. Based on this line of thought, the program is written as follows:
#include
void main()
void s(int arr,int n)int a[10],i;
printf("enter the array");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
s(a,10);
printf("the sorted array:");
for(i=0;i<10;i++)
printf("%5d",a[i]);
printf("");
void s(int arr,int n)int i,j,k,t;
for(i=0;i
void main()
int a[10];
int i,j,t;
printf("enter the array");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("");
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(a[j]>a[j+1])
t=a[j];a[j]=a[j+1],a[j]=t; }printf("the sorted array:");
for(i=0;i<10;i++)
printf("%5d",a[i]);
printf("");
-
The selection method is the simplest logical sorting method and is the fastest when there are few elements. The disadvantage is that the number of comparisons must be n 2 2 (because each time you have to compare them one by one to find the most valuable position).
Bubbling only has the worst-case case of n 2 2 comparisons (because it is usually arranged in the middle of the process), but the number of exchanges is more than that of the choice method (because it is adjacent data exchange, not directly in place). The number of selective exchanges is n - 1 in the worst caseBubbling is n 2 2.
The actual processing selection method is used a lot, and bubbling is a high-level algorithm. When there is a lot of data, the average processing time is shorter than that of selection, but it is much slower than that of o(n * logn) algorithms such as quick sort.
-
The bubble method, also known as bubble sorting, is a relatively simple sorting algorithm in the field of computer science. Bubbling sort is the process of repeatedly visiting the columns of elements to be sorted, then comparing two adjacent elements one by one, and swapping them if their order is wrong.
-
Bubbling sorting, which is commonly used in the C languageSorting algorithmsOne of them, which means an algorithm that sorts a set of numbers from largest to smallest or from smallest to largest.
The algorithm gets its name because the smaller the elements slowly "float" to the top of the sequence (ascending or descending) through exchange, like carbonated drinks.
The bubbles of carbon dioxide eventually rise to the top, hence the name "bubbling sorting".
Algorithm Stability:
Bubbling is the process of putting small elements in front or large elements in the back. A comparison is a comparison of two adjacent elements, and the exchange also takes place between these two elements. So, if the two elements are equal, they will not be exchanged again; If two equal elements are not adjacent, then even if the two are adjacent by the previous pairwise exchange, they will not be swapped at this time, so the order of the same elements does not change, so bubble sorting is a stable sorting algorithm.
-
Bubbling sort and choice sort are both stable sorting methods.
The difference is that the process of processing is different.
Bubbling is a comparison of adjacent pairs, swap the small ones up, and each trip of the comparison will get a minimum value. One by one, it's like bubbling, and it's more vivid. If, in a comparison, no value is found to be exchanged, the sorting is complete.
Select Sort is to select the smallest value from the queue to be sorted and place it after the queue that has been sorted.
For example, the queue to be sorted is: 6 3 2 5
Ascending (bubbling).
First trip: First comparison 3 6 2 5
Second comparison 3 2 6 5
Third comparison 3 2 5 6
Second trip: 2 3 5 6
The third trip: no exchange was found, and the sorting ended.
Sort of selection: 1st trip: 2 3 6 5 (pick 2 and put it in the first position) 2nd trip: 2 3 6 5 (pick 3 and put it in the 2nd position) 3rd trip: 2 3 5 6 (pick 5 and put it in the 3rd position).
-
1 For example, the above 5 numbers we arrange it in order from small to large, from the front to the back of the two places to compare the size, if the previous one is larger than the next one, the two will be reversed, 5 to 4 will be 5 and 4 transposition, and the 453215 will be larger than 3.
5 and 3 transposition.
Get 43521
And so on until then.
This moves the largest number to the end.
Then don't look at 5 left 4321
Then use the above method to move the 4 to the end.
Get 32145
In not looking at 45 left 321
I move 3 to.
Finally, and so on.
Ended up with 12345
This is the bubbling method, which is the easiest and fastest way to sort in computer programming.
In addition to this accident, I can also write many sorting methods, but they are not as efficient as the bubble method, as for why it is called the bubble method, you put these numbers up and look at 1234
5 takes the largest number 5 as the largest bubble, floats to the top, then 4 floats up again, and so on to get 5
Therefore, the figurative one is called the bubble method.
-
For example, the above 5 numbers are arranged in order from small to large, from front to back, two places next to each other compare the size, if the previous one is bigger than the last one, put both of them.
Swap, 5 to 4 is the 5 and 4 to the 5, and the 453215 is greater than 3.
5 and 3 transposition.
Get 43521
And so on until then.
This moves the largest number to the end.
Then don't look at 5 left 4321
Then use the above method to move the 4 to the end. Get.
In not looking at 45 left 321
I move 3 to.
Finally, and so on.
Ended up with 12345
This is the bubbling method, which is the easiest and fastest way to sort in computer programming.
There are a lot of sorting methods that I can write, but none of them are as efficient as the bubbling method.
As for why it's called the bubble method, you put these numbers up and look at them.
Think of the largest number 5 as the biggest bubble, float to the top, then 4 to float again, and so on. Get.
Therefore, the figurative one is called the bubble method.
From Encyclopedia.
The difference between strong flavor liquor and sauce-flavored liquor: >>>More
Like is not the same as love. To like someone is not necessarily to love him; But the premise of loving someone is that you must like him. It's easy to turn like into love, but it's hard to say like after you've loved. >>>More
The distinction between labor law and civil law: civil law is private law; Labor law is social law; Civil law refers to the general term of all legal norms that regulate property relations and personal relations between equal subjects; In order to protect the legitimate rights and interests of laborers, the Labor Law adjusts labor relations, establishes and maintains a labor system suited to the socialist market economy, and promotes economic development and social progress. >>>More
The Criteria for a Good Person (Good Man) Anyone who brings happiness, joy, freedom, and happiness to others is a good person (Good Man). Whoever leads others to the way of God is a good person (good man). Whoever helps others through difficult times is a good person (good person). >>>More
Haha, there are so many funny people, the only difference with Amway is that the name is different, everything else is the same, it's all rubbish.