Results 1 to 5 of 5
- 10-19-2010, 01:52 PM #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[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 !!!
- 10-19-2010, 02:31 PM #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.
- 10-19-2010, 02:36 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
sorry here is the code
- 10-19-2010, 02:37 PM #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[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(); } }
- 10-19-2010, 02:48 PM #5
Similar Threads
-
Use hard path to jar file instead of copy
By pdfbq in forum New To JavaReplies: 5Last Post: 01-14-2012, 07:27 PM -
How hard is it.....
By neilp123 in forum New To JavaReplies: 4Last Post: 07-22-2010, 04:21 PM -
Retrieve Image From Hard Disk
By sayan751 in forum AWT / SwingReplies: 3Last Post: 02-20-2009, 03:29 PM -
Help with a long sorting program.
By leiferouis in forum New To JavaReplies: 12Last Post: 02-04-2009, 04:46 AM -
Help with Sorting Program
By rhm54 in forum New To JavaReplies: 3Last Post: 01-25-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks