Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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
Javascript 如何在C中基于二维数组中的列进行排序?_Javascript_C_Arrays_Sorting - Fatal编程技术网

Javascript 如何在C中基于二维数组中的列进行排序?

Javascript 如何在C中基于二维数组中的列进行排序?,javascript,c,arrays,sorting,Javascript,C,Arrays,Sorting,我刚刚开始学习C语言,我正在尝试用C语言对二维数组中的列进行排序 问题出在这里 编写一个程序来输入一个m x n的数组。按递增顺序对奇数列进行排序,按递减顺序对偶数列进行排序 我的想法是使用递归函数。在数组的每一行,我将把元素放入一个新数组中,对其排序,然后重新分配给旧数组。循环,直到我们到达列的末尾 以下是我的C代码: #include <stdio.h> int j = 0, temp[] = {}, sizeTemp = 0; // The size of temp, c

我刚刚开始学习C语言,我正在尝试用C语言对二维数组中的列进行排序

问题出在这里

编写一个程序来输入一个m x n的数组。按递增顺序对奇数列进行排序,按递减顺序对偶数列进行排序

我的想法是使用递归函数。在数组的每一行,我将把元素放入一个新数组中,对其排序,然后重新分配给旧数组。循环,直到我们到达列的末尾

以下是我的C代码:


#include <stdio.h>

int j = 0, temp[] = {}, sizeTemp = 0; // The size of temp, change overtime in loop()
int arr[4][3] = {
    {1,2,3},
    {4,8,6},
    {2,1,5},
    {3,7,4}
};

void pushToArray(int arr[], int currentEl, int addEl){  // Push an element into an array
    arr[currentEl] = addEl;
}

void bubbleSort(int arr[], int size, int inc_dec){  
// Using bubble sort to sort the temporary array, which is then reassigned to the old array
    int temp;

    for(int i = 0; i < size; i++){
        for(int j = 0; j < size - 1;j++){
            if(inc_dec == 1){
                // Increasing
                if(arr[j] > arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }else{
                // Decreasing
                if(arr[j] < arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}
int justForReturnSomething(){ 
// void function can't return a value, so I use this just for returning something
    return 1;
}
void loop(){
    int temp2;
    for(int i = 0; i < 4; i++){
        if(j > 2){
            return justForReturnSomething();
        }
        temp2 = arr[i][j];
        pushToArray(temp,sizeTemp,temp2);
        sizeTemp++;
        if(j % 2 != 0){
            bubbleSort(temp,sizeTemp,1);
        }else{
            bubbleSort(temp, sizeTemp,-1);
        }

    }

    for(int k = 0; k < 4; k++){
        arr[k][j] = temp[k];
    }

    if(j > 2){
        return justForReturnSomething();
    }else{
        j++;
        for(int m = 0; m < sizeTemp; m++){ // Reassign the temp to empty
            temp[m] = 0;
        }
        sizeTemp = 0; // reassign the size of temp to 0
        loop();    
    }

}

int main(){

    loop();
    printf("Your sorted array: \n");
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 3; j++){
            printf("%d,",arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

为了演示解决问题的方法,我用Javasript进行了尝试,结果很好:

let arr = [
    [1,2,3],
    [4,8,6],
    [2,1,5],
    [3,7,4]
]; 
let temp = [], j=0;
function bubbleSort( arr,  size,  inc_dec){
    let temp;

    for(let i = 0; i < size; i++){
        for(let j = 0; j < size - 1;j++){
            if(inc_dec == 1){
                // Increasing
                if(arr[j] > arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }else{
                // Decreasing
                if(arr[j] < arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}

function loop(){
    for(let i = 0; i < 4; i++){
        if(j > 2){
            return;
        }
        temp.push(arr[i][j]);
        if(j % 2 !== 0){
            bubbleSort(temp,temp.length,1);
        }else{
            bubbleSort(temp, temp.length,-1);
        }

    }

    for(let k = 0; k < 4; k++){
        arr[k][j] = temp[k];
    }

    if(j > 2){
        return;
    }else{
        j++;       
        temp = [];
        loop();    
    }

}
loop();
console.log(arr);

让arr=[
[1,2,3],
[4,8,6],
[2,1,5],
[3,7,4]
]; 
设温度=[],j=0;
功能气泡排序(arr,大小,包括12个){
让温度;
for(设i=0;iarr[j+1]){
温度=arr[j];
arr[j]=arr[j+1];
arr[j+1]=温度;
}
}否则{
//减少
if(arr[j]2){
返回;
}
温度推送(arr[i][j]);
如果(j%2!==0){
气泡分离器(温度,温度长度,1);
}否则{
气泡运动(温度,温度长度,-1);
}
}
for(设k=0;k<4;k++){
arr[k][j]=温度[k];
}
如果(j>2){
返回;
}否则{
j++;
温度=[];
loop();
}
}
loop();
控制台日志(arr);


因为我已经尝试用我自己的想法来解决这个问题,并且没有在google上搜索就找到了一个解决方案,如果你能指导我用这种方法为C带来解决方案,我将非常高兴。但是,我们非常感谢其他方法。

这不是一个完整的答案,因为看起来您希望自己解决这个问题

这里的诀窍是,当C以行主顺序存储数组时,按列排序。您可以通过定义跨步(对于包含M列和N行的数组,这将是[0]的大小,即单行的大小)并以该跨步的倍数进行所有索引来实现。或者,您可以转置数组,按行排序,并将结果转置回原处

为了提高性能,我建议进行适当的排序,但是编写进行往返换位的代码可能会更简单。复制整个阵列两次成本很高,但这并不令人难以置信:它与阵列的大小成正比

如果您编写的函数是按升序和降序排序的,并且您有一个
int[M][N]
,您可以根据需要调用
sort\u升序(int[i],N)
sort\u降序(int[i],N)
。您可以查看标准库中的
qsort()
。如果要尝试就地排序,则需要传递行数、列数和要排序的列数。列数将是您关心的元素之间的跨距


有几种不同的方法可以控制行的上移或下移:总是先按降序排序,然后再按2递增,保留一个标记来记住最后一行是按升序排序还是按降序排序,或者选中
1%2
,这将优化为非常便宜的按位or指令。

这是@Brian给出的答案。非常感谢你,布莱恩

#include <stdio.h>

void bubbleSort2D(int numRows, int numCols, int arr[][numCols], int col, int incDec) {
  // Using bubble sort to sort a column in the matrix
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numRows - 1; j++) {
      int a = arr[j][col];
      int b = arr[j + 1][col];
      if ((incDec && a > b) || (!incDec && a < b)) {
        arr[j][col] = b;
        arr[j + 1][col] = a;
      }
    }
  }
}

int main() {

  int arr[4][3] = {
    {1,2,3},
    {4,8,6},
    {2,1,5},
    {3,7,4}
  };

  for (int i = 0; i < 3; i++) {
    // sort "odd" columns in descending order, "even" columns in ascending order
    bubbleSort2D(4, 3, arr, i, i % 2 == 0);
  }

  printf("Your sorted array: \n");
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) {
      printf("%d,", arr[i][j]);
    }
    printf("\n");
  }

  return 0;
}
#包括
void bubbleSort2D(整数行、整数行、整数行[][numCols]、整数列、整数行){
//使用气泡排序对矩阵中的列进行排序
对于(int i=0;ib)| |(!incDec&a
这里是repl链接:


Javascript具有按引用传递的功能,而C具有按值传递的功能。考虑如何编写可测试代码是一件好事,也就是说,我如何分解代码,使每个函数都能完成其测试用例所需的最低要求?这有助于简化代码。例如,我们可以使用sortColumn函数重构上面的示例,该函数只有一个职责(给定矩阵、列索引和排序方向,在矩阵中对该列的值进行排序)。这个函数可能更容易实现,并且添加逻辑到基于列索引的排序之后就很简单了。例如,我们可以用javascript编写一个简单的表达式来实现sortColumn:
函数sortColumn(matrix,columnIndex,desc){let sortFn=desc?(a,b)=>b-a:(a,b)=>a-b;matrix.map(row=>row[columnIndex]).sort(sortFn).forEach((columnValue,rowIndex)=>matrix[rowIndex][columnIndex]=columnValue);return matrix;}
感谢您的评论。我试试看。但据我所知,我们无法返回C中的数组。这里有一个C中的快速演示:感谢Davidslor的帮助。我去看看that@Darwin应该多说一点让问题变得不那么简单的事情:你是在对列排序,而不是对行排序。
#include <stdio.h>

void bubbleSort2D(int numRows, int numCols, int arr[][numCols], int col, int incDec) {
  // Using bubble sort to sort a column in the matrix
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numRows - 1; j++) {
      int a = arr[j][col];
      int b = arr[j + 1][col];
      if ((incDec && a > b) || (!incDec && a < b)) {
        arr[j][col] = b;
        arr[j + 1][col] = a;
      }
    }
  }
}

int main() {

  int arr[4][3] = {
    {1,2,3},
    {4,8,6},
    {2,1,5},
    {3,7,4}
  };

  for (int i = 0; i < 3; i++) {
    // sort "odd" columns in descending order, "even" columns in ascending order
    bubbleSort2D(4, 3, arr, i, i % 2 == 0);
  }

  printf("Your sorted array: \n");
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) {
      printf("%d,", arr[i][j]);
    }
    printf("\n");
  }

  return 0;
}