C + + bubble sort Sample sharing _c language

Source: Internet
Author: User

Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The name of this algorithm is because the smaller elements will slowly "float" through the exchange to the top of the series, hence the name. Because of the simplicity of bubble sorting, it is often used to introduce the concept of algorithms to students who are getting started with computer programming.

Copy Code code as follows:

#include <stdio.h>
int swap (int *a, int *b)
{
*a = *a + *b;
*b = *a-*b;
*a = *a-*b;
return 0;
}

int Mao_pao (int *a, int n)
{
int I, j, Flag;
for (i = n-1, flag = 1; i > 0 && flag; i.)
{
Flag = 0;
for (j = 0; J < i; ++j)
{
if (A[j] > a[j + 1])
{
flag = 1;
Swap (A + j, a + j + 1);
}
}
}
return 0;
}

int main (int argc, const char *argv[])
{
int i;
int a[] = {7,8,6,2,9,4,3,1,5,10};
Mao_pao (A, 10);
for (i = 0; i < ++i)
{
printf ("%4d", A[i]);
}
printf ("\ n");
return 0;
}

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.