QuickSort Array Out of Bound problem

Discussion in 'C#' started by shah123, Mar 27, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Now i have another problem when i get an array using

    Code:
    getArray(int [] arr)
    for (i = 0; i < arr.Length; i++)
                    {
    
                        String val = Console.ReadLine();
                        arr[i] = Convert.ToInt32(val);
    }
     
     public static void quicks(int lo, int hi, int[] arr)
            {
                int i = lo;
                hi = arr.Length;
                int j = hi;
                int pivot;
                pivot = arr[(lo + hi) / 2];
    
                if (lo >= hi)
                {
                    return;
                }
                Console.WriteLine(pivot);
                
                    do
                    {
                        while (arr[i] < pivot)
                        i++;
                        while (arr[j] > pivot) // when it comes here it says Array
                                              //  out of bound problem   
                        j--;
    
                        if (i < j)
                        {
                        int temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                        }
                    
                } while(i < j ); // end of do condition
    
                if (lo < j) quicks(lo, j,arr);
                if (i < hi) quicks(i, hi,arr);
    [/COLOR] 
    Please help me in correcting quicksort function
    :confused:
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What is the initial value of of hi and lo that is passed to the function.

    The exception you are talking about looks like hi is not within the range of arr.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    I have sorted this problem thanks a million for all help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice