publicstaticvoidbuildHeap(int[] arrays, int size){ for (int i = size-1; i >= 0; i--) { heapify(arrays, i, size); } }
staticvoidheapify(int[] arrays, int currentRootNode, int size){ if (currentRootNode < size) { // arrays[0]作为哨兵,不存值,用于作为交换buffer int left = 2 * currentRootNode + 1; int right = 2 * currentRootNode + 2;
int max = currentRootNode;
if (left < size) { if (arrays[max] < arrays[left]) { max = left; } } if (right < size) { if (arrays[max] < arrays[right]) { max = right; } }
voidradixSort(int[] arrays){ int max = findMax(arrays, 0, arrays.length-1);
// 遍历次数由最大值的位数决定 for (int i = 1; max/i >= 0; i *= 10) { int[][] buckets = newint[arrays.length][10];
// 获取每一位数字放入桶中 for (int j = 0; j < arrays.length; j++) { int num = (arrays[j] / i) % 10; buckets[j][num] = arrays[j]; }
int k = 0;
for (int j = 0; j < 10; j++) { // 对每个桶中的元素进行回收 for (int l = 0; l < arrays.length; l++) { // 若桶中有数据则回收 if (buckets[l][j] != 0) { arrays[k++] = buckets[l][j]; } } } } }
voidfindMax(int[] arrays, int left, int right){ if (left == right) { return arrays[left]; } else { int a = arrays[left]; int b = findMax(arrays, left+1, right); // 找出整体的最大值