Advertisement
mrScarlett

Dumb Bubble Sort

Aug 29th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class sort
  3. {
  4.     public static void main (String args []){
  5.         System.out.println("\f");
  6.         dumbBubbleSort();  
  7.    
  8.  
  9. }
  10.     public static void dumbBubbleSort() {
  11.      int [] nums = {22,11,9,6,3,2,1};
  12.      for (int pass = 0; pass < nums.length - 1; pass++) {  
  13.         //The outer loop will achieve the bubbling up of the highest number each pass
  14.            for (int i = 0; i < nums.length -1; i++) {
  15.            // The inner loop of "neighbor comparisons" get shorter by one each time (because of intArray.length -1)
  16.                if (nums[i] > nums[i+1]) {  //The "neighbor comparison"
  17.                     //Swap
  18.                     int temp = nums[i];        //To understand the need for temp, imagine trying to swap two glasses of
  19.                     nums[i] = nums[i+1];   //juice...you will need a third glass.
  20.                     nums[i+1] = temp;
  21.                }  
  22.                System.out.println(Arrays.toString(nums));
  23.           }
  24.      }
  25. }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement