在冒泡排序中按升序或降序排序

发布于 2024-08-13 09:47:11 字数 1003 浏览 12 评论 0原文

这个问题得到解答之后,我继续编写代码。 它以这种方式工作是完美的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

琴流音 2024-08-20 09:47:11

简单的解决方案:您可以将 asc 变为 1-1 之一吗?

那么你只需要改变一行:

if(asc * customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0)

Simple solution: Can you make asc into one of 1 or -1 ?

Then you'd only need to change one line:

if(asc * customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0)
孤独难免 2024-08-20 09:47:11

升序排序意味着 i 处的元素小于 i + 1 处的元素。降序排序意味着 i 处的元素大于 i + 处的元素。诀窍是翻转你决定元素是否不合适的逻辑。 则应将此行:

if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0){

更改为。

if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) > 0){

具体来说,如果您想翻转排序顺序,

Sorted ascending means the element at i is less than the element at i + 1. Sorted descending means the element at i is greater than the element at i +. The trick is to flip the logic where you decide whether the elements are out of place. Specifically, this line:

if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0){

should be changed to

if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) > 0){

if you want to flip the order of the sorting.

想你的星星会说话 2024-08-20 09:47:11

您始终可以按升序排序,如果需要降序,则只需反转它即可。问题是在循环内重复“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.

甜味拾荒者 2024-08-20 09:47:11

试试这个:

 for (int i = 0  ; i < customerdata.length - 1; i++){
      if(customerdata[i+asc][sortafter].compareTo(customerdata[i+1-asc][sortafter]) < 0){
           temp = customerdata[i];
           customerdata[i] = customerdata[i+1];
           customerdata[i+1] = temp;

           sort = false;
      }
 }

Asc 可以是 0 或 1(升序或降序...)

通过将其添加到索引中,您基本上交换了 if 语句,而不添加另一个 if ;^)

(请注意,我更改了 2 个位置:“ + asc”和“- asc”)

编辑:
不要忘记在第一行放置一个大断言,以确保 Asc 确实不能是 0 或 1 以外的任何值;^)

try this:

 for (int i = 0  ; i < customerdata.length - 1; i++){
      if(customerdata[i+asc][sortafter].compareTo(customerdata[i+1-asc][sortafter]) < 0){
           temp = customerdata[i];
           customerdata[i] = customerdata[i+1];
           customerdata[i+1] = temp;

           sort = false;
      }
 }

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 ;^)

陌上青苔 2024-08-20 09:47:11

如果您想要“软件工程”类型的答案而不是我上面给出的快速破解答案,您可以传递一个函子(搜索 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文