ashutosh
BAN USERI am a computer science student and programming is my hobby. I have joined this website with a view to help myself established in one of the top software companies.
- 0 Answers How to partition an array
There is some problem in the following code that seeks to partition the array (the first element taken as pivot). Please help me fix the issue.
- ashutosh December 19, 2012
#include<stdio.h>
#include<conio.h>
int partition(int [],int,int);
main()
{
int a[20],n,i;
printf("enter the number of elements in the array\n");
scanf("%d",&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;
}| Flag | PURGE - 0 Answers problem with divide and conquer approach to find max-min in an array
Can anyone help me fix the problem with the code for finding max-min in an array?
- ashutosh December 19, 2012
#include<stdio.h>
#include<conio.h>
maxmin(int,int,int,int);
int num[20],max,min,max1,min1;
main()
{ int n,i;
printf("enter number of elements in the array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the number\n");
scanf("%d",&num[i]);
}
maxmin(0,n-1,max,min);
printf("the maximum element is- %d\n",max);
printf("the minimum element is- %d\n",min);
getch();
}
maxmin(int i,int j,int max,int min)
{
if(i=j) max=min=num[i];
else if(j=i+1) { if(num[i]<num[j]) { max=num[j]; min=num[i];}
else if(num[i]>num[j]){max=num[i]; min=num[j];}
else max=min=num[i];
}
else
{
int mid=(i+j)/2;
maxmin(i,mid,max,min);
maxmin(mid+1,j,max1,min1);
}
if(max<max1) max=max1;
if(min>min1) min=min1;
}| Flag | PURGE - 0 Answers finding repeated elements in an array using randomized algorithm
Given an array of size n having n/2 distinct and the rest identical elements ,write a C program to find the repeated one using randomized algorithm.
- ashutosh December 19, 2012
I have written the code but it has some problem at rum time and stops.
Can anyone help please?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int repeated(int [],int);
main()
{ int n,i;
int *num;
printf("enter the number of elements in the array\n");
scanf("%d",&n);
num=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
printf("enter the number\n");
scanf("%d",num+i);
}
printf("the repeated element is %d\n",repeated(num,n));
getch();
}
int repeated(int num[],int n)
{ int i,j;
while(1)
{
i=rand()%n;
j=rand()%n;
if((i!=j)&&(num[i]==num[j])) return num[i];
}
}| Flag | PURGE