Bubble Method Sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.