冒泡排序,升序。释义参考百度百科
public static void bubbleSort(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = i; j < nums.length; j++) { if (nums[i] > nums[j]) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } }
测试代码:
import java.util.Arrays; public class BubbleSort { public static void main(String[] args) { int[] ints = {9, 12, 0, 5, -1}; bubbleSort(ints); System.out.println(Arrays.toString(ints)); } public static void bubbleSort(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = i; j < nums.length; j++) { if (nums[i] > nums[j]) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } } }
测试待排序数组:
9, 12, 0, 5, -1
结果输出:
[-1, 0, 5, 9, 12]
原文:https://www.cnblogs.com/convict/p/14657188.html