learning heap sort

i wrote this to begin learning heap sort algorithm(i know this doesn't sort anything) but everytime i complile the program crashes and i get the error "variable arr was corrupted" anyone can explain me why ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int main(){
	srand(time(NULL));
	int const size = 10;
	int arr[size];
	for (int i = 0; i < size; i++)arr[i] = rand() % 40 + 1;
	//print(arr, size);
	complete(arr, size);
}

void complete(int arr[],int size){
	for (int i = size; i>1; i--){
		swap(arr[1], arr[i]);
	}
}
for (int i = size; i>1; i--){
arr[size] is not a valid element. This is why you write for (int i = 0; i < size;.
Use for(int i = size - 1;
yes, sorry, brainfart i guess :)
Topic archived. No new replies allowed.