Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用鸡尾酒排序按一列对二维数组进行排序_Java_Arrays_Sorting - Fatal编程技术网

Java 如何使用鸡尾酒排序按一列对二维数组进行排序

Java 如何使用鸡尾酒排序按一列对二维数组进行排序,java,arrays,sorting,Java,Arrays,Sorting,我一直在寻找我能找到的任何地方,但我一辈子都想不出这一点 我的老师特别希望我尝试寻找/应用一种更有效的排序(比冒泡排序更有效)。我在网上查了一下,发现了一种双向气泡排序法,也称为摇瓶排序法或鸡尾酒排序法。我寻找了一种将这种排序应用于二维数组的方法,将其按一列排序,但将变量绑定到另一列中的相应值,例如int[][]iCandidate=new int[CandidateID][CandidateScore] 按CandidateScore对其进行排序并将其链接到CandidateID。我想出了这个

我一直在寻找我能找到的任何地方,但我一辈子都想不出这一点

我的老师特别希望我尝试寻找/应用一种更有效的排序(比冒泡排序更有效)。我在网上查了一下,发现了一种双向气泡排序法,也称为摇瓶排序法或鸡尾酒排序法。我寻找了一种将这种排序应用于二维数组的方法,将其按一列排序,但将变量绑定到另一列中的相应值,例如
int[][]iCandidate=new int[CandidateID][CandidateScore]

按CandidateScore对其进行排序并将其链接到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;
        }
    }

}
包装实践;
公共类排序{
公共静态无效排序(int[][]数组){
布尔交换;
int j=1;
做{
交换=假;
对于(int i=0;i数组[i+1][j]){
//测试这两个元素的顺序是否错误
int temp=array[i][1];//1,因为CandidateScore是第二个
数组[i][1]=数组[i+1][1];
数组[i+1][1]=温度;
temp=array[i][0];//交换候选ID
数组[i][0]=数组[i+1][1];
数组[i+1][1]=温度;
交换=真;
}
}
如果(!已交换){
//如果没有发生交换,我们可以退出外部循环。
打破
}
交换=假;
对于(int i=0;i数组[i+1][j]){
//测试这两个元素的顺序是否错误
int temp=array[i][1];//1,因为CandidateScore是第二个
数组[i][1]=数组[i+1][1];
数组[i+1][1]=温度;
temp=array[i][0];//交换候选ID
数组[i][0]=数组[i+1][1];
数组[i+1][1]=温度;
交换=真;
}
}
//如果未交换任何元素,则对列表进行排序
}while(交换);
}
公共静态void main(字符串[]args){
布尔计数=真;
int[][]数组=新的int[5][2];
int col1=0;
数组[0][0]=6;
数组[1][0]=26;
数组[2][0]=29;
数组[3][0]=25;
数组[4][0]=11;
数组[0][1]=1;
数组[1][1]=2;
数组[2][1]=3;
数组[3][1]=4;
数组[4][1]=5;
Sort.Sort(数组);
while(count=true){
System.out.println(数组[col1][0]);
col1=col1+1;
if(col1==4){
计数=假;
}否则{
计数=真;
}
}
}

其思想是,当您要对数组进行排序时,必须记住,您不能交换两个值,而是交换两列/行。因此,在本例中,您刚刚对CandidateScore进行了排序,但是CandidateId现在是错误的,因为您在之后对其进行了排序。修复这个问题非常简单,因为您有两列数组


要解决此问题,只需删除的(j=0;jp请您描述一下为什么您认为此代码是错误的?请给出一些示例输入的输出。请正确缩进您的代码。哇哦,您说得对。1分钟后,让我编辑您发布的代码以打印输出;我实际上不打算运行它来查看输出是什么。请将实际输出包含在q中继续。请正确缩进您的代码。我已经按照您的建议编辑了代码,但仍然不起作用,它只是按照与输入相同的顺序打印出数组。我将在第二次交换的代码中编辑问题,最大长度为(A)-2到0。您可以在此处看到实际代码:。我刚才添加的是候选ID之间的交换。其余代码与您第一次使用的代码一样。好的,我尝试实现上述代码,但仍然无法工作。当我尝试在排序后打印时,原始数组中没有任何更改。当您将其设置为打印数组[0][0]+“| |”+数组[0][1],(等,第一列更改)输出为:6 | 1 26 | 2 29 | 3 25 | 4 11||5@HaseebMuhammadTahir问题是,在你的解释中,你说CandidateId是第二个collmn。当然它不会改变任何东西,因为程序正在检查CandidateId的排序,而不是CandidateScore。只需更改输入:1 | 6,2 | 26,3 | 29,4 | 25 | 5 | 11。或simply在代码中更改,1与0。
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;
        }
      }
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);
}