气泡排序对象
我需要使用气泡排序来按名称对杂货清单进行排序。
显然,我的代码没有按名称对列表进行排序。
顺便说一句,数据存储的库存来自文件输入。
这是我的代码。
public void sortInventoryByName() {
//TODO: use bubble sort and compareTo
int n = inventory.size();
GroceryItem temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (inventory.get(j).compareTo(inventory.get(j + 1)) > 0) {
temp = inventory.get(i);
inventory.set(i, inventory.get(i + 1));
inventory.set(i + 1, temp);
}
}
}
}
这是我的超级类(杂货店)的比较方法
@Override
public int compareTo(Object o) {
if(getClass() != o.getClass()) {
throw new IllegalArgumentException();
}
else {
GroceryItem other = (GroceryItem) o;
return (this.name.compareTo(other.name));
}
}
I need to sort my grocery inventory by name by using bubble sort.
Apparently, my code is not sorting the list by name.
BTW, the data stored inventory comes from a file input.
Here is my code.
public void sortInventoryByName() {
//TODO: use bubble sort and compareTo
int n = inventory.size();
GroceryItem temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (inventory.get(j).compareTo(inventory.get(j + 1)) > 0) {
temp = inventory.get(i);
inventory.set(i, inventory.get(i + 1));
inventory.set(i + 1, temp);
}
}
}
}
Here is my compareTo method from my superclass (GroceryItem)
@Override
public int compareTo(Object o) {
if(getClass() != o.getClass()) {
throw new IllegalArgumentException();
}
else {
GroceryItem other = (GroceryItem) o;
return (this.name.compareTo(other.name));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您有一些不匹配以比较正确的值。
对于LOOPS ,有两种用两种用两个实现气泡排序算法的方法。
下面使第一个循环增加了
屏障
变量,第二个是减少index
。因此,随着外循环的每一次迭代,最低的值将被移至第一名(例如,最小气泡将首先移动)。下一个迭代将跳过第一个元素。它将持续到列表完整列表将结束为止。
您的示例显示相反的行为 - &GT;对于外循环的每一次迭代,列表中的最高元素都移至末尾。
您要确切要迭代内部循环并不重要。最终的排序结果是我们的目标。
代码段:
您的
compareTo()
的实现应正常工作。因此,库存
列表应正确排序。根据您的代码,一些通知:
您无需在循环之外声明
temp
变量。它只是交换两个值的临时变量。内联声明和用法就足够了。建议为循环变量添加更有意义的名称,而不是
i
和j
。它增加了未来的代码可读性和理解else
块是多余的,在comparpoto()
Looks like you have some mismatch for comparing the right values.
There are two ways of implementing a bubble sort algorithm with two for loops.
Below made the first loop incremented
barrier
variable and second is decrementingindex
.Thus with every iteration of the outer loop, the lowest value will be moved to the first place (like the smallest bubble will be moved first). The next iteration will skip this first element. And it will last till the list full list will be over.
Your example shows opposite behaviour -> with every iteration for the outer loop the highest element in a list is moved to the end.
It isn't so important how exactly do you want to iterate the inner for loop. The final sorted result is our aim.
Code snippet:
Your implementation of
compareTo()
should work fine. So,inventory
list should be sorted correctly.A few notices according to your code:
you don't need to declare
temp
variable outside of loops. It is just a temporary variable for swapping two values. Inline declaration and usage will be enough.would suggest adding more meaningful names for loop variables instead of just
i
andj
. It increases code readability and understanding in the futureelse
block is redundant atcompareTo()
我填写了您的代码缺失部分。您应该阅读我如何问一个好问题,以及如何创建一个最小的,可重复的示例。
以下代码是
GroceryItem
类,该类仅包含一个成员,即name
,这是杂货项目的名称。由于您的问题仅涉及操纵该成员,因此我没有尝试猜测课程需要什么。代码后的说明。
上面的代码创建
list
GroceryItem
包含11个元素的对象。填充列表
后, bubble Sort 将在两个中执行循环的嵌套。最后,打印了排序<代码>列表
。请注意,类
GroceryItem
还实现方法toString()
,以便在打印groceryItem
的实例时使输出人类可读。如果将来,您需要使用
groceryItem
作为java.util.hashmap
的键,则groceryItem
将需要覆盖方法< code> hashcode(),如果类覆盖方法hashcode()
,则它也应覆盖方法equals()
。因此,这就是为什么上述代码包括那些覆盖方法的原因。请注意,这些方法都不是 -equals()
,hashcode()
和toString()
- - bubble Sort < /em>。运行上述代码时的oputput是:
I filled in the missing parts of your code. You should read How do I ask a good question and also the link to How to create a Minimal, Reproducible Example.
The below code is the
GroceryItem
class which only contains a single member, i.e.name
, which is the name of the grocery item. Since your question only deals with manipulating this member, I did not try to guess what other data the class needs.Explanations after the code.
The above code creates a
List
ofGroceryItem
objects that contains eleven elements. After populating theList
, the bubble sort is performed in the two, nestedfor
loops. Finally the sortedList
is printed.Note that class
GroceryItem
also implements methodtoString()
so as to make the output human-readable when printing an instance ofGroceryItem
.If, in future, you need to use
GroceryItem
as the key for ajava.util.HashMap
, thenGroceryItem
will need to override methodhashCode()
and if a class overrides methodhashCode()
then it should also override methodequals()
. Hence that is why the above code includes those overridden methods. Note that none of those methods –equals()
,hashCode()
andtoString()
– are required for the bubble sort.The oputput when running the above code is: