简体   繁体   中英

bubble sort broken

I'm trying to code a bubble sort. I'm unable to find the error, I think it has to do with swapping. Can someone tell me where the bug is? It's throwing some unwanted elements at the end of the array.

#include <iostream>
#include <windows.h>
using namespace std;

void swap(int* a,int* b) {
    int *c;
    c = a;
    a = b;
    b = c;
    return;
}

int main()
{
    int array[4], a = 0;

    cout << "Enter 5 numbers to be bubble sorted" << endl;

    for (a = 0; a <= 4; a++)
    {
        std::cin >>array[a];
        Sleep(1000);
    }

    for (int b = 0; b <= 4; b++)
    {
        for(int f = 4;f >= b; f--)
        {
            if (array[f] < array[f-1])
            {
                swap(array[f],array[f-1]);
            }
        }
    }

    for(int d = 0; d <= 4; d++)
    {
        cout  << '\n';
        cout << array[d] << '\n';
    }

    return (0);
}
  1. The maximum index in your for loop is 5, but the size of the array is only 4. Accessing array[4] is likely to modify other local variables, such as a , b , and f .

  2. Your swap is never invoked in your code. array[i] returns int& and your invocation of swap actually calls std::swap .

  3. using namespace std is not a good habit, in my opinion.

You are getting confused over array sizes. It's actually really simple, you want an array of size 5, so just use 5 in your code everywhere

int array[5]; not int array[4];

for (a=0; a<5; a++) not for (a=0; a<=4; a++)

The second one isn't wrong, it's just easier to understand if you always use 5 instead of a mix of 4 and 5 .

Your swap function doesn't work and is not being called correctly. Your version swaps pointers not what is being pointed at. One of the more common things that newbies get wrong about pointers is getting confused about the pointer and what the pointer is pointing at. Here's how it should look:

void swap(int* a,int* b) {
    int c;
    c=*a;
    *a=*b;
    *b=c;
}

Finally you are calling the swap function wrongly. Your swap function uses pointers so you have to call it with pointers:

swap(array[f],array[f-1]);

should be

swap(&array[f],&array[f-1]);

If you say that "Its throwing some unwanted elements at the end of the array", I guess that the problem is in indexing. The inner loop iterates from the end of array down to b (inclusive), but you compare the element with the previous one. While the first iteration of the outer loop ( b = 0 ) the inner loop will iterate over 4, 3, 2, 1, 0 . In the last moment it would compare array[0] vs array[-1] . Even if the exception is not thrown, god knows what does this memory location contain (and I guess that the value is greater than the minimum element you use as the input). At the end of the day your smallest element goes to the location array[-1] (very dangerous...), and the garbage goes to the (possibly) last element of your array.

Another issue is that the last "element" of your array ( array[4] ) is actually the memory location where the variable a (most probably) is stored. When you read the last element from the stream ( a = 4 ), you override this variable with the input value. What happens next? a++ . The "last element" is being incremented. If by accident it is larger than 4, you exit the loop.

There are many other issues with your code, but these are the most probable reasons of observed behavior.

Change array[ 4 ] to array[ 5 ] and change the condition of inner loop from for(int f=4;f>= b ;f--) to for(int f=4;f>= (b+1) ;f--)

#include<iostream>
#include<windows.h>


using namespace std;
void swap(int* a,int* b) {
  int *c;
  c=a;
  a=b;
  b=c;
  return;
}
int main(){

int array[5],a=0;

  cout<< "Enter 5 numbers to be bubble sorted"<<endl;


  for (a=0; a<=4; a++)
{
      std::cin >>array[a];
      Sleep(1000);

}
for (int b=0;b<=4;b++)
{
  for(int f=4;f>=(b+1);f--){
    if (array[f]<array[f-1])
    {
      swap(array[f],array[f-1]);
    }


  }
}
for( int d=0; d<=4;d++){
  cout  << '\n';
  cout << array[d]<< '\n';
}
return (0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM