英文:
How to sort a bucketsort in descending order
问题
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);
}
int i = 0;
// Merge buckets to get sorted array
for(List<Integer> bucket : buckets){
for(int num : bucket){
intArr[i++] = num;
}
}
}
// A very simple hash function
private static int hash(int num){
return num/10;
}
}
英文:
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.
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);
}
int i = 0;
// Merge buckets to get sorted array
for(List<Integer> bucket : buckets){
for(int num : bucket){
intArr[i++] = num;
}
}
}
// A very simple hash function
private static int hash(int num){
return num/10;
}
}
答案1
得分: 1
因为您在对集合进行排序时没有传递比较器,所以默认情况下会按升序执行排序。
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("原始数组- " + Arrays.toString(intArr));
bucketSort(intArr, 10);
System.out.println("桶排序后的数组- " + Arrays.toString(intArr));
}
private static void bucketSort(int[] intArr, int noOfBuckets) {
// 创建桶数组
List<Integer>[] buckets = new List[noOfBuckets];
// 为每个索引在桶数组中关联一个列表
for (int i = 0; i < noOfBuckets; i++) {
buckets[i] = new LinkedList<>();
}
// 使用散列函数将数组中的数字分配到相应的桶中
for (int num : intArr) {
buckets[hash(num)].add(num);
}
// 对桶进行排序
for (List<Integer> bucket : buckets) {
Collections.sort(bucket, Collections.reverseOrder());
}
int i = 0;
// 合并桶以获取排序后的数组
for(int j = buckets.length - 1; j >= 0; j--){
for (int num : buckets[j]) {
intArr[i++] = num;
}
}
}
// 一个非常简单的散列函数
private static int hash(int num) {
return num / 10;
}
}
英文:
Since you didn't pass the comparator while sorting the Collection, so the sorting is performed in ascending order by default.
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论