package com.example.demo;
public class BubbleSort {
// Method to perform bubble sort on an integer array
public static void bubbleSort(int[] array) {
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
// If no two elements were swapped by inner loop, then break
if (!swapped) break;
}
}
// Main method to test the bubble sort
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Unsorted array:");
printArray(array);
bubbleSort(array);
System.out.println("Sorted array:");
printArray(array);
}
// Utility method to print an array
public static void printArray(int[] array) {
for (int value : array) {
System.out.print(value + " ");
}
System.out.println();
}
}
(2)简单的数字计算
(1)说明自己的需求
(2)等待它生成完成
(3)根据文件的代码来适当调整打印格式
(4)输出展示
cpp复制代码
package com.example.demo;
public class NineNineMultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d*%d=%-4d", j, i, i * j);
}
System.out.println();
}
}
}