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

The Archive Base Latest Questions

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

I am trying to Quicksort 10 random numbers within an listbox. But i cant

  • 0

I am trying to Quicksort 10 random numbers within an listbox. But i cant use the method on my random iar, can anyone give me some advice.

Code behind button:

         private void btnSort_Click(object sender, EventArgs e)
    {
        Random r = new Random();
        int n = 10;
        int[] iar = new int[n];
        for (int i = 0; i < iar.Length; i++)
        {
            iar[i] = r.Next(0, 20);
            lb1.Items.Add(iar[i]);

        //here is the error i want to fill lb2 with the quicksorted array 
        // using the quicksort method

            Quicksort(iar, 0, iar.Length - 1);
        }
        for (int i = 0; i < iar.Length; i++)
        {
            lb2.Items.Add(iar[i]);
        }
    }

Quicksort method

 public static void Quicksort(IComparable[] elements, int left, int right)
    {
        int i = left, j = right;
        IComparable pivot = elements[(left + right) / 2];

        while (i <= j)
        {
            while (elements[i].CompareTo(pivot) < 0)
            {
                i++;
            }
            while (elements[j].CompareTo(pivot) > 0)
            {
                j--;
            } 
            if (i <= j)
            {
                // Swap
                IComparable tmp = elements[i];
                elements[i] = elements[j];
                elements[j] = tmp;

                i++;
                j--;
            }
        }
        // Recursive calls
        if (left < j)
        {
            Quicksort(elements, left, j);
        }
        if (i < right)
        {
            Quicksort(elements, i, right);
        }
    }

}

ERRORS:

Error 2 Argument 1: cannot convert from ‘int[]’ to ‘System.IComparable[]’
Error 1 The best overloaded method match for ‘Quicksort.FrmQuicksort.Quicksort(System.IComparable[], int, int)’ has some invalid arguments

Thanxs for looking:)

  • 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:21:16+00:00Added an answer on June 18, 2026 at 2:21 am

    In the given context there is no real need to specify IComparable rather than int because you are only ever using integers. You’re also calling QuickSort as each number is generated when it will work best if you run the QuickSort algorithm on the already generated list so move the statement outside the loop. Also bare in mind that there are better sorting algorithms for sorting tiny amounts of data like this.

    Click Event Handler:

    private void btnSort_Click(object sender, EventArgs e)
    {
        Random r = new Random();
        int n = 10;
        int[] iar = new int[n];
    
        //Generate random numbers and store them in the Unsorted ListBox
        for (int i = 0; i < iar.Length; i++)
        {
            iar[i] = r.Next(0, 20);
            lb1.Items.Add(iar[i]);
        }
    
        //Sort the random numbers array
        Quicksort(iar, 0, iar.Length - 1);
    
        //Now the array is sorted put it in the Sorted Listbox
        for (int i = 0; i < iar.Length; i++)
        {
            lb2.Items.Add(iar[i]);
        }
    }
    

    QuickSort Function

    //Take an array of integers not an array of IComparable
    public static void Quicksort(int[] elements, int left, int right)
    {
        int i = left;
        int j = right;
    
        int pivot = elements[(left + right) / 2];
    
        while (i <= j)
        {
            while (elements[i].CompareTo(pivot) < 0)
            {
                i++;
            }
            while (elements[j].CompareTo(pivot) > 0)
            {
                j--;
            }
    
    
            if (i <= j)
            {
                // Swap
                int tmp = elements[i];
                elements[i] = elements[j];
                elements[j] = tmp;
    
                i++;
                j--;
            }
        }
        // Recursive calls
        if (left < j)
        {
            Quicksort(elements, left, j);
        }
        if (i < right)
        {
            Quicksort(elements, i, right);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a quicksort method to sort some or partial data.
I seem to be having some problems witn my quicksort method. I am trying
I'm trying out an implementation of QuickSort but getting an Exception in thread main
I'm trying to understand quicksort and I get the general idea, but I'm having
I'm trying to implement several different types of quicksort in java, but none of
I'm trying to use quick sort for my project but it doesn't work. And
I am trying to work out an efficient quicksort algo. It works okay, but
I'm trying to implement QuickSort algorithm program in Java, but I'm getting incorrect answer.
So I've been trying to implement a quicksort myself, but it generates a stackoverflowerror,
I am trying to write the code for quicksort but it keeps on going

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.