如何将桶排序按降序排序

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

How to sort a bucketsort in descending order

问题

  1. import java.util.*;
  2. public class BucketSort {
  3. public static void main(String[] args) {
  4. int[] intArr = {47, 85, 10, 45, 16, 34, 67, 80, 34, 4, 0, 99};
  5. //int[] intArr = {21,11,33,70,5,25,65,55};
  6. System.out.println("Original array- " + Arrays.toString(intArr));
  7. bucketSort(intArr, 10);
  8. System.out.println("Sorted array after bucket sort- " + Arrays.toString(intArr));
  9. }
  10. private static void bucketSort(int[] intArr, int noOfBuckets){
  11. // Create bucket array
  12. List<Integer>[] buckets = new List[noOfBuckets];
  13. // Associate a list with each index
  14. // in the bucket array
  15. for(int i = 0; i < noOfBuckets; i++){
  16. buckets[i] = new LinkedList<>();
  17. }
  18. // Assign numbers from array to the proper bucket
  19. // by using hashing function
  20. for(int num : intArr){
  21. //System.out.println("hash- " + hash(num));
  22. buckets[hash(num)].add(num);
  23. }
  24. // sort buckets
  25. for(List<Integer> bucket : buckets){
  26. Collections.sort(bucket);
  27. }
  28. int i = 0;
  29. // Merge buckets to get sorted array
  30. for(List<Integer> bucket : buckets){
  31. for(int num : bucket){
  32. intArr[i++] = num;
  33. }
  34. }
  35. }
  36. // A very simple hash function
  37. private static int hash(int num){
  38. return num/10;
  39. }
  40. }
英文:

please help me, I really cant figure it out.I just found this code on the internet about bucket sort and was wondering if it could be sorted in descending order? I tried using reverse() but it doesnt seem to work it still comes out as ascending order.

  1. import java.util.*;
  2. public class BucketSort {
  3. public static void main(String[] args) {
  4. int[] intArr = {47, 85, 10, 45, 16, 34, 67, 80, 34, 4, 0, 99};
  5. //int[] intArr = {21,11,33,70,5,25,65,55};
  6. System.out.println(&quot;Original array- &quot; + Arrays.toString(intArr));
  7. bucketSort(intArr, 10);
  8. System.out.println(&quot;Sorted array after bucket sort- &quot; + Arrays.toString(intArr));
  9. }
  10. private static void bucketSort(int[] intArr, int noOfBuckets){
  11. // Create bucket array
  12. List&lt;Integer&gt;[] buckets = new List[noOfBuckets];
  13. // Associate a list with each index
  14. // in the bucket array
  15. for(int i = 0; i &lt; noOfBuckets; i++){
  16. buckets[i] = new LinkedList&lt;&gt;();
  17. }
  18. // Assign numbers from array to the proper bucket
  19. // by using hashing function
  20. for(int num : intArr){
  21. //System.out.println(&quot;hash- &quot; + hash(num));
  22. buckets[hash(num)].add(num);
  23. }
  24. // sort buckets
  25. for(List&lt;Integer&gt; bucket : buckets){
  26. Collections.sort(bucket);
  27. }
  28. int i = 0;
  29. // Merge buckets to get sorted array
  30. for(List&lt;Integer&gt; bucket : buckets){
  31. for(int num : bucket){
  32. intArr[i++] = num;
  33. }
  34. }
  35. }
  36. // A very simple hash function
  37. private static int hash(int num){
  38. return num/10;
  39. }
  40. }

答案1

得分: 1

因为您在对集合进行排序时没有传递比较器,所以默认情况下会按升序执行排序。

  1. import java.util.*;
  2. public class BucketSort {
  3. public static void main(String[] args) {
  4. int[] intArr = { 47, 85, 10, 45, 16, 34, 67, 80, 34, 4, 0, 99 };
  5. // int[] intArr = {21,11,33,70,5,25,65,55};
  6. System.out.println("原始数组- " + Arrays.toString(intArr));
  7. bucketSort(intArr, 10);
  8. System.out.println("桶排序后的数组- " + Arrays.toString(intArr));
  9. }
  10. private static void bucketSort(int[] intArr, int noOfBuckets) {
  11. // 创建桶数组
  12. List<Integer>[] buckets = new List[noOfBuckets];
  13. // 为每个索引在桶数组中关联一个列表
  14. for (int i = 0; i < noOfBuckets; i++) {
  15. buckets[i] = new LinkedList<>();
  16. }
  17. // 使用散列函数将数组中的数字分配到相应的桶中
  18. for (int num : intArr) {
  19. buckets[hash(num)].add(num);
  20. }
  21. // 对桶进行排序
  22. for (List<Integer> bucket : buckets) {
  23. Collections.sort(bucket, Collections.reverseOrder());
  24. }
  25. int i = 0;
  26. // 合并桶以获取排序后的数组
  27. for(int j = buckets.length - 1; j >= 0; j--){
  28. for (int num : buckets[j]) {
  29. intArr[i++] = num;
  30. }
  31. }
  32. }
  33. // 一个非常简单的散列函数
  34. private static int hash(int num) {
  35. return num / 10;
  36. }
  37. }
英文:

Since you didn't pass the comparator while sorting the Collection, so the sorting is performed in ascending order by default.

  1. import java.util.*;
  2. public class BucketSort {
  3. public static void main(String[] args) {
  4. int[] intArr = { 47, 85, 10, 45, 16, 34, 67, 80, 34, 4, 0, 99 };
  5. // int[] intArr = {21,11,33,70,5,25,65,55};
  6. System.out.println(&quot;Original array- &quot; + Arrays.toString(intArr));
  7. bucketSort(intArr, 10);
  8. System.out.println(&quot;Sorted array after bucket sort- &quot; + Arrays.toString(intArr));
  9. }
  10. private static void bucketSort(int[] intArr, int noOfBuckets) {
  11. // Create bucket array
  12. List&lt;Integer&gt;[] buckets = new List[noOfBuckets];
  13. // Associate a list with each index
  14. // in the bucket array
  15. for (int i = 0; i &lt; noOfBuckets; i++) {
  16. buckets[i] = new LinkedList&lt;&gt;();
  17. }
  18. // Assign numbers from array to the proper bucket
  19. // by using hashing function
  20. for (int num : intArr) {
  21. // System.out.println(&quot;hash- &quot; + hash(num));
  22. buckets[hash(num)].add(num);
  23. }
  24. // sort buckets
  25. for (List&lt;Integer&gt; bucket : buckets) {
  26. Collections.sort(bucket, Collections.reverseOrder());
  27. }
  28. int i = 0;
  29. // Merge buckets to get sorted array
  30. for(int j = buckets.length - 1; j &gt;= 0; j--){
  31. for (int num : buckets[j]) {
  32. intArr[i++] = num;
  33. }
  34. }
  35. }
  36. // A very simple hash function
  37. private static int hash(int num) {
  38. return num / 10;
  39. }
  40. }

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

发表评论

匿名网友

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

确定