I'm getting a 'Segmentation fault' program while running the following program.Please help me to find out the error.
Code:
//-------------------------------------------------------------------------
int partition(int a[],int first,int last)
{
int pivot,up,down,i,j,n,pivindex,temp;
up=first;
down=last;
pivot=a[first];
do
{
//move 'up' to the first value > pivot
for(i=0;i<n;i++)
{
if(a[i]>pivot)
up=i;
}
//move 'down' to the first value <=pivot
for(i=n;i>0;i--)
{
if(a[i]<=pivot)
down=i;
}
temp=a[up];
a[up]=a[down];
a[down]=temp;
}while(up<=down);
//Swap pivot and a[down]
temp=a[first];
a[first]=a[down];
a[down]=temp;
//Set pivindex at the current position of down
//so that all values below pivindex are <=pivot
//and vice versa
pivindex=down;
return pivindex;
}
//----------------------------------------------------------------------------
void quicksort(int a[],int first,int last)
{
int pivindex;
if(first<last){
pivindex=partition(a,first,last);
quicksort(a,first,pivindex-1);
quicksort(a,pivindex+1,last);
}
}
//-----------------------------------------------------------------------------
main()
{
int a[20],n;
printf("\nPlease enter the no. of elements::");
scanf("%d",&n);
printf("\nPls enter the array to sorted(quick sort)::");
read_arr(a,n);
printf("\nArray elements are as follows:\n");
print_arr(a,n);
quicksort(a,0,n-1);
printf("\n The Sorted list is:\n");
print_arr(a,n);
}
//-------------------------------------------------------------------------------