Swoopy:
That is not the actual program, but a quick sample showing the problem, I typed it fast here, and I forgot to place "<<"
anyway here is my actual program, now I am passing the vector by refrance.
Code:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
long int size = 10000;
void print(vector <long int> &arr, int size);
void h_ify(vector <long int> &arr,int i);
void make(vector <long int> &arr);
void sort_me(vector <long int> &arr);
int left(int i) ;
int right(int i) ;
///////////////////////////////
int main()
{
vector <long int> a ;
srand((unsigned)time(0));
for (int i = 0; i <= size; i++){
int x = rand();
a.push_back(x);
}
sort_me(a);
system ("pause");
return 0;
}
///////////////////////////////
void print(vector <long int> &a, int size) {
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
///////////////////////////////
int parent(int i)
{
if(i==1)
return 0;
if(i%2==0)
return ( (i / 2)-1);
else
return ( (i / 2));
}
int left(int i) {
return (2 * i) + 1;
}
int right(int i) {
return (2 * i) + 2;
}
///////////////////////////////
void h_ify(vector <long int> &a,int i) {
int great;
int l = left(i);
int r = right(i);
if ( (a[l] > a[i]) && (l < size))
great = l;
else
great = i;
if ( (a[r] > a[great]) && (r < size))
great = r;
if (great != i) {
int temp = a[i];
a[i] = a[great];
a[great] = temp;
h_ify(a, great);
}
}
///////////////////////////////
void make(vector <long int> &a) {
for (int i = (size - 1) / 2; i >= 0; i--) {
h_ify(a, i);
// print(a, size);
}
}
///////////////////////////////
void sort_me(vector <long int> &a) {
make(a);
for (int i = size; i > 0; i--) {
int temp = a[0];
a[0] = a[size - 1];
a[size - 1] = temp;
size = size - 1;
h_ify(a, 0);
}
}
This works, only when size is smaller then about 3500 anything larger the program crashes, why and how can I fixt this problem ?