Advertisement
uopspop

Untitled

Dec 24th, 2020
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package com.basic;
  2.  
  3. public class Sort_Bubble_new {
  4.     public static void bubble_sort(int[] nums) {
  5.         for (int round = 0; round < nums.length; round++) {
  6.             // int len = nums.length - round; // improved version
  7.             int len = nums.length;
  8.             for (int i_runner = 1; i_runner < len; i_runner++) {
  9.                 if (nums[i_runner - 1] > nums[i_runner]) { // if faster, swap
  10.                     swap(nums, i_runner - 1, i_runner);
  11.                 }
  12.             }
  13.         }
  14.     }
  15.  
  16.     private static void swap(int[] nums, int i_left, Integer i_right) {
  17.         int tmp = nums[i_left];
  18.         nums[i_left] = nums[i_right];
  19.         nums[i_right] = tmp;
  20.     }
  21.  
  22.     public static void main(String[] args) {
  23.         int[] nums = new int[]{8, 2, 6, 10, 4};
  24.         Sort_Bubble_new.bubble_sort(nums);
  25.  
  26.         System.out.println();
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement