以下是Java语言实现冒泡排序的示例代码:
java public class BubbleSort { public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public static void main(String[] args) { int[] arr = {5, 2, 8, 3, 1, 6}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } }
以上代码中,`bubbleSort`方法实现了冒泡排序算法,`main`方法中对一个数组进行排序并输出结果。其他排序算法的实现类似,只需要修改排序算法的实现即可。