# Thread: a really hard sorting program can you help ??

1. Member Join Date
Oct 2010
Posts
5
Rep Power
0

## 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.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.  Reply With Quote

2. ## 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.  Reply With Quote

3. Member Join Date
Oct 2010
Posts
5
Rep Power
0

## sorry here is the code  Reply With Quote

4. Member Join Date
Oct 2010
Posts
5
Rep Power
0

## 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.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();
}

}```  Reply With Quote

5. ## 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).  Reply With Quote

#### Posting Permissions

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