LeetCode/com/zerroi/leetcode/sort/BubbleSort.java

29 lines
733 B
Java
Raw Normal View History

2024-03-03 20:08:28 +08:00
package com.zerroi.leetcode.sort;
public class BubbleSort {
public static void main(String[] args) {
Bubble bubble = new Bubble();
int[] res = bubble.bubble(new int[]{2, 1, 9, 3, 10});
for (int item : res) {
System.out.println("item = " + item);
}
}
}
class Bubble {
public int[] bubble(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - i - 1; j++) {
int temp;
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
return nums;
}
}