Analysis of Algorithms (AOA)

Tennis Tournament problem using Divide and Conquer Method in C

/*
	Program for Tennis Tournament problem using Divide and Conquer Method in C
	Author: PracsPedia		www.pracspedia.com
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int calcwin(int p[],int x,int n)
{
	int w1,w2,mid;
	if(n-x > 2)
	{ 
		mid=(x+n)/2;
		w1 = calcwin(p,x,mid);
		w2 = calcwin(p,mid+1,n);   
		if(w1 < w2)
		{  
			printf("The winner between %d and %d is %d\n",w1,w2,w2);
			return w2;
		}
		else
		{  
			printf("The winner between %d and %d is %d\n",w1,w2,w1);	
			return w1;
		}
	}
	else if(x==n)
		return p[n];
	else 
		if(p[x] < p[n])
		{
			printf("The winner between %d and %d is %d\n",p[x],p[n],p[n]);
			return p[n];
		}
		else
		{
			printf("The winner between %d and %d is %d\n",p[x],p[n],p[x]);
			return p[x];   
		}
}

void main()
{
		int i,win,n,p[100];
		clrscr();
		printf("Enter the no of players:\n");
		scanf("%d",&n);
		for(i=0;i < n;i++)
		{	
			p[i]=rand() % 60; 
			printf("Player No %d: %d\n",i+1,p[i]);
		}
		win = calcwin(p,0,n-1);	
		printf("The winner is %d\n",win);
		getch();
}
Download Source Code Program List

Sample Output (*Random)

Enter the no of players:
8
Player No 1: 43
Player No 2: 46
Player No 3: 57
Player No 4: 55
Player No 5: 53
Player No 6: 55
Player No 7: 46
Player No 8: 12
The winner between 43 and 46 is 46
The winner between 57 and 55 is 57
The winner between 46 and 57 is 57
The winner between 53 and 55 is 55
The winner between 46 and 12 is 46
The winner between 55 and 46 is 55
The winner between 57 and 55 is 57
The winner is 57