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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:13:03+00:00 2026-06-16T07:13:03+00:00

below code is my quickselect (something very similar to quicksort except that it returns

  • 0

below code is my quickselect (something very similar to quicksort except that it returns nth item in the sorted array, not the whole sorted array) method. the input is array of strings(words), and I have to return the word of nth index if the given words are sorted.

    static string QuickSelect(string[] lst, int index) {
        if (lst.Length == 1)
        {
            Console.Write(lst[0]);
            return lst[0];
        }

        else
        {
            int pivot = _r.Next(lst.Length); // pick a pivot by random number generator
            bool[] isDecre = new bool[lst.Length];
            string[] WordLeft, WordRight;
            int nLeft = 0, nRight = 0;

            for (int i = 0; i < lst.Length; i++) // compare values to pivot
            {
                if (i == pivot) continue;
                if (WordCompare(lst[pivot], lst[i]))
                {
                    nRight++;
                    isDecre[i] = false;
                }
                else
                {
                    nLeft++;
                    isDecre[i] = true;
                }
            }


            if (nLeft == index) // pivot was the (index)th item in the list
                return lst[pivot];

            else
            {

                WordLeft = new string[nLeft];
                WordRight = new string[nRight];
                int l = 0, r = 0;
                // divide list by saved comparison result
                for (int i = 0; i < lst.Length; i++)
                {
                    if (i == pivot) continue;
                    if (isDecre[i])
                    {
                        WordLeft[l] = lst[i];
                        l++;
                    }
                    else
                    {
                        WordRight[r] = lst[i];
                        r++;
                    }
                }

                if (nLeft > index)
                    return QuickSelect(WordLeft, index);
                else if (nLeft < index)
                    return QuickSelect(WordRight, index - nLeft - 1);
            }
        }
    }

the problem is that this code won’t run, throwing “not all code paths return a value” error. I think it has to do with this part:

if (nLeft == index) // pivot was the (index)th item in the list
    return lst[pivot];
// build left and right word lists
 ...
if (nLeft > index)
    return QuickSelect(WordLeft, index);
else if (nLeft < index)
    return QuickSelect(WordRight, index - nLeft - 1);

if I put some ‘else’ after above ‘else if’, the error goes away. But I don’t want to build left and right word lists when the pivot was the nth index string. In fact I think this non-value-returning-path-detected is kinda nonsense. is there any workaround?

  • 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-16T07:13:04+00:00Added an answer on June 16, 2026 at 7:13 am

    No, there is no workaround.

    You have to return value or throw an exception (which won’t be handled in that method) in all paths in the method.

    EDIT:

    On the other hand, your conditions:

    if (nLeft == index) // pivot was the (index)th item in the list
        return lst[pivot];
    // build left and right word lists
     ...
    if (nLeft > index)
        return QuickSelect(WordLeft, index);
    else if (nLeft < index)
        return QuickSelect(WordRight, index - nLeft - 1);
    

    could be changed to this:

    if (nLeft == index) // pivot was the (index)th item in the list
        return lst[pivot];
    // build left and right word lists
     ...
    if (nLeft > index)
        return QuickSelect(WordLeft, index);
    return QuickSelect(WordRight, index - nLeft - 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI: Below code is from RequestFactoryEditorDriver: /** * Returns a new array containing the
Below code returns ? rather than a random character. Any ideas? Please note that
Below code not work, but it's work fine for jsf1.2. Now the framework is
below code is my databasehandler class i got it from a tutorial. Beside that
The below code is very simple. I have a jQuery autocomplete bound to an
Below code prints an Array of fileNames. val pdfFileArray = getFiles() for(fileName <- pdfFileArray){
The below code will not join, when debugged the command does not store the
Below code is not working in IE but working in Chrome... $(this).css(border,solid 1px green)
Below code is working fine in my application. But that leftbarbuttonitem is invisible. And
Below code checks for last item but I need to check for the second

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.