please help with bubble sort c++!! thank you

Dec 5, 2017 at 12:06am
hello there! I need some serious assistance, and I can't figure this out by myself. I am writing a program that is SUPPOSED to ask the user for the size of an array, and ask for number values until the size of the ray is met. then, it sorts the numbers according to size. this is supposed to be done with a bubble sort. Please help me correct my program to fix this! my grade is on the line in my c++ class. Thank you!

this is the code:

#include <iostream>
using namespace std;

#include <iomanip>


int main () {

int Counter=0;
int Number=0;
int Size;
int i=0;

int temp;




cout << "What is the size of the array? ";
cin >> Size;
int a[Size];



for (i=0;i<Size;i++)
{

cout<<endl<<"Element #"<<i<<"= ";
cin >> a[i];
}




for (i=1; i<Size;i++)
cout <<a[i]<<endl;
{
if
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp;
}


cout << a[i]<<endl;
system ("PAUSE");
return 0;
}
Dec 5, 2017 at 12:20am
Please use the proper tags to indent your code!

Let's see whatcha got...

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
#include <iostream>
using namespace std;

#include <iomanip>


int main () {
    int Counter=0;
    int Number=0;
    int Size;
    int i=0;

    int temp;




    cout << "What is the size of the array? ";
    cin >> Size;
    int a[Size];



    for (i=0;i<Size;i++)
    {
        cout<<endl<<"Element #"<<i<<"= ";
        cin >> a[i];
    }

    for (i=1; i<Size;i++)
        cout <<a[i]<<endl; // Do you mean for this to be here or within the brackets?
    {
    if  // You just leave this conditional if here without any conditions 
        temp= a[i];
        a[i]=a[i+1];
        a[i+1]=temp;
    }

    cout << a[i]<<endl;
    system ("PAUSE");
    return 0;
}


Your code is quite the mess right now. You need to document properly.
Dec 5, 2017 at 12:30am
i'm not really sure.... and sorry about the indentation. i dont know wht the condition is supposed to actually BE for the if statement and i THINK the for loop is supposed to be in the brackets?
Dec 5, 2017 at 12:31am
This is the bubble sort func... I hope you study it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// bubble sort that sorts from smallest to biggest
void bubble_sort(int array[], int sizeOfArray)
{
    for(int iter = 1; iter < sizeOfArray; iter++)
    {
        for(int index = 0; index < sizeOfArray - iter; index++) // flipping the < changes it to sort to biggest to smallest
        {
            // basically, you want to go through the list and compare each and
            // every one of the elements
            if(array[index] > array[index+1]) // If the current element is bigger than the next element...
            {
                int temp = array[index]; // we create a temp variable and store the contents of the that element into this
                array[index] = array[index+1]; // The next element becomes equal to the current element
                array[index+1] = temp; // the next element is now equal to the previous element
            }
        }
    }
}
Dec 5, 2017 at 12:31am
my code was maybe a line from finished and because the dev compiler stopped working my teacher tried to fix it and deleted my program!! :( i don't know how to get back to where i was when it was near completion
Dec 5, 2017 at 12:40am
so i rewrote the if statement like this
if (a[i] > a[i+1]) // You just leave this conditional if here without any conditions
{
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp;
}




and when i ran it, this happened
"
What is the size of the array? 4

Element #0= 8

Element #1= 3

Element #2= 4

Element #3= 7
3
4
7
-1669604688

"
Dec 5, 2017 at 12:47am
this is my updated code, but i still cant get it to work. it diplays the numbers, but sometimes more than once and also only bubbles some of them:

#include <iostream>
using namespace std;

#include <iomanip>


int main () {
int Counter=0;
int Number=0;
int Size;
int i=0;
int temp;




cout << "What is the size of the array? ";
cin >> Size;
int a[Size];



for (i=0;i<Size;i++)
{
cout<<endl<<"Element #"<<i<<"= ";
cin >> a[i];
}

for (i=0; i<Size;i++)
{ cout <<a[i]<<endl; // Do you mean for this to be here or within the brackets?

if (a[i] > a[i+1]) // You just leave this conditional if here without any conditions
{
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp;
}

cout << a[i]<<endl;}
system ("PAUSE");
return 0;}
Dec 5, 2017 at 12:48am
thank you for the bubble function! my browser didn't refresh, so i ddnt see it
Dec 5, 2017 at 12:49am

1
2
3
4
5
6
7
8
if (a[i] > a[i+1]) 
{
   temp= a[i];
   a[i]=a[i+1];
   a[i+1]=temp; // When it reaches the last element, it checks the next element.
                        // However, there is no next element. You are actually just accessing
                        // memory that should not be accessed.
}


Look at the code I have provided above and study it. iter prevents the the array from trying to access an index out of its bounds.
Dec 5, 2017 at 12:50am
awesome! thank you so much!! i will study it, you've been a great help
Topic archived. No new replies allowed.