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