Advertisement
Chame

bubbleSort.java

Dec 19th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package edu.hdsb.rbhs.ics3u.closs.u4;
  2.  
  3. public class BubbleSort {
  4.  
  5. public static int[] data = new int [8];
  6. public static int numberOfSwaps;
  7. public static int numberOfComparisons;
  8. public static int i = 0;
  9. public static int j = i+1;
  10.  
  11.  
  12. public static void randomizingArray(){
  13.  
  14.  
  15.  
  16. for (i = 0; i < data.length; i++){
  17. data[i] = (int)(1000 * Math.random()) + 1;
  18.  
  19. System.out.print(data[i] + ", ");
  20. }
  21. }
  22.  
  23. public static void bubbleSort(){
  24.  
  25. for (i=0; i < data.length; i++){
  26. numberOfComparisons = numberOfComparisons + 1;
  27. if (data[i] > data[j]){
  28. int temp;
  29. temp = data[i];
  30. data[i] = data[j];
  31. data[j] = temp;
  32. numberOfSwaps = numberOfSwaps + 1;
  33.  
  34. }
  35. }
  36. }
  37. public static void main(String[] args) {
  38.  
  39. randomizingArray();
  40. bubbleSort();
  41.  
  42. System.out.println("\n");
  43. for (int i = 0; i < data.length; i++){
  44. System.out.print(data[i] + ", ");
  45. }
  46.  
  47. System.out.println("Number of Comparisons: " + numberOfComparisons + "\n" + "Number of Swaps: " + numberOfSwaps);
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement