I am trying to run a bubble sort algorithm which sorts an array in an ascending order but it comes out with segmentation fault in the online compiler and I can't figure out what's going wrong in there because I think that an element in an array should have a size of four, but after I try I couldn't find the solution. Can someone help me to have a look?
#include <iostream>
#include <array>
using namespace std;
void bubble_sort(int arr[]);
void printArray(int arr[]);
int main()
{
int arr[] = {10, 4, 2, 8, 11, 15};
bubble_sort(arr);
printArray(arr);
// cout<<sizeof(arr)<<endl;
return 0;
}
void bubble_sort(int arr[])
{
for (int i = 0; i < sizeof(arr) / 4; i++)
{
for (int j = 0; i < ((sizeof(arr) / 4) - 1); j++)
{
int temp;
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[])
{
for (int i = 0; i < (sizeof(arr) / 4); i++)
{
cout << arr[i] << endl;
}
cout << "\n";
}