Bubble sort keeps crashing

Hello, I am trying to write a program which will sort an array of numbers into ascending order, here is my code

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
#include<iostream>
#include<cmath>
using namespace std;

int main(){
	int array[]={1, 5, 17, 3, 75, 4, 4, 23, 5, 12, 34, 34, 805, 345, 435, 234, 6, 47, 4, 9, 0, 56, 32, 78};
	
	
	int elements=sizeof(array)/sizeof(int);
	int change=1;
	
	
	while(change!=0){
		change=0;
		for(int i=0; i<elements; i++){
			if(array[i]>array[i+1]){
				int temp=array[i+1];
				array[i+1]=array[i];
				array[i]=temp;
				change+=1;
			}
			
		}
	}
	
	
	for(int i=0; i<elements; i++){
		cout<<array[i]<<", ";
	}
	
	return 0;
}

	


This compiles fine but when I run the .exe for the first time an error message comes up saying program has stopped working. If I run the program again without recompiling it seems to work as expected. Can anyone tell me what might be wrong?

Thank you.
i < elements - 1

so you don't access "unavailable" memory array
Thank you, this fixed my problem completely.
Registered users can post here. Sign in or register to post.