Guest User

Untitled

a guest
Jul 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /**
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package homework8;
  7. /**
  8. *
  9. * @author ben
  10. */
  11.  
  12. public class BubbleSort implements Sort {
  13.  
  14. int comparisons = 0;
  15. int swaps = 0;
  16.  
  17. public void resetStats() {
  18. comparisons = 0;
  19. swaps = 0;
  20. }
  21.  
  22. public int getNumComparisons() {
  23. return comparisons;
  24. }
  25.  
  26. public int getNumSwaps() {
  27. return swaps;
  28. }
  29.  
  30. public <T extends Comparable<T>> void sortHW8(T[] table) {
  31. int pass = 1;
  32. boolean exchanges = false;
  33. do{
  34. exchanges = false;
  35. for (int i = 0; i < table.length - pass; i++){
  36. if (table[i].compareTo(table[i + 1]) > 0){
  37. T temp = table[i];
  38. table[i] = table[i+1];
  39. table[i+1] = temp;
  40. exchanges = true;
  41. }
  42. }
  43. pass++;
  44. } while (exchanges);
  45. }
  46.  
  47. public void sortHW8(int[] arr) {
  48. throw new UnsupportedOperationException("Not supported yet.");
  49. }
  50. }
Add Comment
Please, Sign In to add comment