Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
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
 
#define MAX_NAME_LEN   20
 

Functions

void shell_sort (char data[][MAX_NAME_LEN], int LEN)
 
void lazy_sort (char data[][MAX_NAME_LEN], int LEN)
 
int main (int argc, char **argv)
 

Detailed Description

Problem 22 solution

Author
Krishna Vedala

Macro Definition Documentation

◆ MAX_NAME_LEN

#define MAX_NAME_LEN   20

Maximum length of each name

◆ MAX_NAMES

#define MAX_NAMES   6000

Maximum number of names to store

Function Documentation

◆ lazy_sort()

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

Alphabetical sorting using 'lazy sort' algorithm

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

◆ main()

int main ( int  argc,
char **  argv 
)

Main function

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