Im trying to write a program to perform a recursive sort on a string using only 2 processes:
- Move the last character to the front and have everything else shift to the right.
- Swap the first and second character in the array.
so for example if i have a char array {"b","e","c","a","d"};
In this test case the program would perform the processes in following order:
2111212 to eventually get the desired output ("abcde").
Im trying to figure out what program logic I need to use to achieve this behavior, here is what I tried so far:
private static int sort(String[] items, String[] target, int first, int last) {
if(Arrays.equals(target, items)) {
return 1;
}
else if(items[first].compareTo(items[last]) > 0) {
String temp = items[last];
for(int i = last-1; i >= 0; i--) {
items[i+1] = items[i];
}
items[first] = temp;
System.out.print("s ");
System.out.println(Arrays.toString(items));
return sort(items,target,first,last);
}
else if (items[first].compareTo(items[first+1]) < 0){
String temp = items[first];
items[first] = items[first+1];
items[first+1] = temp;
System.out.print("b ");
System.out.println(Arrays.toString(items));
return sort(items,target,first,last);
}
return 2;
}
The output isn't quite correct, I think I have a logical error in there somewhere.