/**
* helpers.c
*
* Helper functions definitions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
// Binary Search implementation
int start = 0;
int end = n - 1;
while (start <= end)
{
int mid = (start + end) / 2;
if (values[mid] == value)
{
return true;
}
else if (value < values[mid])
{
end = mid - 1;
}
else if (value > values[mid])
{
start = mid + 1;
}
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// Counting Sort
//find min and max
int min = values[0];
int max = values[1];
//int range = max - min;
for (int m = 0; m < n; m++)
{
if (min > values[m])
{
min = values[m];
}
else if (max < values[m])
{
max = values[m];
}
}
//index array initialization
int index[(max - min)];
for (int j = 0; j <= (max - min); j++)
{
index[j] = 0;
}
//index counting
for (int p = 0; p <= (max - min); p++)
{
index[(values[p] - min)]++;
}
//sumCount array initialization
int sumCount[(max - min)];
sumCount[0] = index[0];
for (int c = 1; c <= (max - min); c++)
{
sumCount[c] = sumCount[c-1] + index[c];
}
//sortedInput
int sorted[n];
for (int s = 0; s < n; s++)
{
sorted[(sumCount[(values[s] - min)]) - 1] = values[s];
sumCount[(values[s] - min)]--;
}
//values arrray modification
for (int u = 0; u < n; u++)
{
values[u] = sorted[u];
}
return;
}
When I run with few numbers it works. I already tested by printing the process of counting sort algorithm but when I run (./generate 1000 50 | ./find 127) it returns a "Segmentation Fault"
please help me