Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class smarterSort
- {
- public static void main (String args []){
- System.out.println("\f");
- smarterBubbleSort1();
- }
- public static void smarterBubbleSort1() {
- int [] nums = {22,11,9,6,3,2,1};
- for (int pass = 0; pass < nums.length - 1; pass++) {
- //The outer loop will achieve the bubbling up of the highest number each pass
- for (int i = 0; i < nums.length - 1 - pass; i++) {
- // The inner loop of "neighbor comparisons" get shorter by one each time (because of intArray.length - 1 - pass)
- if (nums[i] > nums[i+1]) { //The "neighbor comparison"
- //Swap
- int temp = nums[i];
- nums[i] = nums[i+1];
- nums[i+1] = temp;
- }
- System.out.println(Arrays.toString(nums));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement