-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
173 lines (133 loc) · 3.5 KB
/
sort.c
File metadata and controls
173 lines (133 loc) · 3.5 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// sort.c
// 595_project1
//
// Created by David Milewicz on 2/19/17.
// Copyright © 2017 David Milewicz. All rights reserved.
//
/*
********************************************
*
* Quicksort functions
*
********************************************
*/
#include "sort.h"
#include <string.h>
/*
* Sort data using quicksort algorithm.
*/
void quicksort_data(course_data** data, int p, int r, int (*compare)(course_data*, course_data*) ) {
if (p >= r) return;
// partition data
int q = partition_data(data, p, r, compare);
// sort the data based on partition
quicksort_data(data, p, q-1, compare);
quicksort_data(data, q+1, r, compare);
}
/*
* partition function for the Quicksort operations
*/
int partition_data(course_data** data, int p, int r, int (*compare)(course_data*, course_data*) ) {
course_data* pivot = data[p];
int i = p+1, j = r;
while( i <= j) {
while(i <= j && compare(data[i], pivot) <= 0) {
i++;
}
while(i <= j && compare(data[j], pivot) > 0) j--;
if (i <= j) swap(data, i, j);
}
swap(data, p, j);
return j;
}
/*
* Swap
*/
void swap(course_data** arr, int a, int b) {
course_data* temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
/*
********************************************
*
* Comparison functions
*
********************************************
*/
/*
* Sort by course id.
*/
int compare_course_id(course_data* a, course_data* b) {
return strcmp(a->course_id, b->course_id);
}
/*
* Sort by professors.
*/
int compare_professors(course_data* a, course_data* b) {
return strcmp(a->prof, b->prof);
}
/*
* Sort by enrollment.
*/
int compare_enrollment(course_data* a, course_data* b) {
return a->enrollment - b->enrollment;
}
/*
* Sort by course quality from low to high.
*/
int compare_quality_low(course_data* a, course_data* b) {
return ((100 * a->quality)) - ((100 * b->quality));
}
/*
* Sort by course difficulty from high to low.
*/
int compare_difficulty_high(course_data* a, course_data* b) {
return (b->difficulty * 100) - (a->difficulty * 100);
}
/*
* Sort by instructor quality from
*/
int compare_instructor_quality(course_data* a, course_data* b) {
return (a->instructor_quality * 100) - (100 * b->instructor_quality);
}
/*
* Sort by course quality from high to low.
*/
int compare_quality_high(course_data* a, course_data* b) {
return ((100 * b->quality)) - ((100 * a->quality));
}
/*
* Sort by course difficulty low to high.
*/
int compare_difficulty_low(course_data* a, course_data* b) {
return (100 * b->difficulty) - (100 * a->difficulty);
}
/*
********************************************
*
* Average functions
*
********************************************
*/
int average_enrollment(data_container* d) {
int sum = 0;
for (int i = 0; i < d->length; i++) sum += d->data[i]->enrollment;
return sum / d->length;
}
double average_difficulty(data_container* d) {
double sum = 0;
for ( int i = 0; i < d->length; i++) sum += d->data[i]->difficulty;
return sum / d->length;
}
double average_quality(data_container* d) {
double sum = 0;
for ( int i = 0; i < d->length; i++) sum += d->data[i]->quality;
return sum / d->length;
}
double average_instructor_quality(data_container* d) {
double sum = 0;
for ( int i = 0; i < d->length; i++) sum += d->data[i]->instructor_quality;
return sum / d->length;
}