Created
May 13, 2016 16:12
-
-
Save albertzak/7a7b9bbd7f52f4289abf099e348a2f11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
struct _element { | |
char icao_code[5]; | |
char station_name[100]; | |
}; | |
typedef struct _element element; | |
void insertion_sort(element* stations, int size) { | |
int i, j; | |
element tmp; | |
for(j = 2; j < size; j++) { | |
tmp = stations[j]; | |
i = j - 1; | |
while(i >= 0 && strcmp(stations[i].icao_code, tmp.icao_code) > 0) { | |
stations[i+1] = stations[i]; | |
i--; | |
} | |
stations[i+1] = tmp; | |
} | |
} | |
void selection_sort(element* stations, int size) { | |
int j, i, smallestIndex; | |
element tmp; | |
for(j = 0; j <= size - 1; j++) { | |
smallestIndex = j; | |
// find remaining smallest element | |
for(i = j + 1; i < size; i++) { | |
if (strcmp(stations[i].icao_code, stations[smallestIndex].icao_code) < 0) { | |
smallestIndex = i; | |
} | |
} | |
tmp = stations[smallestIndex]; | |
stations[smallestIndex] = stations[j]; | |
stations[j] = tmp; | |
} | |
} | |
int quick_sort_partition(element * stations, int left, int right, char * x) { | |
int i, j; | |
element tmp; | |
i = left - 1; | |
j = right; | |
while (i <= j) { | |
while(strcmp(stations[i].icao_code, x) < 0) { | |
i++; | |
} | |
while(strcmp(stations[j].icao_code, x) > 0) { | |
j--; | |
}; | |
if (i <= j) { | |
tmp = stations[i]; | |
stations[i] = stations[j]; | |
stations[j] = tmp; | |
} | |
} | |
tmp = stations[i]; | |
stations[i] = stations[right]; | |
stations[right] = tmp; | |
return i; | |
} | |
void quick_sort(element* stations, int left, int right) { | |
if (left < right) { | |
int i, j; | |
element pivot, temp; | |
pivot = stations[left]; | |
i = left; | |
j = right + 1; | |
while (1) { | |
do i++; while (i <= right && strcmp(stations[i].icao_code, pivot.icao_code) <= 0); | |
do j--; while (strcmp(stations[j].icao_code, pivot.icao_code) > 0); | |
if (i >= j) break; | |
temp = stations[i]; | |
stations[i] = stations[j]; | |
stations[j] = temp; | |
} | |
temp = stations[left]; | |
stations[left] = stations[j]; | |
stations[j] = temp; | |
quick_sort(stations, left, j-1); | |
quick_sort(stations, j+1, right); | |
} | |
} | |
void merge_sort(element* stations, element* tmpStations, int left, int right) { | |
if (left < right) { | |
// divide | |
int middle; | |
middle = (left + right) / 2; | |
merge_sort(stations, tmpStations, left, middle); | |
merge_sort(stations, tmpStations, middle + 1, right); | |
// merge | |
int i, j, k; | |
i = left; | |
j = right; | |
k = left; | |
while (i <= middle) | |
tmpStations[k++] = stations[i++]; | |
while (j > middle) | |
tmpStations[k++] = stations[j--]; | |
i = left; | |
j = right; | |
k=left; | |
while (i <= j) { | |
if (strcmp(tmpStations[i].icao_code, tmpStations[j].icao_code) <= 0) { | |
stations[k++] = tmpStations[i++]; | |
} else { | |
stations[k++] = tmpStations[j--]; | |
} | |
} | |
} | |
} | |
void print_stations(element* stations, int size) { | |
int i; | |
for (i = 0; i < size; i++) { | |
printf("%s : %s", stations[i].icao_code, stations[i].station_name); | |
} | |
} | |
int readfile(element* stations, int* size) { | |
FILE * fp; | |
char line[100]; // keeps one line of the file | |
// try to open the file; in case of an error exit the program | |
*size = 0; | |
fp = fopen("stations.csv", "r"); | |
if (!fp) { | |
printf("Cannot open file stations.csv!\n"); | |
return 1; | |
} | |
// read all weather stations from file and add them to the | |
// hash table | |
while (!feof(fp)) { | |
// read one line and skip empty and comment lines | |
fgets(line, 100, fp); | |
if (line[0] == '#') continue; | |
if (isspace(line[0])) continue; | |
// take the first 4 characters as key value | |
strncpy(stations[*size].icao_code, line, 4); | |
stations[*size].icao_code[4] = '\0'; | |
// take the rest as value | |
strcpy(stations[*size].station_name, strchr(line, ';')+1); | |
*size += 1; | |
} | |
// close the file | |
fclose(fp); | |
return 0; | |
} | |
int main(int argc, char** argv) { | |
element* stations; // holds the station codes and names | |
element* tmpStations; // holds temporary station codes and names for merge sort | |
int size; // holds the number of valid entries in the array | |
stations = (element*)malloc(sizeof(element)*6000); | |
tmpStations = (element*)malloc(sizeof(element)*6000); | |
// now lets try insertion sort | |
printf("Sorting with insertion sort:\n\n"); | |
// read the station data from file | |
readfile(stations, &size); | |
// sort the station names | |
insertion_sort(stations, size); | |
// print the result | |
print_stations(stations, size); | |
// now lets try selection sort | |
printf("Sorting with selection sort:\n\n"); | |
// read the station data from file | |
readfile(stations, &size); | |
// sort the station names | |
selection_sort(stations, size); | |
// print the result | |
print_stations(stations, size); | |
// now lets try merge sort | |
printf("Sorting with merge sort:\n\n"); | |
// read the station data from file | |
readfile(stations, &size); | |
// sort the station names | |
merge_sort(stations, tmpStations, 0, size-1); | |
// print the result | |
print_stations(stations, size); | |
// now lets try quick sort | |
printf("Sorting with quick sort:\n\n"); | |
// read the station data from file | |
readfile(stations, &size); | |
// sort the station names | |
quick_sort(stations, 0, size-1); | |
// print the result | |
print_stations(stations, size); | |
free(stations); | |
free(tmpStations); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can u share the csv file ?