You are required to implement a sorting algorithm which is called the 007 sorting technique. In this algorithm, you sort the numbers according to their digits starting by the right most digit.This is my code:
For example,
- the number 2016, is to be broken down into its basic com 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 numbers, they can be sorted according to their digits starting by the right most one which is here the 6.
The algorithm starts by sorting numbers 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 point, 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 number of iterations needed will b e equal to the numb er of digits in the largest element of the array to be 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 1st pass, we will sort the array elements according to the rightmost bit, the result will be as follows:
0 133 44444444 555555 125 666 8907
After the second pass, array will be as follows:
0 8907 125 133 44444444 555555 666
After the third pass, array will be as follows:
0 125 133 44444444 555555 666 8907
After the nth pass, the indices array will be equal to:
0 125 133 666 8907 555555 44444444
Expand|Select|Wrap|Line Numbers
- public class the007sorting
- {
- public static void main (String[] args)
- {
- int [] a ={133,555555,0,8907,666,44444444,125};
- String [] b = new String [a.length];
- String x;
- String y;
- String tmp;
- for (int i = 0 ; i < a.length ; i ++)
- {
- b [i] = "" + a[i];
- }
- int max = b [0].length();
- for (int i = 0 ; i < b.length ; i ++)
- {
- if (b [i].length() > max)
- {
- max = b[i].length();
- }
- }
- for (int j = 0 ; j < max ; j ++)
- {
- for (int i = 0 ; i < b.length ; i ++)
- {
- for (int k = 0 ; k < b.length-1-i ; k ++)
- {
- if (b[k].length() > j )
- {
- x = "" + (b[k+1].charAt(b[k+1].length()-1-j));
- y = "" + (b[k].charAt(b[k].length()-1-j));
- if (Integer.parseInt(x) < Integer.parseInt(y))
- {
- tmp = b[k];
- b[k] = b[k+1];
- b[k+1] = tmp;
- }
- }
- }
- }
- for (int m = 0 ; m < b.length ; m ++)
- {
- System.out.print(b[m] + ".");
- }
- System.out.println();
- }
- }
- }
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 !!!