How to write a C Program to REAL Union of Intervals in C Programming Language ?
This C Program to REAL Union of Intervals.
Solution:
- #include<stdio.h>
- float a[65536],b[65536];
- void quicksort(float min,float max)
- {
- int pivot,i,j;
- float k;
- if(min<max)
- {
- pivot=max;
- i=min;
- j=max;
- while(i<j)
- {
- while(a[i]<=a[pivot] && i<max)
- i++;
- while(a[j]>a[pivot])
- j--;
- if(i<j)
- {
- k=a[i];
- a[i]=a[j];
- a[j]=k;
- k=b[i];
- b[i]=b[j];
- b[j]=k;
- }
- }
- k=a[pivot];
- a[pivot]=a[j];
- a[j]=k;
- quicksort(min,j-1);
- quicksort(j+1,max);
- }
- }
- int main()
- {
- int n,i,j;
- float end,sum=0,t;
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- scanf("%f",&a[i]);
- scanf("%f",&b[i]);
- }
- quicksort(0,n-1);
- end=b[0];
- sum=end-a[0];
- for(i=1;i<n;i++)
- {
- if(a[i]<=end)
- {
- if(b[i]>=end)
- {
- t=b[i]-end;
- sum=sum+t;
- end=b[i];
- }
- }
- else
- {
- t=b[i]-end;
- sum=sum+t;
- end=b[i];
- }
- }
- printf("%f",sum);
- return 0;
- }