Java程序以桶排序创建降序排列

huangapple go评论63阅读模式
英文:

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(&quot;Choose Sorting Order:&quot;);
System.out.println(&quot;[A] Ascending \n[D] Descending \n Enter choice: &quot;);
char choice2 = in.next().charAt(0);
if (choice2 == &#39;A&#39;) {
List&lt;Integer&gt;[] buckets = new List[numBuckets];
System.out.println(&quot;The user inputted values are &quot; + Arrays.toString(array));
System.out.println(&quot;Algorithm choice is Bucket Sort&quot;);
System.out.println(&quot;The sorting order choice is Ascending&quot;);
// Creates empty buckets
for(int i =0; i &lt; numBuckets; i++){
buckets[i] = new LinkedList&lt;&gt;();
}
for(int num: array){
buckets[hash(num, numBuckets)].add(num);
}
for(List&lt;Integer&gt; bucket :buckets) {
Collections.sort(bucket);
}
int i = 0;
for(List &lt;Integer&gt; 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 &gt;= 0; index--) {
for (int num: buckets[index]){
array[i++] = num;
}

huangapple
  • 本文由 发表于 2020年9月14日 17:31:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63881681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定