[英]Insertion Sort with a 2D pointer array by certain column in C
我目前正在努力为 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,值 4 表示要根据哪一列进行排序):
insertionSort(student,rows,4);
它需要根据每行最后一列的值对每一行进行排序: 示例:按第 3 列排序
输入
10 11 71
71 25 65
98 42 16
output:
98 42 16
71 25 65
10 11 71\
任何帮助将不胜感激,因为这是本周到期的:)
假设arr
是一个已分配的指针块,其中每个指针保存一个已分配的double
块的地址,您只需按每个double
块中的最后一列进行比较,然后在arr
中交换指针即可进行排序。 您的 insertSort insertionSort()
function 可以简化为:
void insertionSort (double **arr, int rows, int cols)
{
for (int i = 0; i < rows; i++) /* insertion sort of a by last col */
for (int j = i; j > 0 && arr[j][cols-1] < arr[j-1][cols-1]; j--) {
double *tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
(注: c
已更改为cols
)
这不适用于二维数组。 对于 2D 数组,您必须使用循环或memcpy()
保存和复制行而不是指针。 这一切都假定您的double **arr
是正确的,并且您已经为double
分配了指针和分配块,现在排序为arr
每行中的最后一个double
。 一个例子是:
#include <stdio.h>
#include <stdlib.h>
void insertionSort (double **arr, int rows, int cols)
{
for (int i = 0; i < rows; i++) /* insertion sort of a by last col */
for (int j = i; j > 0 && arr[j][cols-1] < arr[j-1][cols-1]; j--) {
double *tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
void prnrowscols (size_t rows, size_t cols, double **arr)
{
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++)
printf (j ? " %4g" : "%4g", arr[i][j]);
putchar ('\n');
}
}
int main (void) {
double tmp[][3] = { {10, 11, 71},
{71, 25, 65},
{98, 42, 16} },
**arr = NULL;
size_t rows = sizeof tmp / sizeof *tmp,
cols = sizeof *tmp / sizeof **tmp;
if (!(arr = malloc (rows * sizeof *arr))) {
perror ("malloc-arr");
return 1;
}
for (size_t i = 0; i < rows; i++) {
if (!(arr[i] = malloc (cols * sizeof *arr[i]))) {
perror ("malloc-arr[i]");
return 1;
}
for (size_t j = 0; j < cols; j++)
arr[i][j] = tmp[i][j];
}
puts ("orignal array:");
prnrowscols (rows, cols, arr);
insertionSort (arr, rows, cols);
puts ("\nsorted array:");
prnrowscols (rows, cols, arr);
for (size_t i = 0; i < rows; i++) /* free memory */
free (arr[i]);
free (arr);
}
示例使用/输出
$ ./bin/inssort_ptr2ptr
orignal array:
10 11 71
71 25 65
98 42 16
sorted array:
98 42 16
71 25 65
10 11 71
如果您还有其他问题,请仔细查看并告诉我。
我得到了我的问题的答案,只需要在我的 while 循环中使用指针指向 c 列作为可比较的(而不是 arr[i][c] 它变成 *(temp+c):
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;
}
}
TX 帮助大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.