使用节点而不是整数在Java中实现基数排序
Posted
技术标签:
【中文标题】使用节点而不是整数在Java中实现基数排序【英文标题】:Implementation of Radix sort in Java using Nodes instead of integers 【发布时间】:2015-12-21 12:26:52 【问题描述】:我的数据结构类有一个最终项目,但我不知道该怎么做。我需要实现基数排序,并且我在很大程度上理解了这个概念。但是到目前为止,我在网上找到的所有实现都严格使用整数,我需要将它与我创建的另一个类型称为 Note 一起使用,它是一个带有 ID 参数的字符串。 这是我目前所拥有的,但不幸的是它没有通过任何 JUnit 测试。
package edu.drew.note;
public class RadixSort implements SortInterface
public static void Radix(Note[] note)
// Largest place for a 32-bit int is the 1 billion's place
for(int place=1; place <= 1000000000; place *= 10)
// Use counting sort at each digit's place
note = countingSort(note, place);
//return note;
private static Note[] countingSort(Note[] note, long place) //Where the sorting actually happens
Note[] output = new Note[note.length]; //Creating a new note that would be our output.
int[] count = new int[10]; //Creating a counter
for(int i=0; i < note.length; i++) //For loop that calculates
int digit = getDigit(note[i].getID(), place);
count[digit] += 1;
for(int i=1; i < count.length; i++)
count[i] += count[i-1];
for(int i = note.length-1; i >= 0; i--)
int digit = getDigit((note[i].getID()), place);
output[count[digit]-1] = note[i];
count[digit]--;
return output;
private static int getDigit(long value, long digitPlace) //Takes value of Note[i] and i. Returns digit.
return (int) ((value/digitPlace ) % 10);
public Note[] sort(Note[] s) //
Radix(s);
return s;
//Main Method
public static void main(String[] args)
// make an array of notes
Note q = new Note(" ", " ");
Note n = new Note("CSCI 230 Project Plan",
"Each person will number their top 5 choices.\n" +
"By next week, Dr. Hill will assign which piece\n" +
"everyone will work on.\n");
n.tag("CSCI 230");
n.tag("final project");
Note[] Note = q,n;
//print out not id's
System.out.println(Note + " Worked");
//call radix
Radix(Note);
System.out.println(Note);
//print out note_id's
【问题讨论】:
【参考方案1】:代替
public Note[] sort(Note[] s) //
Radix(s);
return s;
我应该用过
public Note[] sort(Note[] s) //
s = Radix(s);
return s;
并将Radix
的变量类型从void
更改为Note[].
【讨论】:
以上是关于使用节点而不是整数在Java中实现基数排序的主要内容,如果未能解决你的问题,请参考以下文章