hi, i created a program to use bubblesort to sort numbers in an array, but when i try to display the new array, nothing appears on the command prompt. after some exploring, i figured out that it gets stuck in one of the loops in the sorting function. here is the code:

#include <iostream>
using namespace std;
void swap ( int &a, int &b )
{
     a = a ^ b;
     b = a ^ b;
     a = a ^ b;
}
void PrintArray ( int *in, int l )
{
     for ( int i = 0; i < l; i++ )
     {
         cout << in[i];
     }
}
void BubbleSort ( int *in, int l )
{
     int n = l - 1;
     int t = 1;
     int i;
     while ( t = 1 )
     {
           t = 0;
           for ( i = 0; i < n; i++ )
           {
               if ( in[i] > in[i+1] )
               {
                    t = 1;
                    swap (in[i],in[i+1]);
               }
           }
           
     }
}
int main()
{
    int array[5] = {23,11,65,34,1};
    BubbleSort (array,5);
    PrintArray (array,5);
    cin.get();
}

can someone please tell me what i did wrong?

Edited 6 Years Ago by mebob: code changes

Line 21 doesn't do what you want it to do. Look closely.

PS: Such implementation of swap is very dangerous. In this program it's all right, but in general it may really hurt you.

alright, i ran into another problem. the array doesnt get changed by the bubblesort function. i know its probably an obvious solution, but i am very oblivious.

When i is 4 you are testing beyond the range of your array if ( in[i] > in[i+1] ) There is no in[5] as your code is using.

This article has been dead for over six months. Start a new discussion instead.