你如何用鸡尾酒排序来对一个2维数组进行排序?

时间:2022-02-24 22:05:24

I have been looking everywhere I could, but I cannot for the life of me figure this out.

我到处寻找,但我不能让我的生活弄明白这一点。

My teacher wants me specifically to try and find/apply a more efficient sort (more efficient than a bubble sort). I looked online and I found a bidirectional bubble sort, also known as a shaker sort or a cocktail sort. I looked for a way to apply this sort to a 2 dimensional array, sorting it by one column but binding the variable to the corresponding value in the other column e.g. int[][] iCandidate = new int[CandidateID][CandidateScore]

我的老师特别想让我试着找到一种更有效的方法(比冒泡排序更有效)。我上网查了一下,发现了一个双向气泡排序,也叫shaker sort或鸡尾酒类。我找了一种方法将其应用到二维数组中,将其排序为一列,但将变量绑定到另一列中的相应值,例如int[][] []

Sorting it by the CandidateScore and linking it to the CandidateID. I came up with this code but I don't know what is going wrong:

将其分类,并将其链接到CandidateID。我想出了这个代码,但我不知道哪里出了问题:

package practice;
public class Sort {
public static void sort( int[][] array ){
    boolean swapped;
    int j = 1;
    do {
        swapped = false;
        for (int i =0; i<=  array.length  - 2;i++) {
            if (array[i][j] > array[i+1][j]) {
                //test whether the two elements are in the wrong order
                int temp = array[i][1];  //1 since CandidateScore is the second one
                array[i][1] = array[i+1][1];
                array[i+1][1]=temp;

                temp = array[i][0]; //interchange the CandidateIds
                array[i][0] = array[i+1][1];
                array[i+1][1]=temp;
                swapped = true;
            }
        }
        if (!swapped) {
            //we can exit the outer loop here if no swaps occurred.
            break;
        }
        swapped = false;
        for (int i =0; i<=  array.length  - 2;i++) {
            if (array[i][j] > array[i+1][j]) {
                //test whether the two elements are in the wrong order
                int temp = array[i][1];  //1 since CandidateScore is the second one
                array[i][1] = array[i+1][1];
                array[i+1][1]=temp;

                temp = array[i][0]; //interchange the CandidateIds
                array[i][0] = array[i+1][1];
                array[i+1][1]=temp;
                swapped = true;
            }
        }
        //if no elements have been swapped, then the list is sorted
    } while (swapped);
}

public static void main(String[] args) {

    boolean count = true;
    int[][] array = new int[5][2]; 
    int col1 = 0;
    array[0][0] = 6;
    array[1][0] = 26;
    array[2][0] = 29;
    array[3][0] = 25;
    array[4][0] = 11;
    array[0][1] = 1;
    array[1][1] = 2;
    array[2][1] = 3;
    array[3][1] = 4;
    array[4][1] = 5;
    Sort.sort(array);
    while(count = true){
        System.out.println(array[col1][0]);
        col1 = col1+1;
        if(col1 == 4){
            count = false;
        } else {
            count = true;
        }
    }

}

1 个解决方案

#1


0  

The idea is that when you want to sort an array, so you have to keep in mind that you don't interchange two values, but two columns/rows. So, in this case, you just sorted the CandidateScore, but the CandidateId is now wrong, because you sort it afterwards. It's very simple to fix this, since you have 2 columns array.

这个想法是,当你想要对一个数组进行排序时,你必须记住,你不会交换两个值,而是两个列/行。所以,在这种情况下,您只需要对CandidateScore进行排序,但是CandidateId现在是错误的,因为您在之后对它进行排序。这很简单,因为有两个列数组。

To fix this, just remove the for (j=0;j<=2;j++) and add this

为了解决这个问题,只需删除for (j=0;j<=2;j++)并添加这个。

for (int i =0; i<=  array.length  - 2;i++) {
      if (array[i][j] > array[i+1][j]) {
          //test whether the two elements are in the wrong order
          int temp = array[i][1];  //1 since CandidateScore is the second one
          array[i][1] = array[i+1][1];
          array[i+1][1]=temp;

          temp = array[i][0]; //interchange the CandidateIds
          array[i][0] = array[i+1][0];
          array[i+1][0]=temp;
          swapped = true;
        }
      }

Modify in both places, and i guess it'll work.

在这两个地方都要修改,我想会有用的。

EDIT: The code is as following:

编辑:代码如下:

public static void sort( int[][] array ){
     boolean swapped;
     do {
          swapped = false;
          for (int i =0; i<=  array.length  - 2;i++) {
                if (array[i][1] > array[i+1][1]) {
                     //test whether the two elements are in the wrong order
                     int temp = array[i][1];  //1 since CandidateScore is the second one
                     array[i][1] = array[i+1][1];
                     array[i+1][1]=temp;

                     temp = array[i][0]; //interchange the CandidateIds
                     array[i][0] = array[i+1][0];
                     array[i+1][0]=temp;
                     swapped = true;
                }
           }
           if (!swapped) {
              //we can exit the outer loop here if no swaps occurred.
              break;
           }
           swapped = false;
           for (int i =array.length - 2;  i>=0; i--) {
                 if (array[i][1] > array[i+1][1]) {
                      int temp = array[i][1]; 
                      array[i][1] = array[i+1][1];
                      array[i+1][1]=temp;

                      temp = array[i][0]; 
                      array[i][0] = array[i+1][0];
                      array[i+1][0]=temp;
                      swapped = true;
                }
            }
           //if no elements have been swapped, then the list is sorted
     } while (swapped);
}

#1


0  

The idea is that when you want to sort an array, so you have to keep in mind that you don't interchange two values, but two columns/rows. So, in this case, you just sorted the CandidateScore, but the CandidateId is now wrong, because you sort it afterwards. It's very simple to fix this, since you have 2 columns array.

这个想法是,当你想要对一个数组进行排序时,你必须记住,你不会交换两个值,而是两个列/行。所以,在这种情况下,您只需要对CandidateScore进行排序,但是CandidateId现在是错误的,因为您在之后对它进行排序。这很简单,因为有两个列数组。

To fix this, just remove the for (j=0;j<=2;j++) and add this

为了解决这个问题,只需删除for (j=0;j<=2;j++)并添加这个。

for (int i =0; i<=  array.length  - 2;i++) {
      if (array[i][j] > array[i+1][j]) {
          //test whether the two elements are in the wrong order
          int temp = array[i][1];  //1 since CandidateScore is the second one
          array[i][1] = array[i+1][1];
          array[i+1][1]=temp;

          temp = array[i][0]; //interchange the CandidateIds
          array[i][0] = array[i+1][0];
          array[i+1][0]=temp;
          swapped = true;
        }
      }

Modify in both places, and i guess it'll work.

在这两个地方都要修改,我想会有用的。

EDIT: The code is as following:

编辑:代码如下:

public static void sort( int[][] array ){
     boolean swapped;
     do {
          swapped = false;
          for (int i =0; i<=  array.length  - 2;i++) {
                if (array[i][1] > array[i+1][1]) {
                     //test whether the two elements are in the wrong order
                     int temp = array[i][1];  //1 since CandidateScore is the second one
                     array[i][1] = array[i+1][1];
                     array[i+1][1]=temp;

                     temp = array[i][0]; //interchange the CandidateIds
                     array[i][0] = array[i+1][0];
                     array[i+1][0]=temp;
                     swapped = true;
                }
           }
           if (!swapped) {
              //we can exit the outer loop here if no swaps occurred.
              break;
           }
           swapped = false;
           for (int i =array.length - 2;  i>=0; i--) {
                 if (array[i][1] > array[i+1][1]) {
                      int temp = array[i][1]; 
                      array[i][1] = array[i+1][1];
                      array[i+1][1]=temp;

                      temp = array[i][0]; 
                      array[i][0] = array[i+1][0];
                      array[i+1][0]=temp;
                      swapped = true;
                }
            }
           //if no elements have been swapped, then the list is sorted
     } while (swapped);
}