
December 25th, 2012, 04:38 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 40 m 52 sec
Reputation Power: 0
|
|
Problem in array partition program
The following program to partition the array stops unexpectedly when executed. Can anyone help me out?
Code:
#include<stdio.h>
#include<conio.h>
int partition(int [],int,int);
main()
{
int n,i;
printf("enter the number of elements in the array\n");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("enter the number\n");
scanf("%d",a+i);
}
printf("the index of the pivot is %d and the partitioned array is\n",partition(a,0,n-1));
for(i=0;i<n;i++) printf("%d\t",*(a+i));
getch();
}
int partition(int a[],int lower,int upper)
{
int p=a[lower];
int i=lower,j=upper+1,temp;
do
{ do{i++;}while(a[i]>=p);
do{j--;}while(a[j]<=p);
if(i<j){ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i>=j);
a[lower]=a[j];
a[j]=p;
return j;
}
|