I'm attempting to implement the bubble sort algorithm using recursion to sort an array of integers. However, I keep encountering a segmentation error when I compile and run the code. I've tried debugging it but I can't find the problem.
#include <stdio.h>
#define ARR_SIZE 10
//primitive function
void bubble_sort(int array[]);
//sort the number array using bubble sort
int main(void)
{
int array[] = {4,2,9,3,2,4,9,10,20,4};
bubble_sort(array);
for (int i = 0; i < ARR_SIZE ; i++)
{
printf("%i\n", array[i]);
}
}
void bubble_sort(int array[])
{
int swap_counter = 0;
for (int i = 0; i < ARR_SIZE - 2; i++)
{
if (array[i] >= array[i + 1])
{
swap_counter++;
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
if (swap_counter != 0)
{
bubble_sort(array);
}
else
{
return;
}
}
else { return }seems pretty pointless since that's going to happen anyway at the end of the function.