Hi....
I wrote a program for Quick Sort algorithm....
It is compiled fine....but it is giving a run time error i.e., Segmentation Fault....
I tried hard but couldn't find out what is that thing causing segmentation error in this program....
Please help....
I'm getting segmentation fault very frequently (i'm using gcc compiler).....why?
Any help would be highly appreciated...
Code
#include<stdio.h>
int quick(int a[],int left,int right){
if(left<right){
int newpivi;
newpivi=partition(a,left,right); //positioning pivot in its correct position.......
quick(a,left,newpivi-1); //left part of the pivot element....
quick(a,newpivi+1,right); //right part of the pivot element.....
}
}
int partition(int a[],int left,int right){ //partition function for positioning pivot
int pivot=left,big=left+1,small=right,temp=0; //left most element is selected as pivot
do {
while(a[pivot]>=a[big])
big++;
while(a[pivot]<=a[small])
small--;
if(big<small){
temp=a[big];
a[big]=a[small];
a[small]=temp;
}
}while(small>big);
temp=a[small];
a[small]=a[pivot];
a[pivot]=temp;
}
main(){
int i,j,key;
int n;
printf("Enter size:");
scanf("%d",&n);
int a[n];
printf("\n Enter elements:\n");
for(j=0;j<n;j++)
scanf("%d",&a[j]);
quick(a,0,n-1); //calling quick sort function
print(&a,n);
}
print(int *a,int p){ //function for printing the array elements
int i;
for(i=0;i<p;i++)
printf("%d ",a[i]);
printf("\n");
}