Main function.
71{
72 unsigned long COUNT = 0;
73 char *fname = "names.txt";
75 short method = 0;
76
77 if (argc == 2)
78 method = atoi(argv[1]);
79
80 FILE *fp = fopen(fname, "rt");
81 if (!fp)
82 {
83 perror("Unable to open file");
84 return -1;
85 }
86
87
88
89
90 do
91 {
92 int ret = fscanf(fp, "\"%[^\",]\",", names[COUNT++]);
93 if (ret <= 0)
94 continue;
95
96 } while (!feof(fp));
97 fclose(fp);
98
99 printf("\nTotal number of names: %lu\n", COUNT);
100
101 if (method == 0)
102 {
103 clock_t start_time = clock();
105 clock_t end_time = clock();
106 printf("\nShell sort: %.4g millisecond\n",
107 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
108 }
109 else if (method == 1)
110 {
111 clock_t start_time = clock();
113 clock_t end_time = clock();
114 printf("\nLazy sort: %.4g millisecond\n",
115 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
116 }
117
118 long sum_score = 0;
119 clock_t start_time = clock();
120 int i;
121
122#ifdef _OPENMP
123#pragma omp parallel for schedule(runtime) reduction(+ : sum_score)
124#endif
125#ifdef DEBUG
126 for (i = 935; i < 940; i++)
127#else
128 for (i = 0; i < COUNT; i++)
129#endif
130 {
131 long score = 0;
132
133 for (int j = 0; names[i][j] != '\0'; j++)
134 score += names[i][j] - 'A' +
135 1;
136 sum_score += score * (i + 1);
137#ifdef DEBUG
138 printf("Name: %s\tScore: %u x %u = %lu\n", names[i], score, i + 1,
139 (unsigned long)score * (i + 1));
140#endif
141 }
142 clock_t end_time = clock();
143 printf("Scoring time: %.4g millisecond\n",
144 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
145
146 printf("Total Score = %lu\n", sum_score);
147
148 return 0;
149}
void shell_sort(char data[][MAX_NAME_LEN], int LEN)
Alphabetical sorting using 'shell sort' algorithm.
Definition sol1.c:20
#define MAX_NAMES
Maximum number of names to store.
Definition sol1.c:14
void lazy_sort(char data[][MAX_NAME_LEN], int LEN)
Alphabetical sorting using 'lazy sort' algorithm.
Definition sol1.c:48