Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol1.c File Reference

Problem 22 solution More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for sol1.c:

Macros

#define MAX_NAMES   6000
 Maximum number of names to store.
 
#define MAX_NAME_LEN   20
 Maximum length of each name.
 

Functions

void shell_sort (char data[][MAX_NAME_LEN], int LEN)
 Alphabetical sorting using 'shell sort' algorithm.
 
void lazy_sort (char data[][MAX_NAME_LEN], int LEN)
 Alphabetical sorting using 'lazy sort' algorithm.
 
int main (int argc, char **argv)
 Main function.
 

Detailed Description

Problem 22 solution

Author
Krishna Vedala

Function Documentation

◆ lazy_sort()

void lazy_sort ( char  data[][MAX_NAME_LEN],
int  LEN 
)

Alphabetical sorting using 'lazy sort' algorithm.

49{
50 int i, j;
51 for (i = 0; i < LEN; i++)
52 {
53 for (j = i + 1; j < LEN; j++)
54 {
55 if (strcmp(data[i], data[j]) > 0)
56 {
57 char tmp_buffer[MAX_NAME_LEN];
58 strcpy(tmp_buffer, data[i]);
59 strcpy(data[i], data[j]);
60 strcpy(data[j], tmp_buffer);
61 }
62 }
63 }
64#ifdef DEBUG
65 for (i = 0; i < LEN; i++) printf("%s\t", data[i]);
66#endif
67}
#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.

71{
72 unsigned long COUNT = 0;
73 char *fname = "names.txt";
74 char names[MAX_NAMES][MAX_NAME_LEN];
75 short method = 0; /* sorting algorithm to use. 0 = lazy, 1 = shell-sort */
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 * Loops to get total number of rows and columns in the file
89 */
90 do
91 {
92 int ret = fscanf(fp, "\"%[^\",]\",", names[COUNT++]);
93 if (ret <= 0)
94 continue;
95 // printf("%s\t", names[COUNT - 1]);
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();
104 shell_sort(names, COUNT);
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();
112 lazy_sort(names, COUNT);
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 /* score the alphabets in i^th name */
133 for (int j = 0; names[i][j] != '\0'; j++)
134 score += names[i][j] - 'A' +
135 1; /* convert ASCII character to integer score */
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
Here is the call graph for this function:

◆ shell_sort()

void shell_sort ( char  data[][MAX_NAME_LEN],
int  LEN 
)

Alphabetical sorting using 'shell sort' algorithm.

21{
22 const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
23 const int gap_len = 8;
24 int i, j, g;
25
26 for (g = 0; g < gap_len; g++)
27 {
28 int gap = gaps[g];
29 for (i = gap; i < LEN; i++)
30 {
31 char tmp_buffer[MAX_NAME_LEN];
32 strcpy(tmp_buffer, data[i]);
33
34 for (j = i; j >= gap && strcmp(data[j - gap], tmp_buffer) > 0;
35 j -= gap)
36 strcpy(data[j], data[j - gap]);
37 strcpy(data[j], tmp_buffer);
38 }
39 }
40#ifdef DEBUG
41 for (i = 0; i < LEN; i++) printf("%s\t", data[i]);
42#endif
43}