英文:
Java program to create a Descending Order using bucket sort
问题
以下是翻译好的内容:
public class BucketSort{
    Scanner in = new Scanner(System.in);
    
    public void bucketSort(int array[], int numBuckets){
      
        System.out.println("选择排序顺序:");
        System.out.println("[A] 升序 \n[D] 降序 \n 输入选择: ");
        char choice2 = in.next().charAt(0);
        
        if (choice2 == 'A') {
            List<Integer>[] buckets = new List[numBuckets];
            System.out.println("用户输入的值为 " + Arrays.toString(array));
            System.out.println("算法选择为桶排序");
            System.out.println("排序顺序选择为升序");
            
            // 创建空的桶
            for(int i = 0; i < numBuckets; i++){
                buckets[i] = new LinkedList<>();
            }
            
            for(int num : array){
                buckets[hash(num, numBuckets)].add(num);
            }
            
            for(List<Integer> bucket : buckets) {
                Collections.sort(bucket);
            }
            
            int i = 0;
            for(List<Integer> bucket : buckets) {
                for (int num : bucket){
                    array[i++] = num;
                }
            }
        }
        
        // 在这里实现降序排序的部分
        
    }
    
    private static int hash(int num, int numBuckets) {
        return num / numBuckets;
    }
}
英文:
I was making a program to take 10 user input values(1-100) and use bucket sort to sort it according to the user preference whether by ascending or descending order. I was able to create the ascending order. This is my code for ascending, but how could I make it descending?
 public class BucketSort{
Scanner in = new Scanner(System.in);
public  void bucketSort(int array[], int numBuckets){
System.out.println("Choose Sorting Order:");
System.out.println("[A] Ascending \n[D] Descending \n Enter choice: ");
char choice2 = in.next().charAt(0);
if (choice2 == 'A') {
List<Integer>[] buckets = new List[numBuckets];
System.out.println("The user inputted values are " + Arrays.toString(array));
System.out.println("Algorithm choice is Bucket Sort");
System.out.println("The sorting order choice is Ascending");
// Creates empty buckets
for(int i =0; i < numBuckets; i++){
buckets[i] = new LinkedList<>();
}
for(int num: array){
buckets[hash(num, numBuckets)].add(num);
}
for(List<Integer> bucket :buckets) {
Collections.sort(bucket);
}
int i = 0;
for(List <Integer> bucket : buckets) {
for (int num: bucket){
array[i++] = num;
}
}
}
private static int hash(int num, int numBuckets) {
return num/numBuckets;
}
}
答案1
得分: 0
尝试以下操作:
Collections.sort(bucket, Collections.reverseOrder());
请记住,在将这些桶组合成数组时,您必须反转桶的顺序:
for(int index = buckets.length - 1; index >= 0; index--) {
    for (int num: buckets[index]){
       array[i++] = num;
    }
}
英文:
Try the following:
Collections.sort(bucket, Collections.reverseOrder());
Keep in mind that you have to reverse the order of the buckets when you combine them into the array:
for(int index = buckets.length - 1; index >= 0; index--) {
for (int num: buckets[index]){
array[i++] = num;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论