bubble sort function... not working
Oct 23, 2008 at 4:33am UTC
trying to get the output of the bubble sort as a function in ascending order
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
#include "stdafx.h"
#include <iostream>
using namespace std;
void arrBubbleSort(int num[], int );
int main()
{
const int SIZE = 8;
int num[SIZE] = { 35, 10, 40, 5, 20, 15, 30, 25 };
cout << "numbers are: " ;
arrBubbleSort(num, SIZE);
return (0);
}
void arrBubbleSort(int num[], int size)
{
bool swap;
int temp;
do
{
swap = false ;
for ( int i = 0; i < ( size - 1 ); i++ )
{
if ( num[i] > num[i + 1] )
{
temp = num[i];
num[i] = num[i + 1];
num[i + 1] = temp;
swap = true ;
}
}
}while ( swap );
}
Last edited on Oct 23, 2008 at 4:34am UTC
Oct 23, 2008 at 6:21am UTC
I think you forgot to print the array.
It works with me with following code added right after arrBubbleSort.
1 2
for (int i = 0; i < SIZE-1; i ++)
cout << num[i] << endl;
Oct 24, 2008 at 1:20pm UTC
swap(var, var2) is a built-in var used to swap to variables.
I think you should use it instead of using a temp and use another variable for keeping track of whether you swapped.
Topic archived. No new replies allowed.