Guest User

Untitled

a guest
May 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class SelectionSort{
  2. public <DataType>[] selectionSort(<DataType>[] toSort) {
  3. int minIndex;
  4. for (int i = 0; i < toSort.length; i++) {
  5. minIndex = toSort.length - 1;
  6. for (int j = i; j < toSort.length; j++) {
  7. int compareValue = Character.compare(toSort[j].charAt(0), toSort[minIndex].charAt(0));
  8. if (compareValue == 0) {
  9. int index = 0;
  10. while (compareValue == 0 && index < toSort[j].length() && index < toSort[minIndex].length()) {
  11. compareValue = Character.compare(toSort[j].charAt(0), toSort[minIndex].charAt(0));
  12. index++;
  13. if (compareValue < 0) {
  14. swapValues(toSort, i, minIndex);
  15. }
  16. }
  17. } else if (compareValue < 0) {
  18. minIndex = j;
  19. }
  20. }
  21. swapValues(toSort, i, minIndex);
  22.  
  23. }
  24. return toSort;
  25. }
  26. }//O(n^2)
Add Comment
Please, Sign In to add comment