Array [bubble sort]

Source: Internet
Author: User

 

1 # include <iostream. h>
2 void PopSort (int array [], int n); // function declaration
3 void main ()
4 {
5 // test the Function
6 int array [10] = };
7 PopSort (array, 10 );
8 for (int I = 0; I <10; I ++)
9 {
10 cout <array [I] <"\ t ";
11 if (I + 1) % 5 = 0)
12 {
13 cout <endl;
14}
15}
16}
17 /************************************** ***************
18 * function name: PopSort (int array [], int n)
19 * parameters:
20 * int array [], array data to be sorted
21 * int n, number of sorted data, that is, array size
22 * Function Description: uses the Bubble Method to sort arrays in ascending order.
23 *************************************** **************/
24 void PopSort (int array [], int n)
25 {
26 int I, j;
27 int temp; // intermediate variable
28 for (I = 0; I <n; I ++) // for (I = 1; I <n-1; I ++)
29 {
30 for (j = 0; j <n-i-1; j ++)
31 {
32 if (array [j]> array [j + 1]) // j <n-i-1
33 {
34 // data exchange
35 temp = array [j];
36 array [j] = array [j + 1];
37 array [j + 1] = temp;
38}
39}
40}
41}

 

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.