Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8390553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:01:20+00:00 2026-06-09T19:01:20+00:00

Sorry to bother You with yet another question about K&R programs. But again some

  • 0

Sorry to bother You with yet another question about K&R programs.
But again some things are not clear to me.
The program below is used to sort lines saved by readlines function (is it really saving them?) For that purpose we create an array of pointers pointing to each of these lines.

Questions:
[1] Why do we pass qsort(lineptr, 0, nlines-1); I mean nlines-1 and not just nlines ?

[2] Suppose I entered two lines:

bravo

alfa

now they’re pointed from array (I’ll make a visualization so don’t get mad seeing chars in brackets ;))

*v[] == [lineptr[0] – pointer to bravo][lineptr[1] – pointer to alfa]

according to the algorithm: lineptr[0] is the pivot, so we swap them; last = 0; entering for loop: strcmp of – lineptr[1] is not lexically smaller than lineptr[0]; incrementing i is already finished so we exit for loop. Now we swap last = 0 with left = 0, so nothing happens. They’re already in place.

BUT WHAT IF they begin with the same letter eg. abravo and alfa, how would qsort go on to next letter if it only operates on array’s indexes from left to right meaning different number of lines not next chars in examined strings.

Please correct me, because all those pointers are driving me crazy.

The whole code:

#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /* max #lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
/* sort input lines */
main()
{
    int nlines;
    /* number of input lines read */
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
        qsort(lineptr, 0, nlines-1);
        writelines(lineptr, nlines);
        return 0;
    } else {
        printf("error: input too big to sort\n");
        return 1;
    }
}
#define MAXLEN 1000 /* max length of any input line */
int getline(char *, int);
char *alloc(int);
/* readlines: read input lines */
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'; /* delete newline */
            strcpy(p, line);
            lineptr[nlines++] = p;
        }
    return nlines;
}
/* writelines: write output lines */
void writelines(char *lineptr[], int nlines)
{
    int i;
    for (i = 0; i < nlines; i++)
        printf("%s\n", lineptr[i]);
}

/* qsort: sort v[left]...v[right] into increasing order */
void qsort(char *v[], int left, int right)
{
    int i, last;
    void swap(char *v[], int i, int j);
    if (left >= right) /* do nothing if array contains */
        return;
    /* fewer than two elements */
    swap(v, left, (left + right)/2);
    last = left;

    for (i = left+1; i <= right; i++)
        if (strcmp(v[i], v[left]) < 0)
            swap(v, ++last, i);
    swap(v, left, last);
    qsort(v, left, last-1);
    qsort(v, last+1, right);
}

THANKS IN ADVANCE FOR HELP

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:01:22+00:00Added an answer on June 9, 2026 at 7:01 pm

    strcmp compares two strings for lexicographic order. This will take care of using the whole string for comparison, not just the first character.

    And the parameter right takes the index of the right-most index to sort on. If you have an array of length 10, the first element is 0 and the last is 9 (i.e. 10 – 1, which is right -1). The clue is in the line for (i = left+1; i <= right; i++), which loops i from left+1 to right including both values.

    You called it qsort(lineptr, 0, nlines-1);, so it will include the 0th (first) element of the array and the nlines-1 (last) element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry to bother you guys & girls again on Christmas eve, but I need
Sorry to bother again, but I really need help transforming this Python2 code into
Morning again..., Sorry to bother everyone but I need more help... I haven't done
Sorry but I am not sure how to ask this question but I am
Hello I am sorry to bother you with such a simple question but this
Sorry to bother you but I'm having some trouble with my preg_match parameters. This
Sorry to bother everyone again. For some reason when I try to run the
Sorry to bother you with such a simple question, but I'm stuck here since
Hey, sorry to bother you again, but I can't get this to work and
Sorry to bother again, but as I am new C++ I am having a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.