在冒泡排序中按升序或降序排序
在这个问题得到解答之后,我继续编写代码。 它以这种方式工作是完美的:
static String[][] bubbleSort(String customerdata[][], int sortafter, int asc)
{
String temp [];
boolean sort;
do{
sortiert = true;
for (int i = 0 ; i < customerdata.length - 1; i++){
if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0){
temp = customerdata[i];
customerdata[i] = customerdata[i+1];
customerdata[i+1] = temp;
sort = false;
}
}
}while(!sort);
return customerdata;
}
但正如你所看到的,我在这个函数中缺少 int asc 。我想要的是额外返回一个排序的降序或升序数组(取决于 asc == 1 (asc) 或 asc == 0 (desc))。
我不知道如何在其中实现它。我的意思是,目前我可以按升序或降序对其进行排序,但是在使用一些令人讨厌的长 for() 和 if() 循环调用此方法之后。
我希望将其紧凑地放在内部,并根据我是否给出 bubblesort(x,0,0) 或 (x,0,1) 列表应按降序或升序返回。
After this was answered I continued to work my way through the code.
It work's perfect this way:
static String[][] bubbleSort(String customerdata[][], int sortafter, int asc)
{
String temp [];
boolean sort;
do{
sortiert = true;
for (int i = 0 ; i < customerdata.length - 1; i++){
if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0){
temp = customerdata[i];
customerdata[i] = customerdata[i+1];
customerdata[i+1] = temp;
sort = false;
}
}
}while(!sort);
return customerdata;
}
But as you can see, I'm missing int asc inside this function. What I want is to additionaly return a sorted descending or ascending array (depending wether asc == 1 (asc), or asc == 0 (desc)).
I'm at loss how to implement it inside this. I mean currently I can sort it ascending or descending, but once AFTER this method was called with some nasty long for() and if() loops.
I'd like to have it compactly inside and depending wether I give bubblesort(x,0,0) or (x,0,1) the list should be returned descending or ascending.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的解决方案:您可以将
asc
变为1
或-1
之一吗?那么你只需要改变一行:
Simple solution: Can you make
asc
into one of1
or-1
?Then you'd only need to change one line:
升序排序意味着
i
处的元素小于i + 1
处的元素。降序排序意味着i
处的元素大于i +
处的元素。诀窍是翻转你决定元素是否不合适的逻辑。 则应将此行:更改为。
具体来说,如果您想翻转排序顺序,
Sorted ascending means the element at
i
is less than the element ati + 1
. Sorted descending means the element ati
is greater than the element ati +
. The trick is to flip the logic where you decide whether the elements are out of place. Specifically, this line:should be changed to
if you want to flip the order of the sorting.
您始终可以按升序排序,如果需要降序,则只需反转它即可。问题是在循环内重复“if”测试是否比数组的另一次遍历效率更低。
我假设数组的大小相对较小。冒泡排序是出了名的低效,除了小数组之外不应该使用。
You can always sort ascending and simply reverse it if descending is required. It's a question of whether or not repeating the "if" test inside the loop is less efficient that another traversal of the array.
I'm assuming that the size of the array is relatively small. Bubble sort is notoriously inefficient and shouldn't be used except for small arrays.
试试这个:
Asc 可以是 0 或 1(升序或降序...)
通过将其添加到索引中,您基本上交换了 if 语句,而不添加另一个 if ;^)
(请注意,我更改了 2 个位置:“ + asc”和“- asc”)
编辑:
不要忘记在第一行放置一个大断言,以确保 Asc 确实不能是 0 或 1 以外的任何值;^)
try this:
Asc can be 0 or 1 (ascending or descending...)
by adding it to your index, you basically swap the if statement, without adding another if ;^)
(note there are 2 positions which I changed: the "+ asc" and the "- asc")
EDIT:
Don't forget to put a big assert at the first line making sure Asc can really not be anything else than 0 or 1 ;^)
如果您想要“软件工程”类型的答案而不是我上面给出的快速破解答案,您可以传递一个函子(搜索 Comparator 类)来进行比较,以允许最终灵活的搜索。
And if you want the "software engineering" type answer instead of the quick hack answer I gave above, you can pass a functor (search for the Comparator class) to do the comparison, to allow for ultimately flexible searching.