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

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

Java program to create a Descending Order using bucket sort

问题

以下是翻译好的内容:

  1. public class BucketSort{
  2. Scanner in = new Scanner(System.in);
  3. public void bucketSort(int array[], int numBuckets){
  4. System.out.println("选择排序顺序:");
  5. System.out.println("[A] 升序 \n[D] 降序 \n 输入选择: ");
  6. char choice2 = in.next().charAt(0);
  7. if (choice2 == 'A') {
  8. List<Integer>[] buckets = new List[numBuckets];
  9. System.out.println("用户输入的值为 " + Arrays.toString(array));
  10. System.out.println("算法选择为桶排序");
  11. System.out.println("排序顺序选择为升序");
  12. // 创建空的桶
  13. for(int i = 0; i < numBuckets; i++){
  14. buckets[i] = new LinkedList<>();
  15. }
  16. for(int num : array){
  17. buckets[hash(num, numBuckets)].add(num);
  18. }
  19. for(List<Integer> bucket : buckets) {
  20. Collections.sort(bucket);
  21. }
  22. int i = 0;
  23. for(List<Integer> bucket : buckets) {
  24. for (int num : bucket){
  25. array[i++] = num;
  26. }
  27. }
  28. }
  29. // 在这里实现降序排序的部分
  30. }
  31. private static int hash(int num, int numBuckets) {
  32. return num / numBuckets;
  33. }
  34. }
英文:

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?

  1. public class BucketSort{
  2. Scanner in = new Scanner(System.in);
  3. public void bucketSort(int array[], int numBuckets){
  4. System.out.println(&quot;Choose Sorting Order:&quot;);
  5. System.out.println(&quot;[A] Ascending \n[D] Descending \n Enter choice: &quot;);
  6. char choice2 = in.next().charAt(0);
  7. if (choice2 == &#39;A&#39;) {
  8. List&lt;Integer&gt;[] buckets = new List[numBuckets];
  9. System.out.println(&quot;The user inputted values are &quot; + Arrays.toString(array));
  10. System.out.println(&quot;Algorithm choice is Bucket Sort&quot;);
  11. System.out.println(&quot;The sorting order choice is Ascending&quot;);
  12. // Creates empty buckets
  13. for(int i =0; i &lt; numBuckets; i++){
  14. buckets[i] = new LinkedList&lt;&gt;();
  15. }
  16. for(int num: array){
  17. buckets[hash(num, numBuckets)].add(num);
  18. }
  19. for(List&lt;Integer&gt; bucket :buckets) {
  20. Collections.sort(bucket);
  21. }
  22. int i = 0;
  23. for(List &lt;Integer&gt; bucket : buckets) {
  24. for (int num: bucket){
  25. array[i++] = num;
  26. }
  27. }
  28. }
  29. private static int hash(int num, int numBuckets) {
  30. return num/numBuckets;
  31. }
  32. }

答案1

得分: 0

尝试以下操作:

  1. Collections.sort(bucket, Collections.reverseOrder());

请记住,在将这些桶组合成数组时,您必须反转桶的顺序:

  1. for(int index = buckets.length - 1; index >= 0; index--) {
  2. for (int num: buckets[index]){
  3. array[i++] = num;
  4. }
  5. }
英文:

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:

  1. for(int index = buckets.length - 1; index &gt;= 0; index--) {
  2. for (int num: buckets[index]){
  3. array[i++] = num;
  4. }

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:

确定