Sorting

Hey guys. I can't seem to get this code to write. I think I'm passing my arrays wrong. I'm writing a sorting algorithm that is supposed to display the contents of an array after every pass with the sorting algorithm but I keep getting repeating numbers and nothing that is part of the actual array. Can I get any assistance?

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <stdio.h>

using namespace std;

void bubbleSort(int bubble[], int SIZE);
void selectSort();
const int SIZE = 8;

int main()
{
    int bubble[SIZE] = {2, 0, 4, 5, 7, 6, 1, 3};
    int select[SIZE] = {3, 4, 7, 0, 1, 2, 6, 5};

    for (int i = 0; i < SIZE; i++){
        cout << bubble[i];
        bubbleSort(bubble, SIZE);
    }
      cout << endl;
      cout << "\tNow the array will be sorting using the select sort algorithm.";
      cout << endl;

    for (int i = 0; i < SIZE; i++){
            cout << select[i];
    }


   return 0;
}

void bubbleSort(int bubble[], int SIZE)
{
    int temp;
    bool madeAswap;

    do
    {
        madeAswap = false;
        for (int k = 0; k < (SIZE - 1); k++)
        {
            if (bubble[k] > bubble[k + 1])
            {
                temp = bubble[k];
                bubble[k] = bubble[k + 1];
                bubble[k + 1] = temp;
                madeAswap = true;
                for (int k = 0; k < (SIZE); k++)
                    cout << bubble[k];
            }
        }
    }while (madeAswap);
}
Last edited on
Be careful of your name re-use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ g++ -Wall -Wextra -Wshadow foo.cpp
foo.cpp: In function ‘void bubbleSort(int*, int)’:
foo.cpp:31:39: warning: declaration of ‘SIZE’ shadows a global declaration [-Wshadow]
 void bubbleSort(int bubble[], int SIZE)
                                       ^
foo.cpp:8:11: note: shadowed declaration is here
 const int SIZE = 8;
           ^
foo.cpp:47:26: warning: declaration of ‘k’ shadows a previous local [-Wshadow]
                 for (int k = 0; k < (SIZE); k++)
                          ^
foo.cpp:39:18: note: shadowed declaration is here
         for (int k = 0; k < (SIZE - 1); k++)
                  ^
try cleaning up the output and looking just at the first part.
after doing nothing but output cleanup so I could see what it was doing, my output looked like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\c>a
0 2 4 5 7 6 1 3
0 2 4 5 6 7 1 3
0 2 4 5 6 1 7 3
0 2 4 5 6 1 3 7
0 2 4 5 1 6 3 7
0 2 4 5 1 3 6 7
0 2 4 1 5 3 6 7
0 2 4 1 3 5 6 7
0 2 1 4 3 5 6 7
0 2 1 3 4 5 6 7
0 1 2 3 4 5 6 7

        Now the array will be sorting using the select sort algorithm.
… etc


put a space after each array element, and an end of line after each full array to get it like this.
Last edited on
You can try this program. this is the easiest way to write shorting program
#include<iostream>
using namespace std;
class sorting
{
int a[100],n,rev,i,j,temp;
public:
void input()
{
cout<<"enter number:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter value:";
cin>>a[i];
}
}
void process()
{
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"sorted array:";
}
void show()
{
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
}
};
main()
{
sorting object;
object.input();
object.process();
object.show();
}
Topic archived. No new replies allowed.