Recently in the interview fresh students, out of a few simple face questions, bubbling method of sorting is one of them. Many people find it easy to bubble up and neglect it. Search on the internet, but also spread a lot of wrong wording. I have written it again, it is a review.
The C + + code is as follows:
#include"stdafx.h"/************************************************************************//*Bubble Sort*//************************************************************************/voidBubblesort (intData[],intN) { for(intI=0; i<n-1; i++) { BOOLExchange =false; for(intj=0; j<n-i-1; J + +) { if(Data[j] > data[j+1]) { intTMP =Data[j]; DATA[J]= data[j+1]; Data[j+1] =tmp; Exchange=true; } } if(!Exchange) Break; }}int_tmain (intARGC, _tchar*argv[]) { intdata[Ten]={- -, -, -3,0, the, +,8, $,12341,0}; Bubblesort (data,Ten); return 0;}
Note Two questions:
1. Bubble Sorting comparison is the next two elements, rather than the head element and the following several elements compared to some of the online writing errors, in the final analysis, they do not actually go hand-written side bubble sort, take it for granted;
2. Note that the condition of the loop end is not necessarily always compared until the end of the program, but when there is no exchange operation during the comparison process, the program can be terminated.
Bubble Method Sort