Problem 22 solution
More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
|
#define | MAX_NAMES 6000 |
| Maximum number of names to store.
|
|
#define | MAX_NAME_LEN 20 |
| Maximum length of each name.
|
|
◆ lazy_sort()
void lazy_sort |
( |
char |
data[][MAX_NAME_LEN], |
|
|
int |
LEN |
|
) |
| |
Alphabetical sorting using 'lazy sort' algorithm.
51 for (i = 0; i <
LEN; i++)
53 for (j = i + 1; j <
LEN; j++)
58 strcpy(tmp_buffer,
data[i]);
60 strcpy(
data[j], tmp_buffer);
65 for (i = 0; i <
LEN; i++) printf(
"%s\t",
data[i]);
#define LEN
Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...
Definition: alaw.c:28
#define MAX_NAME_LEN
Maximum length of each name.
Definition: sol1.c:15
Definition: prime_factoriziation.c:25
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function.
72 unsigned long COUNT = 0;
73 char *fname =
"names.txt";
78 method = atoi(argv[1]);
80 FILE *fp = fopen(fname,
"rt");
83 perror(
"Unable to open file");
92 int ret = fscanf(fp,
"\"%[^\",]\",", names[COUNT++]);
99 printf(
"\nTotal number of names: %lu\n", COUNT);
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);
109 else if (method == 1)
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);
119 clock_t start_time = clock();
123#pragma omp parallel for schedule(runtime) reduction(+ : sum_score)
126 for (i = 935; i < 940; i++)
128 for (i = 0; i < COUNT; i++)
133 for (
int j = 0; names[i][j] !=
'\0'; j++)
134 score += names[i][j] -
'A' +
136 sum_score += score * (i + 1);
138 printf(
"Name: %s\tScore: %u x %u = %lu\n", names[i], score, i + 1,
139 (
unsigned long)score * (i + 1));
142 clock_t end_time = clock();
143 printf(
"Scoring time: %.4g millisecond\n",
144 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
146 printf(
"Total Score = %lu\n", sum_score);
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
◆ shell_sort()
void shell_sort |
( |
char |
data[][MAX_NAME_LEN], |
|
|
int |
LEN |
|
) |
| |
Alphabetical sorting using 'shell sort' algorithm.
22 const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
23 const int gap_len = 8;
26 for (g = 0; g < gap_len; g++)
29 for (i = gap; i <
LEN; i++)
32 strcpy(tmp_buffer,
data[i]);
34 for (j = i; j >= gap && strcmp(
data[j - gap], tmp_buffer) > 0;
37 strcpy(
data[j], tmp_buffer);
41 for (i = 0; i <
LEN; i++) printf(
"%s\t",
data[i]);