C-language Bubble Sorting Algorithm and Bubble Sorting Algorithm

Source: Internet
Author: User

C-language Bubble Sorting Algorithm and Bubble Sorting Algorithm

#include <stdio.h>

int main (void) {
Ranch
int a [5];
printf ("please input sort number:");
int k;
for (k = 0; k <5; k ++)
{
scanf ("% d", & a [k]); // int type should add ampersand to get address
}
// Start sorting
int n = sizeof (a) / sizeof (a [0]);
printf ("% d number of arrays \ n", n);
Ranch

int w, j; // 5 numbers to compare 5-1 = 4 times 3》 1
for (w = 0; w <n-1; w ++) {
// printf ("w =% d \ n", w); loop 4 times
for (j = 0; j <n-1-w; j ++) {
if (a [j]> a [j + 1]) {
// Large back
int temp;
temp = a [j];
a [j] = a [j + 1]; // put the smallest
a [j + 1] = temp;
}
}
}
// output

int i;
printf ("The array is initialized to:");
for (i = 0; i <n; i ++) {
printf ("% d", a [i]);
}
printf ("\ n");
return 0;
} 



Related Article

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.