Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BubbleSort{
- public String[] bubbleSort(<DataType>[] toSort) {
- for (int i = 0; i < toSort.length; i++) {
- for (int j = 1; j < toSort.length; j++) {
- int compareValue = Character.compare(toSort[j].charAt(0), toSort[j - 1].charAt(0));
- if (compareValue == 0) {
- int index = 0;
- while (compareValue == 0 && index < toSort[j].length() && index < toSort[j - 1].length()) {
- compareValue = Character.compare(toSort[j].charAt(index), toSort[j - 1].charAt(index));
- index++;
- if (compareValue < 1) {
- swapValues(toSort, j, j - 1);
- }
- }
- } else if (compareValue < 0) {
- swapValues(toSort, j, j - 1);
- }
- }
- }
- return toSort;
- }
- }//O(n^2)
Add Comment
Please, Sign In to add comment