与排序的qsort二维数组 [英] Sorting a 2D array with qsort

查看:290
本文介绍了与排序的qsort二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排序二维数组。首先,我按列,对其进行排序然后行。逐列是工作,但按行不能行。什么是错在这code?

  INT scmpr(常量无效*一,常量无效* B){
返回STRCMP((为const char *)A,(为const char *)B);
}诠释主要(无效){
  INT I,J;  焦炭**卡口;
  标签=(字符**)的malloc(sizeof的(字符*)* 10);  对于(I = 0; I&小于10;我++){
    标签由[i] =(字符*)malloc的(的sizeof(char)的* 15);
  }  对于(I = 0; I&小于10;我++){
    为(J = 0; J&下; 15; J ++){
      标签[I] [J] =兰特()%20 +'B';
      的printf(%C选项卡[I] [J]);
    }
    卖出期权();
  }
  对于(I = 0; I&小于10;我++){
    的qsort(安培;标签[I] [0],15的sizeof(炭),scmpr);
  }
  的qsort(选项卡,10的sizeof(炭),scmpr); //< - 不工作    对于(I = 0; I&小于10;我++){
      为(J = 0; J&下; 15; J ++){
        的printf(%C选项卡[I] [J]);
      }
    卖出期权();
    }
  卖出期权();
  返回0;
  }


解决方案

我想你指的是以下

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;#定义M 10
#定义的N个15INT CCMP(常量无效* LHS,常量无效* RHS)
{
    unsigned char型C1 = *(const的无符号字符*)LHS;
    unsigned char型C2 = *(const的无符号字符*)RHS;    如果(C< C2)返回-1;
    否则,如果(C< C1)返回1;
    否则返回0;
}INT SCMP(常量无效* LHS,常量无效* RHS)
{
    返回STRCMP(*(为const char **)LHS,*(为const char **)RHS);
}INT主要(无效)
{
    焦炭**卡口;
    标签=(字符**)的malloc(M *的sizeof(字符*));    用于(为size_t我= 0; I<米;我++)
    {
        标签由[i] =(字符*)malloc的(N * sizeof的(炭));
    }    函数srand((unsigned int类型)时间(NULL));    用于(为size_t我= 0; I<米;我++)
    {
        用于(为size_t J = 0; J< N - 1; J ++)
        {
            标签[I] [J] =兰特()%('Z' - 'A'+ 1)+'A';
        }
        标签[I] [N-1] ='\\ 0';
    }    用于(为size_t我= 0; I<米;我++)
    {
        的printf(%S \\ n,制表符[I]);
    }    的printf(\\ n);    用于(为size_t我= 0; I<米;我++)
    {
        的qsort(标签[I],N - 1的sizeof(炭),CCMP);
    }
    的qsort(标签,男,sizeof的(字符*),南华早报);    用于(为size_t我= 0; I<米;我++)
    {
        的printf(%S \\ n,制表符[I]);
    }    的printf(\\ n);    用于(为size_t我= 0; I<米;我++)免费(选项卡[I]);
    免费(标签);    返回0;
}

程序输出可能如下方法

  DJSKLJOHGHEANW
ZSDZJZXCKGYOVF
LHEOQYAEHOLPYR
PLORDTQOSNQFVP
TQUEYAVQYVUHKH
WIZOVPHYKXPEMF
JHUFARLARGQSEN
BOWYYXOTMVTYUI
DIOOPKVPDHPXPI
PTXQJVQHTGCHDYAAEFGHJLNQRRSU
ADEGHHJJKLNOSW
AEEHHLLOOPQRYY
AEHHKQQTUUVVYY
BIMOOTTUVWXYYY
CDFGJKOSVXYZZZ
CDGHHJPQQTTVXY
DDHIIKOOPPPPVX
DFLNOOPPQQRSTV
EFHIKMOPPVWXYZ

I'm trying to sort 2d array. First i sort it by column, then by rows. Column by column is working but row by row not. What's wrong in this code?

int scmpr (const void *a, const void *b){ 
return strcmp((const char*)a, (const char*)b);
}

int main(void){
  int i,j;

  char **tab;
  tab=(char**)malloc(sizeof(char*)* 10); 

  for(i=0; i<10; i++){
    tab[i]=(char*)malloc(sizeof(char)*15);
  }

  for(i=0; i<10; i++){
    for(j=0; j<15; j++){
      tab[i][j]=rand()%20+'b';
      printf("%c ", tab[i][j]);
    }
    puts("");
  }
  for (i = 0; i<10; i++){
    qsort(&tab[i][0], 15, sizeof(char), scmpr); 
  }
  qsort(tab, 10, sizeof(char), scmpr); //<-- doesn't work

    for(i=0; i<10; i++){
      for(j=0; j<15; j++){
        printf("%c ", tab[i][j]);
      }
    puts("");
    } 
  puts("");
  return 0;
  }

解决方案

I think that you mean the following

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define M   10
#define N   15

int ccmp( const void *lhs, const void *rhs )
{
    unsigned char c1 = *( const unsigned char *)lhs;
    unsigned char c2 = *( const unsigned char *)rhs; 

    if ( c1 < c2 ) return -1;
    else if ( c2 < c1 ) return 1;
    else return 0;
}

int scmp( const void *lhs, const void *rhs )
{
    return strcmp( *( const char ** )lhs, *( const char ** )rhs );
}

int main( void ) 
{
    char **tab;
    tab = ( char** )malloc( M * sizeof( char* ) ); 

    for ( size_t i = 0; i < M; i++ )
    {
        tab[i] = ( char* )malloc( N * sizeof( char ) );
    }

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N - 1; j++ )
        {
            tab[i][j] = rand() % ( 'Z' - 'A' + 1 ) + 'A';
        }
        tab[i][N-1] = '\0';
    }

    for ( size_t i = 0; i < M; i++ )
    {
        printf( "%s\n", tab[i] );
    }

    printf( "\n" );

    for ( size_t i = 0; i < M; i++ )
    {
        qsort( tab[i], N - 1, sizeof( char ), ccmp ); 
    }
    qsort( tab, M, sizeof( char * ), scmp );

    for ( size_t i = 0; i < M; i++ )
    {
        printf( "%s\n", tab[i] );
    }

    printf( "\n" );

    for ( size_t i = 0; i < M; i++ ) free( tab[i] );
    free( tab );

    return 0;
}

The program output might look the following way

DJSKLJOHGHEANW
ZSDZJZXCKGYOVF
LHEOQYAEHOLPYR
PLORDTQOSNQFVP
TQUEYAVQYVUHKH
WIZOVPHYKXPEMF
JHUFARLARGQSEN
BOWYYXOTMVTYUI
DIOOPKVPDHPXPI
PTXQJVQHTGCHDY

AAEFGHJLNQRRSU
ADEGHHJJKLNOSW
AEEHHLLOOPQRYY
AEHHKQQTUUVVYY
BIMOOTTUVWXYYY
CDFGJKOSVXYZZZ
CDGHHJPQQTTVXY
DDHIIKOOPPPPVX
DFLNOOPPQQRSTV
EFHIKMOPPVWXYZ

这篇关于与排序的qsort二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆