admin管理员组文章数量:1399499
I am trying to sort the tuples in the 2D array based on a single column value similar to Javascript's sort function.
arr.sort((a,b) => a[0] - b[0])
C Code :
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int comp(const void *a, const void *b){
return (((int*)a)[0] - ((int *)b)[0]);
}
int main(){
int arr[][4] = {
{3,2,5,3}, {1,0,5,2},{0,2,2,4},{0,4,4,5}
};
int ROWS = sizeof(arr)/sizeof(arr[0]);
int COLS = sizeof(arr[0])/sizeof(arr[0][0]);
//qsort(arr, ROWS, COLS*sizeof(int), comp); //this works and sorts arr correctly
int **rect = malloc(ROWS*sizeof(int*));
for(int i = 0; i < COLS; i++){
*(rect+i) = malloc(COLS*sizeof(int));
}
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
*(*(rect+i)+j) = arr[i][j];
}
}
qsort(rect, ROWS, COLS*sizeof(int), comp); //this doesn't work on the rect 2d array.
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
printf("%d ", *(*(rect+i)+j));
}
printf("\n");
}
}
Quicksort works with the comparator function on the 2D array arr
.
When I try using it with the **rect
array, the sorting doesn't work. I end up getting a segmentation fault. Can somebody point out where I am going wrong and how **rect
is being treated differently from arr
?
I am trying to sort the tuples in the 2D array based on a single column value similar to Javascript's sort function.
arr.sort((a,b) => a[0] - b[0])
C Code :
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int comp(const void *a, const void *b){
return (((int*)a)[0] - ((int *)b)[0]);
}
int main(){
int arr[][4] = {
{3,2,5,3}, {1,0,5,2},{0,2,2,4},{0,4,4,5}
};
int ROWS = sizeof(arr)/sizeof(arr[0]);
int COLS = sizeof(arr[0])/sizeof(arr[0][0]);
//qsort(arr, ROWS, COLS*sizeof(int), comp); //this works and sorts arr correctly
int **rect = malloc(ROWS*sizeof(int*));
for(int i = 0; i < COLS; i++){
*(rect+i) = malloc(COLS*sizeof(int));
}
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
*(*(rect+i)+j) = arr[i][j];
}
}
qsort(rect, ROWS, COLS*sizeof(int), comp); //this doesn't work on the rect 2d array.
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
printf("%d ", *(*(rect+i)+j));
}
printf("\n");
}
}
Quicksort works with the comparator function on the 2D array arr
.
When I try using it with the **rect
array, the sorting doesn't work. I end up getting a segmentation fault. Can somebody point out where I am going wrong and how **rect
is being treated differently from arr
?
2 Answers
Reset to default 2The objects being sorted are of type int *
and are being sorted in ascending order of the first int
element they point to. See the definition of the function comp2
, and the call to qsort
in the following, modified code:
#include <stdio.h>
#include <stdlib.h>
int comp2(const void *a, const void *b){
int *aa = *(int * const *)a;
int *bb = *(int * const *)b;
// N.B. this expression is immune from integer overflow, unlike aa[0] - bb[0]
return (aa[0] > bb[0]) - (aa[0] < bb[0]);
}
int main(){
static const int arr[][4] = {
{3,2,5,3}, {1,0,5,2},{0,2,2,4},{0,4,4,5}
};
int ROWS = sizeof(arr)/sizeof(arr[0]);
int COLS = sizeof(arr[0])/sizeof(arr[0][0]);
int **rect = malloc(ROWS*sizeof(int*));
// N.B. OP's original code incorrectly used i < COLS here...
for(int i = 0; i < ROWS; i++){
rect[i] = malloc(COLS*sizeof(int));
}
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
rect[i][j] = arr[i][j];
}
}
qsort(rect, ROWS, sizeof(rect[0]), comp2);
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
printf("%d ", rect[i][j]);
}
printf("\n");
}
}
Use pointers to array
int main()
{
int arr[][4] = {
{3,2,5,3},
{1,0,5,2},
{0,2,2,4},
{0,4,4,5}};
size_t ROWS = sizeof(arr)/sizeof(arr[0]);
size_t COLS = sizeof(arr[0])/sizeof(arr[0][0]);
/* pointer to array */ // <<----
int (*rect)[COLS] = malloc(ROWS * sizeof(*rect));
for(size_t i = 0; i < ROWS; i++)
{
for(size_t j = 0; j < COLS; j++)
{
rect[i][j] = arr[i][j];
}
}
qsort(rect, ROWS, sizeof(*rect), comp);
for(size_t i = 0; i < ROWS; i++)
{
for(size_t j = 0; j < COLS; j++)
{
printf("%d ", rect[i][j]);
}
printf("\n");
}
free(rect);
}
版权声明:本文标题:How do I use quicksort in C with a multi-dimensional array? I am getting an incorrect result - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744195664a2594729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rect
is not a multidimensional array but a pointer to a pointer. Pointers are very different from arrays.(((int*)a)[0]
accesses a pointer and interpretes it as aint
. – Gerhardh Commented Mar 25 at 13:04*(rect+i) =
do yourself any everyone else a favor and use array notation:rect[i] =
– Gerhardh Commented Mar 25 at 13:07qsort(rect, ROWS, COLS*sizeof(int), comp);
fails as the size is wrong. Should beqsort(rect, ROWS, sizeof rect[0], comp);
. But then that is not sorting an array ofint
. If you want a 2D array, allocate a 2D array and not an array of pointers to arrays. What is the real goal? A 2d array or an array of pointers to arrays? – chux Commented Mar 25 at 13:13