#include<stdio.h>
#include<stdlib.h>
void quicksort(int*,int,int);
int partition(int*,int,int);
void swap(int*,int*,int*);
int main()
{
int t,k,q,i,j,p,*a,*b,*sum,qi,c;
//printf("Enter no of test cases");
scanf("%d",&t);
for(i=0;i<t;i++)
{
p=0;
//printf("Enter no of chefs and no of queries");
scanf("\n%d %d",&k,&q);
a=(int*)malloc(k*sizeof(int));
b=(int*)malloc(k*sizeof(int));
sum=(int*)malloc(k*k*sizeof(int));
//printf("Enter satisfaction levels");
for(j=0;j<k;j++)
scanf("%d",&a[j]);
//printf("Enter motivation levels");
for(j=0;j<k;j++)
scanf("%d",&b[j]);
//calc sum[]
for(c=0;c<k;c++)
for(j=0;j<k;j++)
sum[p++]=a[c]+b[j];
free(a);
free(b);
quicksort(sum,0,--p); //partition,swap
for(j=0;j<q;j++)
{
//printf("\n qith lowest sum");
scanf("\n%d",&qi);
printf("%d",sum[qi-1]);
}
free(sum);
}
return 0;
}
void quicksort(int sum[],int low,int high)
{
int m;
if(low<high)
{
m=partition(sum,low,high);
quicksort(sum,low,m-1);
quicksort(sum,m+1,high);
}
}
int partition(int sum[],int low,int high)
{
int pivot=sum[low],i=low,j=high;
while(i<=j)
{
while(sum[i]<=pivot)
i++;
while(sum[j]>pivot)
j--;
if(i<j)
swap(sum,&i,&j);
}
swap(sum,&low,&j);
return j;
}
void swap(int sum[],int *i,int *j)
{
int temp;
temp=sum[*i];
sum[*i]=sum[*j];
sum[*j]=temp;
}
getting a runtime error(SIGSEGV) neither i am accessing an array which is out of bounds nor i am using a variable which i haven't initialised Also my main function returns 0. Not getting whats the cause of the error ?