Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.2 kB
0
Indexable
8
#include<stdio.h>

// #include<conio.h>

#define MAX 10
int s[MAX],x[MAX],d;
void sumofsub(int p,int k,int r)
{
int i;
x[k]=1;
if((p+s[k])==d)
{
for(i=1;i<=k;i++)
if(x[i]==1)
printf("%d ",s[i]);
printf("\n");
}
else
if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r-s[k]);
if((p+r-s[k]>=d) && (p+s[k+1]<=d))
{
x[k]=0;
sumofsub(p,k+1,r-s[k]);
}
}
int main()
{
int i,n,sum=0;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the set in increasing order:");
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
printf("\nEnter the max subset value:");
scanf("%d",&d);
for(i=1;i<=n;i++)
sum=sum+s[i];
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);

return 0;

}

9
#include<stdio.h>

#include<stdlib.h>

#include<time.h>
void selectionsort(int A[],int n)
{
int i,j,min;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
{
min=j;
}
}
if(min!=i)
{
int temp=A[j];
A[j]=A[min];
A[min]=A[j];
}
}
}
int main()
{
srandom(time(NULL));
int n=30000,i;
int elements[n];
for(int i=0;i<n;i++)
{
elements[i]=rand()%1000;
}
for(int size=5000;size<=n;size+=2000)
{
int arr[size];
for(int i=0;i<size;i++)
{
arr[i]=elements[i];
}

clock_t start=clock();

selectionsort(arr,size);
clock_t stop=clock();
double time_taken=((double)(stop-start)/CLOCKS_PER_SEC);
printf("Timen taken to sort %d elements is %f seconds\n",size,time_taken);
}
return 0;
}

10
#include<stdio.h>

#include<time.h>

#include<stdlib.h>
void quicksort(int a[],int low,int high);
int partition(int a[],int low,int high);
void swap(int*,int*);
void quicksort(int a[],int low,int high)
{
if(low<high)
{
int pi = partition(a,low,high);
quicksort(a,low,pi-1);
quicksort(a,pi+1,high);
}
}
void swap(int *a,int *b)
{
int c=*a;
*a=*b;
*b=c;
}
int partition(int a[],int low,int high)
{
int pivot=a[high];
int i=low-1;
for(int j=low;j<=high-1;j++)
{
if(a[j]<=pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[high]); return (i+1);
}
void main()

{

srand(time(NULL));
int a[100000],n=100000;
int elements[n];
for(int i=0;i<n;i++)
 {
 elements[i]=rand()%1000;
 }
 for(int size=5000;size<=n;size+=2000)
{
int arr[size];
for(int i=0;i<size;i++)
{
arr[i]=elements[i];
}
clock_t start=clock();
quicksort(a,0,size);
clock_t end=clock();
printf("Total time taken to sort %d elements is %lf\n",size,((double)(end-start)/CLOCKS_PER_SEC));
}
}

12
#include<stdio.h>

#include<stdlib.h>

int board[20], count;
void print(int n)
{
int i, j;
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;i++)
printf("\t%d",i);
for(i=1;i<=n;i++)
{
printf("\n\n%d",i);
for(j=1;j<=n;j++)
{
if(board[i]==j)
printf("\tQ");
else
printf("\t-");
}
}
}
int place(int row,int column)
{
int i;
for(i=1;i<=row-1;i++)
{
if(board[i]==column)
return 0;
else if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1;
}
void queen(int row,int n)
{
int column;
for(column=1;column<=n;column++)
{
if(place(row,column))

{

board[row]=column;
if(row==n)
print(n);
else
queen(row+1,n);
}
}
}
int main()
{
int n;
printf("Enter number of Queens:");
scanf("%d",&n);
queen(1,n);
}
Leave a Comment