public class BubbleSort {
	public static void main(String[] args) {
		int[] arr = {5, 7, 4, 3, 6, 2};
		bubbleSort(arr);
		System.out.println("Sorted array: " + Arrays.toString(arr));
	}

	public static void BubbleSort(int[] arr) {
		int n = arr.length;
		for (int i = 0; i < n - 1; i++) {
			boolean swapped = false;
			for (int j = 0; j < n - i - 1; j++) {
				if (arr[j] > arr[j + 1]) {
					in temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
					swapped = true;
				}
			}
			if (!swapped) break;
		}
	}
}
posted on 2025-07-21 09:57  caoshikui  阅读(6)  评论(0)    收藏  举报