thorax232

Java - Bubble Sort

Nov 8th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. public static double[] bubble(double[] list)
  2.    {
  3.       double temp;
  4.       boolean changed = false;
  5.       do
  6.       {
  7.          changed = false; // Resets to false every time
  8.          for(int j = 0; j < list.length - 1; j++) // Minus one because if uses plus one to look at next
  9.             if(list[j] > list[j + 1]) // If next value is higher
  10.             {
  11.                temp = list[j]; // Shift value into temporary spot
  12.                list[j] = list[j + 1]; // Shift higher value into value (i)
  13.                list[j + 1] = temp; // Shift temp into higher value (swap)
  14.                changed = true;
  15.             }
  16.       } while(changed); // Stop when there's no change
  17.      
  18.       return list;
  19.    }
Advertisement
Add Comment
Please, Sign In to add comment