Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.hdsb.rbhs.ics3u.closs.u4;
- public class BubbleSort {
- public static int[] data = new int [8];
- public static int numberOfSwaps;
- public static int numberOfComparisons;
- public static int i = 0;
- public static int j = i+1;
- public static void randomizingArray(){
- for (i = 0; i < data.length; i++){
- data[i] = (int)(1000 * Math.random()) + 1;
- System.out.print(data[i] + ", ");
- }
- }
- public static void bubbleSort(){
- for (i=0; i < data.length; i++){
- numberOfComparisons = numberOfComparisons + 1;
- if (data[i] > data[j]){
- int temp;
- temp = data[i];
- data[i] = data[j];
- data[j] = temp;
- numberOfSwaps = numberOfSwaps + 1;
- }
- }
- }
- public static void main(String[] args) {
- randomizingArray();
- bubbleSort();
- System.out.println("\n");
- for (int i = 0; i < data.length; i++){
- System.out.print(data[i] + ", ");
- }
- System.out.println("Number of Comparisons: " + numberOfComparisons + "\n" + "Number of Swaps: " + numberOfSwaps);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement