Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. // Usage:
  2. // > java BSort Petra Bob Peter Adam Zap John Adam
  3. // Adam Adam Bob John Peter Petra Zap
  4.  
  5. class BSort {
  6. public static void main(String[] args) {
  7. bubbleSortSmart(args);
  8. for(String arg : args) System.out.print(arg + " ");
  9. System.out.println();
  10. System.exit(0);
  11.  
  12. }
  13. static void bubbleSortSmart(String[] a) {
  14. int newIdx = a.length - 1;
  15. int oldIdx = 0;
  16. while(newIdx != oldIdx) {
  17. oldIdx = newIdx;
  18. for(int i = 0; i < oldIdx; i++) {
  19. if (a[i].compareTo(a[i+1]) > 0) {
  20. String tmp = a[i];
  21. a[i] = a[i+1];
  22. a[i+1] = tmp;
  23. newIdx = i;
  24. }
  25. }
  26. }
  27. }
  28. static void bubbleSortNaive(String[] a) {
  29. int maxIdx = a.length - 1;
  30. while(maxIdx != 0) {
  31. for(int i = 0; i < maxIdx; i++) {
  32. if (a[i].compareTo(a[i+1]) > 0) {
  33. String tmp = a[i];
  34. a[i] = a[i+1];
  35. a[i+1] = tmp;
  36. }
  37. }
  38. maxIdx--;
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement