Advertisement
mrScarlett

Smarter Bubble Sort 1

Aug 29th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class smarterSort
  3. {
  4.     public static void main (String args []){
  5.         System.out.println("\f");
  6.         smarterBubbleSort1();  
  7.    
  8.  
  9. }
  10.     public static void smarterBubbleSort1() {
  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 - pass; i++) {
  15.           // The inner loop of "neighbor comparisons" get shorter by one each time (because of intArray.length - 1 - pass)
  16.                if (nums[i] > nums[i+1]) { //The "neighbor comparison"
  17.                     //Swap
  18.                     int temp = nums[i];  
  19.                     nums[i] = nums[i+1];  
  20.                     nums[i+1] = temp;
  21.                }
  22.                System.out.println(Arrays.toString(nums));
  23.           }
  24.      }
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement