Bubble Sort Method

Source: Internet
Author: User

Example: Use the bubble sort method to sort 10 numbers in order from small to large.

int main ()
{
int i,j,t,a[10];
printf ("Input ten integers:\n");
for (i=0;i<10;i++)
scanf ("%d", &a[i]);
printf ("\ n");

for (j=0;j<9;j++)
for (i=0;i<9-j;i++)
if (a[i]>a[i+1])

{

T=a[i];
A[I]=A[I+1];
a[i+1]=t;
}
printf ("The sorted numbers:\n");
for (i=0;i<10;i++)
printf ("%4d", A[i]);

return 0;
}

Analysis:

    1. Compare the first number with the second number, in reverse order a[0]>a[1]. Then the second and third numbers are compared, and so on, until the first bubble is sorted by the number of n-1 and the nth number, the maximum number of results is placed in the last position.
    2. The second bubbling sequence of the number of previous n-1 results in the largest number being placed in the position of the first N-1 element.
    3. Repeat the above process, a total of n-1 order, the end of the sort.

Bubble Sort Method

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.