package com.hcq.sort;
import java.util.Arrays;
/**
* 冒泡排序
*
* @author Administrator
*
*/
public class Main {
public static void main(String[] args) {
int[] a = { 1, 0, 2, 7, 9, 2, 3 };
Main.bubbleSort(a);
System.out.println(Arrays.toString(a));
Main.bubbleSort2(a);
System.out.println(Arrays.toString(a));
}
/**
* 冒泡排序方法一
*
* @param nums
*/
public static void bubbleSort(int[] nums) {
for (int k = 0; k < nums.length - 1; k++) {
for (int i = 0; i < nums.length - 1 - k; i++) {
if (nums[i] > nums[i + 1]) {
int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
}
}
/**
* 冒泡排序方法二
*
* @param nums
*/
public static void bubbleSort2(int[] nums) {
for (int k = 0; k < nums.length - 1; k++) {
for (int i = k + 1; i < nums.length; i++) {
if (nums[k] > nums[i]) {
int temp = nums[i];
nums[k] = nums[i];
nums[i] = temp;
}
}
}
}
}
冒泡排序
最新推荐文章于 2022-04-29 23:24:15 发布