请帮帮我,我真的不明白。我刚刚在互联网上找到了有关存储桶排序的代码,想知道它是否可以降序排序?我尝试使用reverse(),但是它似乎不起作用,它仍然以升序出现。
CREATE TABLE vi_vb(Vital STRING, VB STRING)
PARTITIONED BY(cTime STRING, VI STRING)
CLUSTERED BY(VI) SORTED BY(cTime) INTO 32 BUCKETS
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '1'
COLLECTION ITEMS TERMINATED BY '2'
MAP KEYS TERMINATED BY '3'
STORED AS SEQUENCEFILE;
答案 0 :(得分:0)
由于在对Collection进行排序时没有通过比较器,因此默认情况下,排序是按升序进行的。
import java.util.*;
public class BucketSort {
public static void main(String[] args) {
int[] intArr = { 47, 85, 10, 45, 16, 34, 67, 80, 34, 4, 0, 99 };
// int[] intArr = {21,11,33,70,5,25,65,55};
System.out.println("Original array- " + Arrays.toString(intArr));
bucketSort(intArr, 10);
System.out.println("Sorted array after bucket sort- " + Arrays.toString(intArr));
}
private static void bucketSort(int[] intArr, int noOfBuckets) {
// Create bucket array
List<Integer>[] buckets = new List[noOfBuckets];
// Associate a list with each index
// in the bucket array
for (int i = 0; i < noOfBuckets; i++) {
buckets[i] = new LinkedList<>();
}
// Assign numbers from array to the proper bucket
// by using hashing function
for (int num : intArr) {
// System.out.println("hash- " + hash(num));
buckets[hash(num)].add(num);
}
// sort buckets
for (List<Integer> bucket : buckets) {
Collections.sort(bucket, Collections.reverseOrder());
}
int i = 0;
// Merge buckets to get sorted array
for(int j = buckets.length - 1; j >= 0; j--){
for (int num : buckets[j]) {
intArr[i++] = num;
}
}
}
// A very simple hash function
private static int hash(int num) {
return num / 10;
}
}