Skip to content

Commit 5770f21

Browse files
authored
Merge pull request #1 from teo-tsirpanis/master
Use built-in sorting function.
2 parents 422fd7f + 34d6901 commit 5770f21

File tree

1 file changed

+12
-42
lines changed

1 file changed

+12
-42
lines changed

ActivitySelectionProblem.c

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23

34
void findMaxActivities(int n,int arr1[n],int depr[n]);
4-
void quickSort(int a[],int l,int h);
5-
int partition(int a[],int l,int h);
65

76
int main(){
87
printf("Give number of activities: ");
@@ -20,64 +19,35 @@ int main(){
2019
return 0;
2120
}
2221

22+
int compareIntegers(void *x1,void *x2){
23+
return *(int *) x1 - *(int *) x2;
24+
}
25+
2326
void findMaxActivities(int n,int arrl[n],int depr[n]){
24-
quickSort(arrl,0,n-1); //sort the tables(you can use your own sort method)
25-
quickSort(depr,0,n-1);
27+
qsort((void *) arrl, n, sizeof(int), compareIntegers); //sort the tables
28+
qsort((void *) depr, n, sizeof(int), compareIntegers);
2629
int depr_time[n];
2730
int activitiesIn = 1,maxActivities = 1;
2831
depr_time[0] = depr[0];
2932
int i = 1, j = 0,k = 0;
3033

31-
while (i < n && j < n){
34+
while (i < n && j < n){
3235
if (arrl[i] < depr[j]){
33-
activitiesIn++;
36+
activitiesIn++;
3437
if (activitiesIn >= maxActivities){
3538
if(activitiesIn == maxActivities)k++; //k is to keep the number of the same activities in 2 or more time periods
36-
else k = 0;
39+
else k = 0;
3740
maxActivities = activitiesIn;
3841
depr_time[k] = depr[j]; //departure time of max activity
3942
}
4043
i++;
4144
}
4245
else{
43-
activitiesIn--;
44-
j++;
46+
activitiesIn--;
47+
j++;
4548
}
4649
}
4750
for(i=0; i<=k; i++){
4851
printf("Maximum Number of Activities : %d at time period %d-%d\n",maxActivities,depr_time[i]-1,depr_time[i]);
4952
}
5053
}
51-
52-
void quickSort(int a[],int l,int h){
53-
int j;
54-
if(l<h){
55-
j=partition(a,l,h);
56-
quickSort(a,l,j-1);
57-
quickSort(a,j+1,h);
58-
}
59-
}
60-
61-
int partition(int a[],int l,int h){
62-
int v,i,j,temp;
63-
v=a[l];
64-
i=l;
65-
j=h+1;
66-
67-
do{
68-
do{
69-
i++;
70-
}while(a[i]<v&&i<=h);
71-
do{
72-
j--;
73-
}while(v<a[j]);
74-
if(i<j){
75-
temp=a[i];
76-
a[i]=a[j];
77-
a[j]=temp;
78-
}
79-
}while(i<j);
80-
a[l]=a[j];
81-
a[j]=v;
82-
return j;
83-
}

0 commit comments

Comments
 (0)