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

  • Home
  • SEARCH
  • 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 7018575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:09+00:00 2026-05-27T23:04:09+00:00

I am debugging my code for a quick sort algorithm in C. It compiles

  • 0

I am debugging my code for a quick sort algorithm in C. It compiles but fails with a “Segmentation fault” when running it.

Can anybody help me to debug it and give me the working version of my code? I know there are existing and working ones on the internet. But what I really want is to find the bug of my own code.

void myQuickSort(int list[],int head, int tail)
{
    int m = head;
    int n = tail;

    int key = list[m];
    ++head;
    while(head < tail)
    {
        while(list[head] < key)
        {
            ++head;
        }
        while(list[tail] >= key)
        {
            --tail;
        }
        //swamp two elements, to divide the array to two groups
        int temp = list[head];
        list[head] = list[tail];
        list[tail] = temp;

    }

    //get the pivot element in dividing position
    int temp = list[m];
    list[m] = list[head];
    list[head] = temp;

    myQuickSort(list, m, head-1);
    myQuickSort(list, head+1, n);
}
  • 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-05-27T23:04:09+00:00Added an answer on May 27, 2026 at 11:04 pm

    Your function will never exit.

    It will keep calling itself until the call stack is full and cause a stack overflow exception.

    The compiler should generate a warning for this:

    warning C4717: 'myQuickSort' : recursive on all control paths, function will cause runtime stack overflow
    

    You need an exit condition, something along the lines of:

    void myQuickSort(int list[],int head, int tail)
    {
        //exit condition, or else the function will always call itself
        if ( head >= tail )
           return;
    
        /**
        ...
        */
    
        myQuickSort(list, m, head-1);
        myQuickSort(list, head+1, n);
    }
    

    Also, make sure you call the function like:

    int num[5] = {1,4,2,3,5};
    myQuickSort(num,0,4);
    

    the final parameter must be 1 less than the length of the array since C++ arrays are 0-based.

    You also need one extra check in your while loops:

     while( head < tail && list[head] < key )  // see if head reached the end
     {
         ++head;
     }
     while( head < tail && list[tail] >= key )
     {
         --tail;
     }
    

    or else you might pass the end of the array.

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

Sidebar

Related Questions

Sometimes when I am debugging code in Eclipse it happens that although I can
I have some debugging code that if executed while running with GBD attached should
I'm very busy write now debugging some code, so I can't cookup a complete
While debugging some C# code during a sort of peer-review, I noticed an odd
I'm debugging some ASP code and I need to get a quick printout of
i get this dialog while debugging code in visual studio 2008. (your step-into request
Possible Duplicate: What's wrong with Delphi's “with” I am have a problem debugging code
I am debugging some code and have encountered the following SQL query (simplified version):
I am debugging some code and I want to find out when a particular
There I was happily debugging my code, when I did a restart, and even

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.