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 9215637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:14:55+00:00 2026-06-18T02:14:55+00:00

Currently trying to figure out what the issue is with this sort. Built directly

  • 0

Currently trying to figure out what the issue is with this sort. Built directly from the in-place Quicksort pseudocode from Wikipedia, which I am going to assume is reliable. I am attempting to sort an array of structs by a null-terminated 3-character “code” field.

The sort mostly works, but there are always a few elements out of place. I can only assume this has to do with the pivot somehow, but I have spent a few hours staring at it and have gotten nowhere. Thanks!

void quicksort(Cdir *directory, int left, int right) {

    if (left < right) {
        int pivotIdx = left;
        pivotIdx = partition(directory, left, right, pivotIdx);
        quicksort(directory, left, pivotIdx - 1);
        quicksort(directory, pivotIdx + 1, right);
    }
}

int partition(Cdir *directory, int left, int right, int pivot) {

    char *pivotVal = directory[pivot].code;
    int curIdx = left;
    swap(&directory[pivot], &directory[right]);

    int i;
    for (i = left; i < right; i++) {
        if (strncmp(directory[i].code, pivotVal, 3) < 0) {
            swap(&directory[i], &directory[curIdx]);
            curIdx++;
        }
    }
    swap(&directory[curIdx], &directory[right]);
    return curIdx;
} 

void swap(Cdir *s1, Cdir *s2) {

    Cdir temp = *s1; 
    *s1 = *s2;
    *s2 = temp;
}
  • 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-18T02:14:56+00:00Added an answer on June 18, 2026 at 2:14 am

    I finally figured it out. When I replaced “pivotVal” in my string comparison with a direct reference to the pivot value (directory[right]), the sort works fine. Still trying to decide why that is, but it is fixed!

    void quicksort(Cdir directory[], int left, int right) {
    
        if (left < right) {
            int pivotIdx = left;
            pivotIdx = partition(directory, left, right, pivotIdx);
            quicksort(directory, left, pivotIdx - 1);
            quicksort(directory, pivotIdx + 1, right);
        }
    }
    
    int partition(Cdir directory[], int left, int right, int pivot) {
    
        int curIdx = left;
        swap(&directory[pivot], &directory[right]);
    
        int i;
        for (i = left; i < right; i++) {
            if (strncmp(directory[i].code, directory[right].code, 3) < 0) {
                swap(&directory[i], &directory[curIdx]);
                curIdx++;
            }
        }
        swap(&directory[curIdx], &directory[right]);
    
        return curIdx;
    } 
    
    void swap(Cdir *s1, Cdir *s2) {
    
        Cdir temp = *s1; 
        *s1 = *s2;
        *s2 = temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying everything to figure this issue out. :( For a TextBox, I
I am currently trying to figure out KendoUI. I am using version 2012.1.322. I
I am currently trying to figure out how you can bind multiple fields in
I am currently trying to figure out the best way to create a .xls
I am currently building a Rails app, and trying to figure out the best
I have been trying to figure this one out for the last couple of
I'm currently having a really weird issue and I can't seem to figure out
I'm having trouble trying to figure out how to solve a dependency issue within
Currently trying to figure out how best to process the following sample XML data:
I've spent a while trying to figure this one out. I'm sure someone with

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.