pass sorting

I need my program to look like this. I kinda started right but I'm stuck. I'm not sure what to do next. please help

Thanks in advance




Data items in original order
   2   6   4   8  10  12  89  68  45  37

After pass 0:   2  4  6  8 10 12 68 45 37 89
After pass 1:   2  4  6  8 10 12 45 37 68
After pass 2:   2  4  6  8 10 12 37 45
After pass 3:   2  4  6  8 10 12 37
After pass 4:   2  4  6  8 10 12
After pass 5:   2  4  6  8 10
After pass 6:   2  4  6  8
After pass 7:   2  4  6
After pass 8:   2  4

Data items in ascending order
   2   4   6   8  10  12  37  45  68  89
Number of comparisons = 45





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
using namespace std;

#include <iomanip>

int main() {

    const int arraysize = 10;
    int a [arraysize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
    int hold;
    
    cout << "Data in Original Order" << endl;
    for (int i=0; i <arraysize; i++)
        cout << setw(4) << a[i];
        cout << endl << endl;
    for (int pass=0;pass < arraysize-1;pass++)
        for (int j=0; j<arraysize-1;j++)
            if ( a[ j ] > a[ j + 1 ] ) {
            hold = a[ j ];
            a[ j ] = a[ j + 1 ];
            a[ j + 1 ] = hold;
        cout <<"After Pass 0:";
        for ( int k = 0; k < arraysize; k++ )
        cout << setw( 4 ) << a[ k ];
        cout << endl;
                


    return 0;
            }
}
Hi,

With braces and indenting - you need to fix these up.

It's good practice to always use braces in control flow (loops, if etc.) even if there is only 1 statement.

In your code, the intention is shown with the indenting, but there are no braces.

You should be able to get your IDE to do this for you, automatically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
//using namespace std; avoid this

#include <iomanip>

int main() {

    const int arraysize = 10;
    int a [arraysize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
    int hold;
    
    std::cout << "Data in Original Order\n";

    for (int i=0; i <arraysize; i++) {
        std::cout << std::setw(4) << a[i];
        std::cout << "\n\n";  // use '\n' rather than std::endl
    }  // line up closing brace

    for (int pass=0;pass < arraysize-1;pass++) { // always use a brace
        for (int j=0; j<arraysize-1;j++) {
            if ( a[ j ] > a[ j + 1 ] ) {
              hold = a[ j ];
              a[ j ] = a[ j + 1 ];
              a[ j + 1 ] = hold;
            } // line up closing brace
        }
    }  // line up closing brace

        std::cout <<"After Pass 0:";

        for ( int k = 0; k < arraysize; k++ ) {
           std::cout << std::setw( 4 ) << a[ k ] << "\n";
           //std::cout << endl;
        }
                
    return 0;
           // }  
}


Also, provide meaningful names : a is not good for a container.

Hope all goes well :+)

Edit:

So when this is run, the placement of the '\n' is different to what you intended, so have a go a fixing that :+)
Last edited on
Topic archived. No new replies allowed.