Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.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
Arrays 按C中的某列使用2D指针数组进行插入排序_Arrays_C_Pointers - Fatal编程技术网

Arrays 按C中的某列使用2D指针数组进行插入排序

Arrays 按C中的某列使用2D指针数组进行插入排序,arrays,c,pointers,Arrays,C,Pointers,我目前正在努力为2D指针数组实现插入排序算法。 请不要使用Qsort解决方案,它必须是老式的 这是我的密码 void insertionSort(double **arr, int rows,int c) { double *temp; int i, j; for (i = 1; i < rows; i++) { temp = *(arr+i); j = i - 1; /* Move elements of arr[

我目前正在努力为2D指针数组实现插入排序算法。
请不要使用Qsort解决方案,它必须是老式的 这是我的密码

void insertionSort(double **arr, int rows,int c)
{
    double *temp;
    int i, j;
    for (i = 1; i < rows; i++) {
        temp = *(arr+i);
        j = i - 1;

        /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        while (j >= 0 && arr[j][c] > arr[i][c]) {
            int r = j + 1;
            *(arr+r) = *(arr+j);
            j = j - 1;
        }
        int t= j + 1;
        *(arr+t) = temp;
    }
}
它需要根据每行最后一列中的值对每行进行排序:示例:按第3列排序

输入
101171
712565
98 42 16

输出:
984216
712565
101171\


任何帮助都将不胜感激,因为这将在本周到期:)

假定
arr
是一个分配的指针块,其中每个指针持有一个分配的
double
块的地址,您只需按
double
块中的最后一列进行比较,并交换
arr
中的指针即可进行排序。您的
insertionSort()
函数可以简化为:

void insertionSort(双**arr,int行,int列)
{
对于(int i=0;i0&&arr[j][cols-1]
注意:
c
已更改为
cols

这不适用于二维阵列。对于2D数组,必须使用循环或
memcpy()
保存和复制行,而不是指针。这一切都假定您的
double**arr
是正确的,并且您已经为
double
分配了指针和块,现在正在排序
arr
的每一行中的最后一个
double
。例如:

#包括
#包括
void insertionSort(双**arr、整数行、整数列)
{
对于(int i=0;i0&&arr[j][cols-1]
示例使用/输出

$。/bin/inssort\ptr2ptr
原始数组:
10   11   71
71   25   65
98   42   16
排序数组:
98   42   16
71   25   65
10   11   71

仔细检查一下,如果您还有其他问题,请告诉我。

我得到了我的问题的答案,只是必须使用while循环中的指针指向c列作为可比项(而不是arr[I][c],它变成了*(temp+c):

void insertionSort(双**arr,int行,int c)
{
双*温度;
int i,j;
对于(i=1;i=0&&arr[j][c]>*(temp+c)){
int r=j+1;
*(arr+r)=*(arr+j);
j=j-1;
}
int t=j+1;
*(arr+t)=温度;
}
}

tx for help everyone

你的代码不起作用吗?你的问题是什么?如果我没记错的话,
qsort
早在20世纪70年代就被添加到了C编程语言中。这使得它在我的书中非常老套。你需要显示你传递的任何东西的声明,作为参数
arr
double**
有绝对值这与数组无关。它是一个指向已分配指针块的单指针,每个指针块依次指向已分配的
double
。如果这不是您所拥有的,那么它就不起作用了。您的指针算法将失败。(您可能需要为返回选择type
int
,这样您就可以向调用者指示成功或失败——由您决定)
insertionSort(student,rows,4);
void insertionSort(double **arr, int rows,int c)
{
    double *temp;
    int i, j;
    for (i = 1; i < rows; i++) {
        temp = *(arr+i);
        j = i - 1;

        /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        
        while (j >= 0 && arr[j][c] > *(temp+c) ) {
            int r = j + 1;
            *(arr+r) = *(arr+j);
            j = j - 1;
        }
        int t= j + 1;
        *(arr+t) = temp;
    }
}