Results 1 to 5 of 5
  1. #1
    splash128 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default a really hard sorting program can you help ??

    I am required as an assignment to do this
    Code:
    a) You are required to implement a sorting algorithm which is called the 007 sorting technique. In this

    algorithm, you sort the numb ers according to their digits starting by the right most digit.

    For example, the numb er 2016, is to b e broken down into its basic comp onents. The 2 is in

    the thousands, the 0 is in the hundreds, the 1 is in the tens, and 6 is in some units. Thus

    given some numb ers, they can b e sorted according to their digits starting by the right most

    one which is here the 6.

    The algorithm starts by sorting numb ers according to their right most bits, and then moves

    to the one that is on its left i.e the 2nd right most bit and sort the array resulting from the

    previous pass according to that bit -i.e. the 2nd right most bit-, until it reaches the end -i.e.

    the leftmost bit- at which p oint, the array will b e sorted.

    The Sort consists of several iterations though the data, with each pass, making it more and

    more sorted. The numb er of iterations needed will b e equal to the numb er of digits in the

    largest element of the array to b e sorted.

    Within every pass to sort the elements according to the ith digit you may use an additional

    one dimensional array.

    For example, you have the following array:

    133 555555 0 8907 666 44444444 125

    In the rst pass, we will sort the array elements according to the rightmost bit, the result will b e

    as follows:

    0 133 44444444 555555 125 666 8907

    After nishing the second pass, array will b e as follows:

    0 8907 125 133 44444444 555555 666

    After nishing the third pass, array will b e as follows:

    0 125 133 44444444 555555 666 8907

    After the nth pass, the indices array will b e equal to:

    0 125 133 666 8907 555555 44444444

    this is my code
    Code:
    class Sort007{
    public static void main(String[] args) {
    int[]A={133,555555,0,8907,666,44444444,125};
    String[] AA=new String[A.length];
    String a;
    String b;
    String tmp;
    for(int i=0;i<A.length;i++){
    AA[i]=""+A[i];
    }
    int max=AA[0].length();
    for(int i=1;i<AA.length;i++){
    if(AA[i].length()>max){
    max=AA[i].length();
    }
    }
    for(int j=0;j<max;j++){
    for(int i=0;i<AA.length;i++){
    for(int k=0;k<AA.length-1-i;k++){
    if(AA[k].length()>j ){
    a=""+(AA[k+1].charAt(AA[k+1].length()-1-j));
    b=""+(AA[k].charAt(AA[k].length()-1-j));
    if(Integer.parseInt(a)<Integer.parseInt(b)){
    tmp=AA[k];
    AA[k]=AA[k+1];
    AA[k+1]=tmp;

    }

    }

    }
    }
    for(int f=0;f<AA.length;f++){

    System.out.print(AA[f]+",");

    }
    System.out.println();
    }

    }
    }

    but every time I try it I get this output and this error
    Code:
    --------------------Configuration: <Default>--------------------
    0,133,44444444,555555,125,666,8907,
    0,8907,125,133,44444444,555555,666,
    0,125,133,44444444,555555,666,8907,
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at Sort007.main(007.java:21)

    Process completed.

    can you please help I can't find what's wrong !!!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    It's telling you exactly what's wrong. On line 21, you're trying to access a String index of -1, which is obviously a no-no.

    If you want more help, you'll have to post some formatted code using the code tags. Also, you should use more meaningful variable names that follow the standard naming conventions (variables start with a lower-case letter, for example) if you want other people to read through your code.

  3. #3
    splash128 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    sorry here is the code

  4. #4
    splash128 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    class Sort007{
        public static void main(String[] args) {
            int[]A={133,555555,0,8907,666,44444444,125};
            String[] AA=new String[A.length];
            String a;
            String b;
            String tmp;
            for(int i=0;i<A.length;i++){
                AA[i]=""+A[i];
            }
            int max=AA[0].length();
            for(int i=1;i<AA.length;i++){
                if(AA[i].length()>max){
                    max=AA[i].length();
                }
            }
            for(int j=0;j<max;j++){
                for(int i=0;i<AA.length;i++){
                    for(int k=0;k<AA.length-1-i;k++){
                        if(AA[k].length()>j ){
                            a=""+(AA[k+1].charAt(AA[k+1].length()-1-j));
                            b=""+(AA[k].charAt(AA[k].length()-1-j));
                        if(Integer.parseInt(a)<Integer.parseInt(b)){    
                            tmp=AA[k];
                            AA[k]=AA[k+1];
                            AA[k+1]=tmp;
                            
                        }
                            
                        }
                        
                    }
                }
                for(int f=0;f<AA.length;f++){
    
                    System.out.print(AA[f]+",");
    
                }
                System.out.println();
            }
            
        }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    That's fine. It doesn't change anything I told you. What are you doing on line 21? Step through it with a debugger if you can't figure out what's going on. At least put some print statements in there so you know what index you're trying to access (hint: it's -1, and it shouldn't be).

Similar Threads

  1. Use hard path to jar file instead of copy
    By pdfbq in forum New To Java
    Replies: 5
    Last Post: 01-14-2012, 08:27 PM
  2. How hard is it.....
    By neilp123 in forum New To Java
    Replies: 4
    Last Post: 07-22-2010, 05:21 PM
  3. Retrieve Image From Hard Disk
    By sayan751 in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2009, 04:29 PM
  4. Help with a long sorting program.
    By leiferouis in forum New To Java
    Replies: 12
    Last Post: 02-04-2009, 05:46 AM
  5. Help with Sorting Program
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 11:08 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
  •