🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 1. 冒泡排序 ~~~ public void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { swap(arr, j, j + 1); } } } } private void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } ~~~ # 2. 双端冒泡排序 ~~~ public void bBubbleSort(int[] arr) { int left = 0, right = arr.length - 1; while (left < right) { for (int j = left; j < right; j++) { if (arr[j] > arr[j + 1]) { swap(arr, j, j + 1); } } right--; for (int j = right; j > left; j--) { if (arr[j - 1] > arr[j]) { swap(arr, j - 1, j); } } left++; } } ~~~ # 3. 快排 ~~~ public void quickSort(int[] arr) { qSort(arr, 0, arr.length - 1); } private void qSort(int[] arr, int left, int right) { if (left < right) { int value = partition(arr, left, right); qSort(arr, left, value - 1); qSort(arr, value + 1, right); } } private int partition(int[] arr, int left, int right) { int value = arr[left]; while (left < right) { while (left < right && arr[right] >= value) { right--; } swap(arr, right, left); while (left < right && arr[left] <= value) { left++; } swap(arr, right, left); } return left; } ~~~