在Java中使用BUBBLESORT对2D字符串数组进行排序 [英] Sorting 2D String Array with BUBBLESORT in java

查看:149
本文介绍了在Java中使用BUBBLESORT对2D字符串数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问过类似的问题,但从来没有问过关于2D字符串数组的问题,因此,尝试了很长时间后,我找不到想要的东西。我正在尝试使用BubbleSort在Java中对2D字符串数组进行排序。

Similar questions have been asked but never about 2D String Arrays, therefore after trying for a long time I couldn't find what I wanted. I'm trying to sort a 2D String Array in java using BubbleSort.

作为输入,我收到一个二维的String数组(一个表),并且应该对 column的索引进行排序。我应该按照指定列中的值对行进行排序。

As input, I receive a two-dimensional array (a table) of Strings and the index of the "column" you should sort. I should sort the rows by the values in the indicated column.

您可以看到第一个索引为行索引,第二个索引为列索引。例如,以下Java数组和表彼此对应:

You can see the first index as a row index, and the second index as a column index. For example, the following Java array and table correspond with each other:

String[][] table = {
  {"a", "b"},
  {"c", "d"}
};

-

0   1
  +---+---+
0 | a | b |
  +---+---+
1 | c | d |
  +---+---+

在此示例中,请继续表[ 0] [1]将产生值 b,因为它是第0行和第1列中的项目。

To continue on this example, table[0][1] will yield the value "b", since it’s the item in row 0 and column 1.

重要提示:我不允许使用Java库中的任何排序算法,例如Arrays.sort。

这是我到目前为止尝试过的:

This is what I've tried so far:

class Solution {
        public static void stableSort(String[][] table, int column) {
            int i;
            int j;
            String temp = null;
            for (i = 0; i < table.length - 1; i++) {
                for (j = 0; j < table.length - 1 - i; j++) {
                    if (table[i][j].compareTo(table[i][j + 1]) > 0) {
                        temp = table[i][j];
                        table[i][j] = table[i][j + 1];
                        table[i][j + 1] = temp;
                    }
                }
            }
        }
    }

我从边界错误中得到一个索引,并且它也无法正常工作,因为测试期望表[0] [0]
中的结果不同。

I get an Index out of Bounds error and also it is not working as the test expects a different result in table[0][0] Thanks for your help.

推荐答案

public static String[][] stableSort(String[][] table, int column) {
    int i=0,j=0;
    String[] temp = null;
    boolean swap=true;
    while(swap)
    for (i = 0; i < table.length - 1; i++) {
        swap=false;
        if(table[i][column].compareTo(table[i+1][column]) > 0){
            temp = table[i];
            table[i] = table[i+1];
            table[i+1]=temp;
            swap=true;
        }
    }
    return table;
}

它将继续应用冒泡排序,直到不再执行任何交换为止。这时,while(swap)条件不再满足,该方法返回。
在main中试过并且可以工作(如果我理解您的意思):

It keeps applying the bubblesort until no more swaps are performed. At that point,the condition while(swap) is no longer satisfied and the method returns. Tried in a main and it works (if I understand what you meant):

public static void main(String[] args) {
    String[][] table = {
            {"z", "b", "v"},
            {"s", "w", "a"},
            {"r", "c", "h"}
    };

    table = stableSort(table,1);
    for(int i = 0; i < table.length; i++){
        for(int j = 0; j < table[0].length; j++){
            System.out.printf("%5s ", table[i][j]);
        }
        System.out.println();
    }
}

此输出:

z     b     v 
r     c     h 
s     w     a 

这篇关于在Java中使用BUBBLESORT对2D字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆