#include<iostream>
using namespace std;
void BubbleSort(int ar[10], int n, int & compare_costs, int & swap_costs)
{
int t; int swaps=-1;
while(swaps)
{
swaps++;
compare_costs++;
for(int i=0; i<n-1; i++)
if(ar[i] > ar[i+1])
{
t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t;
swap_costs=swap_costs+5;
}
}
}
int main()
{
int n, compare_costs, swap_costs;
int ar[10];
int *bs = new int[n];
int *ss = new int[n];
int *qs = new int[n];
srand(time(0));
for(int i=0; i<n-1; i++)
*(bs+i) = *(ss+i) = *(qs+i) = 1+rand()%100;
for(int i=0; i<n-1; i++)
cout<< *(bs+i)<< " "; //prints random number before sort
BubbleSort(bs,n,compare_costs,swap_costs);
cout<<endl;
for(int i=0; i<n-1; i++)
cout<< *(bs+i)<< " "; //"SUPPOSE TO" print number after sort
}
Lines 5, 6, 8 - swaps is initialized to -1, then it's incremented to 0 regardless of whether there are any swaps, at which time the loop will exit.
lines 20, 32, 3, 9, 14. compare_costs and swap_costs are declared in line 20 but never initialized, passed by reference in line 32, then used in your function. You need to initialize them somewhere or get rid of them. Don't assume they'll be initialized to 0.
Last edited by VernonDozier; Aug 30th, 2008 at 4:28 pm.
Reputation Points: 2614
Solved Threads: 687
Posting Expert
Offline 5,374 posts
since Jan 2008