Question

This is the sorting program that can be found in K&R section 5.11.

alloc.c:

#include <stdio.h>

#define BUFSIZE 10000   

static char allocbuf[BUFSIZE];
static char *allocp = allocbuf;

char *alloc(int n) {
   if(allocbuf + BUFSIZE - allocp >= n) {
       allocp += n;
       return allocp - n;
   }
   else {
       return NULL;
   }
}

io.c:

#include <stdio.h>
#include <string.h>
#define MAXLEN 1000


char *alloc(int);

int getLine(char *s, int maxline) {
   int i;
   int c;

   c = getchar();
   maxline--;
   for(i = 0; c != EOF && c != '\n' && maxline > 0; i++, c = getchar(), maxline-- ) {
       s[i] = c;
   }

   if(c == '\n') {
       s[i] = c;
       i++;
   }
   s[i] = '\0';
   return i;
}

int readlines(char *lineptr[], int maxlines) {
   int len, nlines;
   char *p, line[MAXLEN];

   nlines = 0;

   while((len = getLine(line, MAXLEN)) > 0) {
       if(nlines >= maxlines || (p = alloc(len)) == NULL)
           return -1;
       else {
           line[len - 1] = '\0';
           strcpy(p, line);
           lineptr[nlines++] = p;

       }
   }
   return nlines;
}

void writelines(char *lineptr[], int nlines) {
   int i;

   for(i = 0; i < nlines; i++)
       printf("%s\n", lineptr[i]);

}

main.c:

#include <stdio.h>
#include <string.h>

#define MAXLINES 5000

char *lineptr[MAXLINES];

int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);

void qqsort(void *lineptr[], int left, int right, int(*comp)(void *, void *));

int numcmp(char *, char *);

int main(int argc, char *argv[]) {
   int nlines;
   int numeric = 0;

   if(argc > 1 && strcmp(argv[1], "-n") == 0) {
       numeric = 1;
   }
   if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
       qqsort((void **)lineptr, 0, nlines - 1, (int (*)(void *, void *)) (numeric ? numcmp : strcmp));
       writelines(lineptr, nlines);
       return 0;
   }
   else {
       printf("input too big to sort\n");
       return 1;
   }
}

readlines.c:

#include <stdlib.h>

void swap(void *v[], int i, int j) {
   void * tmp;

   tmp = v[i];
   v[i] = v[j];
   v[i] = tmp;
}

int numcmp(char *s1, char *s2) {
   double v1, v2;

   v1 = atof(s1);
   v2 = atof(s2);

   if(v1 < v2) {
       return -1;
   }
   else if(v1 > v2) {
       return 1;
   }
   else {
       return 0;
   }
}

void qqsort(void *v[], int left, int right, int(*comp)(void *, void *)) {
   int i, last;
   if(left >= right) {
       return; 
   }

   swap(v, left, (left + right)/2);
   last = left;

   for(i = left + 1; i <= right; i++) {
       if((*comp)(v[i], v[left]) < 0) {
           swap(v, ++last, i);
       }
   }

   swap(v, left, last);
   qqsort(v, left, last - 1, comp);
   qqsort(v, last + 1, right, comp);
}

The problem is that I don't actually understand what is this program doing. It should read some lines from the input and it should print them in sorted order. But I see no change in the output of the program compared to it's input.

For example:

when the intput is:

test1
asdasdasdas
test2
fobafoobarfoobar
barfoobarfoobarfooofoobar

The output is:

test1
asdasdasdas
test2
fobafoobarfoobar
barfoobarfoobarfooofoobar

When the symbolic argument "-n" is given, the output is still the same. For example: Input:

1234234432
2333
2222222
23423
1

Output:

1234234432
2333
2222222
23423
1

I think I don't understand the purpose of this program very well. Reading it's source code and the documentation related to it I can figure out what is supposed to do. The program doesn't work as I expected. In the second example I would expect the output to look like:

1
2333
23423
2222222
1234234432

Could someone explain me what am I doing wrong? Btw, this program can be found in K&R 2 at page 133.

Was it helpful?

Solution

That swap() is wrong:

   tmp = v[i];
   v[i] = v[j];
   v[i] = tmp;

that last v[i] = tmp; makes this swap() a no-op (no operation), it should be v[j] = tmp;.

So try this one:

void swap(void *v[], int i, int j) {
   void * tmp;

   tmp = v[i];
   v[i] = v[j];
   v[j] = tmp;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top