void bubbleSort(int[] array) {for (int i = 0; i < array.length; i++) {
int flag = 0;
for (int j = i; j < array.length - 1; j++) {if (array[j] > array[j + 1]) {exchange(array, j, j + 1);
++flag;
}
}
if (flag == 0)
break;
}
}
void exchange(int[] array, int i, int j) {array[i] ^= array[j];
array[j] ^= array[i];
array[i] ^= array[j];
}
void partition(int[] array, int start, int end) {if (start > end)
return;
int i = start, j = end, val = array[i];
while (i < j) {while (i < j && val < array[j])
j--;
array[i] = array[j];
while (i < j && array[i] < val)
i++;
array[j] = array[i];
}
array[i] = val;
partition(array, start, i - 1);
partition(array, i + 1, end);
}
void heapSort(int[] array) {
// 构建大顶堆
for (int i = (array.length / 2 - 1); i >= 0; i--) {heapAdjust(array, i, array.length);
}
for (int i = array.length - 1; i > 0; i--) {exchange(array, 0, i);// 将堆顶元素与末尾元素进行交换
heapAdjust(array, 0, i);// 重新对堆进行调整
}
}
void heapAdjust(int[] array, int root, int length) {while (root < length) {
int k = root * 2 + 1;
if (k < length && array[k] < array[k + 1])
k++;
if (k < length && array[k] > array[root]) {exchange(array, root, k);
root = k;
} else {break;}
}
}
void exchange(int[] array, int i, int j) {array[i] ^= array[j];
array[j] ^= array[i];
array[i] ^= array[j];
}