Basic ideas
If the order is sorted from small to large, the sorted array is traversed, and if the current value is greater than the subsequent value then it is exchanged and traversed continuously until no exchange action occurs. The best time for bubbling sorting is O (n), and the worst time complexity is O (N2), so the average time complexity of the bubble sort is O (N2), and the bubble sort does not change the order of the same elements, so it is a stable sort algorithm.
Implementation code
#include <iostream>using namespacestd;intMain () {intmydata[Ten] = {7,3, A, $, +, -, -, -, +,5 }; intTemp_data =0, Count =1;//record whether the interchange has not occurred, if a trip bubbling without exchange occurs then exits while(count) {count=1; for(inti =0; I <Ten-1; i++) { if(Mydata[i] > mydata[i +1]) {Count++; Temp_data= Mydata[i +1]; Mydata[i+1] =Mydata[i]; Mydata[i]=Temp_data; } } if(Count = =1) {Count=0; } } for(inti =0; I <Ten; i++) {cout<< Mydata[i] <<" "; } cout<<Endl; System ("Pause"); return 0;}
Sort the bubble sort algorithm (Bubble sort)