Bubble method Sorting

Source: Internet
Author: User

The idea of the foaming method is: Compare the adjacent two numbers, and the small one to the front.

It can be inferred that if there are n numbers, then the (n-1) wheel is compared and exchanged. In the first round to carry out (n-1) 22 comparison, in the J-Wheel to carry out (n-j) times 22 comparison.

The following 10 numbers are sorted from small to large:

#include <iostream>using namespace Std;int main () {int a[11];int I, J, t;cout<< "input numbers:\n"; for (I=1; i<11; i++) {cin>>a[i];} Cout<<endl;for (j=1; j<=9; J + +) {for (i=1; i<=10-j; i++) {if (A[i] > A[i+1]) {t = a[i];a[i] = a[i+1];a[i+1] = t;}}} cout<< "the sorted numbers: \ n"; for (I=1; i<11; i++) {Cout<<a[i]<<endl;} Cout<<endl;return 0;}

Bubble method Sorting

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.