Hi I'm implementing bubblesort in C and got stuck with something that I couldn't figure out.
#include <stdio.h>
#include <time.h>
void main(){
int i;
int j;
int vahe;
int sisse;
int lst[50];
printf("Enter how many elements you want to sort:");
scanf("%d",&sisse);
srand(time(NULL));
for(i=0;i<sisse;i++){
lst[i]=rand() % 1000;
}
for(i=sisse-2;i>=0;i--){
for(j=0;j<=i;j++){
if(lst[j]>lst[j+1]){
vahe=lst[j];
lst[j]=lst[j+1];
lst[j+1]=vahe;
}
}
}
printf("Result: ");
for(i=0;i<sisse;i++){
printf("%d ",lst[i]);
}
}
in this code i can give more than 50 elemnts to sort and it works for 53 elements. if I give 54 the last one isn't sorted. Why I'm able to do this and how to fix it so that i can only give in as much elements as the size of an array?