In this code for quicksort, if input array is decreasing, for example 5 4 3 2 1 , variable i goes on incrementing without check and go out of array bound. Shouldn't that give segmentation error? As pointed out in replies, even if it doesnt give segmentation fault, why doesn't it swap arr[j] with garbage value in arr[i](where i is out of bounds)?
int HoarePartition(int a[],int l,int r)
{
int p,i,j,temp;
p=a[l],i=l,j=r+1;
do
{
do
{
i++;
}while(a[i]<p);
do
{
j--;
}while(a[j]>p);
temp=a[i];
a[i]=a[j];
a[j]=temp;
}while(i<j);
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=a[l];
a[l]=a[j];
a[j]=temp;
return j;
}
This along with quicksort function gives correct sorted array for input array of 5 4 3 2 1 or similar decreasing array.