Bubble Sorting is really very simple. Well, if you have 15 minutes, maybe you will write it out soon. Really, I believe in you, and maybe you may think about it quite well, here, I only use this blog to record the Bubble Sorting I know.
Why is this name used for Bubble sorting?
You can think about the bubbles in the pond, floating from the bottom to the top, whether it is a process of small or larger, This is a physical knowledge, needless to say, do not know, go back to junior high school kicben, so is the bubble that floated to the water the biggest? This is the reason for the name bubble. It is the biggest that floated to the top, of course, you don't think that bubble can only be sorted from small to large. Big and small are a relative concept ~
Idea of Bubble Sorting (sorting from small to large)
1: Compare adjacent elements. If the first element is smaller than the second element, swap it
2: perform the same operation on each adjacent element, from the first pair to the last pair.
3: After completing step 1, the largest element has floated to the top position, removed the last element, and re-executed the above step. If no exchange occurs during the comparison of all adjacent elements, sorting is completed.
Time complexity of Bubble Sorting
In the best case, the elements to be sorted are already in the ordered state. Here we only need to compare the elements with n-1 times, and we do not need to exchange the elements, therefore, the best time complexity is O (n)
The worst case is that the elements to be sorted are in the reverse order, so each cycle needs to be compared with the n-i-1 times, according to the mathematical knowledge (high school) we can know that the total number of comparisons required is n * (n-1)/2. Three mobile exchange operations are required for each soft ratio, therefore, the total number of mobile exchange operations is 3n * (n-1)/2. therefore, the total time complexity is O (n ^ 2)
Therefore, the average time complexity of the algorithm is O (n ^ 2)
Spatial complexity of Bubble Sorting
The space complexity is a good computing. You can see it at a Glance. A variable is used in the entire program for exchange. Therefore, the space complexity is O (1), but not necessarily, the following describes how to reduce the space complexity to 0.
Stability of Bubble Sorting
Stability. This is also obvious. It is stable. If two adjacent elements are equal, they will not be exchanged unless it is your intention ~~~
Common bubble sort source program
[
void BubbleSort(int *a ,int N) { bool flag=true; int tmp; for(int i=0;i<N;i++) { for(int j=0;j<N-i;j++) { if(a[j]>a[j+1]) { tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; flag=false; } } if(flag) break; } return ; } void BubbleSort(int *a ,int N){ bool flag=true; int tmp; for(int i=0;i<N;i++) { for(int j=0;j<N-i;j++) { if(a[j]>a[j+1]) { tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; flag=false; } } if(flag) break; } return ;}
Bubble sorting with a faster space complexity of 0
void BubbleSort(int *a ,int N) { bool flag=true; for(int i=0;i<N;i++) { for(int j=0;j<N-i;j++) { if(a[j]>a[j+1]) { a[j]=a[j]^a[j+1]; a[j+1]=a[j+1]^a[j]; a[j]=a[j+1]^a[j]; flag=false; } } if(flag) break; } return ; } void BubbleSort(int *a ,int N){ bool flag=true; for(int i=0;i<N;i++) { for(int j=0;j<N-i;j++) { if(a[j]>a[j+1]) { a[j]=a[j]^a[j+1]; a[j+1]=a[j+1]^a[j]; a[j]=a[j+1]^a[j]; flag=false; } } if(flag) break; } return ;
} The above Code uses an exception or operation to reduce the complexity of the algorithm space.