I have the next problem: My program reads two arrays and multiplies each element of the first array with each of the 2nd array. I have to count the number of results smaller than a given number. My code works correctly but i need to find a faster algorithm.
Here is my code:
void sort(int *array, int length)
{
int index, jndex = 0, aux, compElPoz;
for(index = 1; index < length; index++)
{
jndex = index - 1;
compElPoz = index;
while(array[compElPoz] < array[jndex])
{
aux = array[compElPoz];
array[compElPoz] = array[jndex];
array[jndex] = aux;
if(jndex > 0)
jndex--;
compElPoz--;
}
}
}
int main()
{
unsigned int n, in, jn, nr = 0, p, m;
scanf("%u %u", &n, &p);
int ar[n];//1st array
for(in = 0; in < n; in++)
{
scanf("%u", &ar[in]);//reading the 1st array
}
scanf("%u", &m);
int arr[m];//2nd array
for(in = 0; in < m; in++)
{
scanf("%u", &arr[in]);//reading the 2nd array
}
sort(arr, m);//sorting the 2nd array
for(in = 0; in < n; in++)
{
for(jn = 0; jn < m; jn++)
{
if(ar[in] * arr[jn] < p)
nr++;
else
break;
}
}
printf("%d", nr);
return 0;
}
So i have to read ar[] and arr[] and p. This is an example:
n = 5
p = 99
ar[5] = {1, 2, 3, 4, 5}
m = 2
arr[2] = {34, 25}
The program will print 5 because 1 * 34 < 99, 1 * 25 < 99, 2 * 34 < 99, 2 * 25 < 99, 3 * 25 < 99.