Bubble sorting and selective sorting are two simple sorting algorithms.
The idea of Bubble Sorting is:Compares adjacent elements. If the first is bigger than the second, exchange the two of them.Perform the same operation on each adjacent element, starting from the first pair to the last one at the end,Repeat the preceding steps for all elements,In this way, there will be fewer and fewer elements involved in each comparison until there is no need to compare any number. For the Bubble Sorting of N numbers, you need to compare the N-1, the I need to compare n-I times. Because many elements are repeatedly compared in the bubble sort, the time efficiency of this algorithm is not very high, and the average time complexity is O (n * n ).
Here is a reference code for Bubble Sorting:
// Description: Bubble Sorting // Author: hust_luojun // data: 2014_7_22 # include <iostream> using namespace STD; int main () {void bubble_sort (INT arrary [], int length); int arrary [] = {,}; cout <"the origin arrary is:" <Endl; int I; for (I = 0; I <10; I ++) cout <arrary [I] <"; bubble_sort (arrary, 10); I = 0; cout <Endl; cout <"the sorted arrary is:" <Endl; For (INT I = 0; I <10; I ++) cout <arrary [I] <"; return 0;} void bubble_sort (INT arrary [], int length) {int I; Int J; int K; for (I = 0; I <length-1; I ++) for (j = 0; j <length-1-i; j ++) {If (arrary [J]> arrary [J + 1]) {k = arrary [J]; arrary [J] = arrary [J + 1]; arrary [J + 1] = K ;}}}
The sorting mode starts from the first number and compares the number with each number after it. If the number is smaller than the number after it, it is not exchanged and remains in the original position: if the number is greater than the number next to it, find the smallest number behind it, so that it exchanges with it, so that the smallest number ran to the front, and so on, N number needs to be compared with the N-1. The time complexity of sorting is O (n * n), and the efficiency is not high. In practice, Bubble sorting and selection sorting are rarely selected, because there is a better Sorting Algorithm with time complexity, we only need to understand the ideas of these two algorithms. Here is the code for sorting clothes. For more information, see
// Description: Sorting by selection method // Author: hust_luojun // data: 2014_7_22 # include <iostream> using namespace STD; int main () {void selection_sort (INT arrary [], int length); int arrary [] = {,}; cout <"the origin arrary is:" <Endl; int I; for (I = 0; I <10; I ++) cout <arrary [I] <""; selection_sort (arrary, 10); I = 0; cout <Endl; cout <"the sorted arrary is:" <Endl; For (INT I = 0; I <10; I ++) cout <arrary [I] <"; return 0;} void selection_sort (INT arrary [], int length) {int I; Int J; int K; int T; for (I = 0; I <length-1; I ++) {k = arrary [I]; for (j = I + 1; j <length; j ++) if (arrary [J] <k) {T = arrary [J]; arrary [J] = K; k = T;} arrary [I] = K ;}}
algorithm journey-Bubble sorting and selection sorting