Sort the bubble sort algorithm (Bubble sort)

Source: Internet
Author: User

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)

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.