-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMAXMIN.C
107 lines (76 loc) · 2.86 KB
/
MAXMIN.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* MAX-MIN USING DIVIDE AND CONQUER */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void MaxMin(int i,int j);
int arr[1000], max, min;
int main()
{
int n,i;
time_t start, end;
time_t diff;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
randomize(); //Generates random numbers
for(i=0;i<n;i++)
{
// scanf("%d",&arr[i]);
arr[i]=random(100);
}
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
min=arr[0];
max=arr[0];
start=time(NULL);
MaxMin(0,n-1);
printf("\n\nMaximum value: %d \nMinimum value: %d\n",max,min);
end=time(NULL);
diff=difftime(end, start);
printf("\nTime taken is %ld seconds\n",diff);
getch();
return 0;
}
//-------FIND MAX AND MIN-------
void MaxMin(int i,int j)
{
int max1,min1;
if(i==j) //Only 1 element
max=min=arr[i];
else if(j==i+1) //2 elements
{
if(arr[i]<arr[j])
{
min=arr[i];
max=arr[j];
}
else
{
min=arr[j];
max=arr[i];
}
}
else //More than 2 elements
{
int mid=(i+j)/2;
MaxMin(i,mid);
max1=max;
min1=min;
MaxMin(mid+1,j);
if(min1<min)
min=min1;
if(max<max1)
max=max1;
}
}
/* OUTPUT
Enter the number of elements: 10
Enter the elements: 98 61 53 75 28 92 93 94 22 90
Maximum value: 98
Minimum value: 22
Time taken is 0 seconds
*/