Results 1 to 3 of 3
  1. #1
    doobybug is offline Member
    Join Date
    Mar 2008
    Posts
    17
    Rep Power
    0

    Default Help with a sorting algorithm

    This algorithm is supposed to put an array in descending order but at some point the variable lo is reassigned to the number 2 and I have no idea why!!

    public class Test{
    static int data[] = {37, 11, 52, 7};
    static int lo = 0;
    static int hi = 3;

    public static void main (String args[]){
    proc(data, lo, hi);
    }

    public static void proc(int[] data, int lo, int hi){
    if (lo < hi){
    if (data [lo] < data[lo + 1]){
    int t = data[lo + 1];
    data[lo + 1] = data[lo];
    data[lo] = t;
    }
    proc (data, lo + 1, hi);
    if (lo == 0){
    proc (data, 0, hi-1);
    }
    }
    }
    }

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    I just ran your code and it just work correctly. I iterated over the array to see the descending order of integers, and everything looked fine.

    Even the variables lo and hi are printed with their default values, 0 and 3.

    What is your problem in this program?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    doobybug is offline Member
    Join Date
    Mar 2008
    Posts
    17
    Rep Power
    0

    Default Still not understanding

    I am running this code with a debugger to check how the variables are changing and at one point in time the variable lo changes from 3 to 2 by first going to the last curly bracket of the method and jumps to the if(lo==0) statement. I cannot understand this when there is in no part lo= lo-1; or something similar. I highlighted the parts where it jumps from one statement to another

    public class Test{


    public static void main (String args[]){
    int data[] = {37, 11, 52, 7};
    int lo = 0;
    int hi = 3;
    proc(data, lo, hi);
    }

    public static void proc(int[] data, int lo, int hi){
    if (lo < hi){
    if (data [lo] < data[lo + 1]){
    int t = data[lo + 1];
    data[lo + 1] = data[lo];
    data[lo] = t;
    }
    proc (data, lo + 1, hi);
    if (lo == 0)
    proc (data, 0, hi-1);

    }
    }
    }

Similar Threads

  1. Algorithm help? :)
    By Mirix in forum New To Java
    Replies: 6
    Last Post: 05-24-2010, 03:08 AM
  2. Need some help in an algorithm
    By ea09530 in forum New To Java
    Replies: 3
    Last Post: 04-04-2010, 02:13 PM
  3. Help with an Algorithm
    By Manfizy in forum New To Java
    Replies: 22
    Last Post: 07-03-2009, 08:16 AM
  4. O(log n) algorithm help !!!!!!
    By itseeker87 in forum New To Java
    Replies: 8
    Last Post: 09-09-2008, 06:12 PM
  5. Help with algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 11:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •