[英]String array Sorting C
我一直试图使该程序按列索引对字符串的二维数组进行排序。
我像这样初始化2d数组:
char *str[ROWS][COLS] = {{"Russia", "Boxing", "Mens", "Gold"},
{"America", "Cycling", "Mens", "Gold"},
{"New Zealand", "Swimming", "Womens", "Silver"},
{"India", "Badminton", "Mens", "Bronze"}};
如果我想按第一列(国家/地区名称)对这个数组进行排序,那么它将看起来像这样:
char *str[ROWS][COLS] = {{"America", "Cycling", "Mens", "Gold"},
{"India", "Badminton", "Mens", "Bronze"}};
{"New Zealand", "Swimming", "Womens", "Silver"},
{"Russia", "Boxing", "Mens", "Gold"}};
到目前为止,这是我所做的事情,除排序方法外,几乎是正确的。 我在执行该程序时遇到了麻烦。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 4
#define COLS 4
void print_array(char *str[][COLS]);
void sort_array(char *str[][COLS], int nrows, int col);
int
main(void) {
char *str[ROWS][COLS] = {{"Russia", "Boxing", "Mens", "Gold"},
{"America", "Cycling", "Mens", "Gold"},
{"New Zealand", "Swimming", "Womens", "Silver"},
{"India", "Badminton", "Mens", "Bronze"}};
int col;
/* array before sorting */
printf("Before: \n");
print_array(str);
/*choosing column index to sort by*/
printf("\nChoose which column index you wish to sort by: ");
if (scanf("%d", &col) != 1) {
printf("Invalid input\n");
exit(EXIT_FAILURE);
}
sort_array(str, ROWS, col);
/* array after sorting */
printf("\nAfter: \n");
print_array(str);
return 0;
}
void
print_array(char *str[][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%s ", str[i][j]);
}
printf("\n");
}
}
/*function used for sorting the array */
void
sort_array(char *str[][COLS], int nrows, int col) {
int i, j;
char *temp;
for (i = 0; i < nrows; i++) {
for (j = i; j < nrows; j++) {
if(strcmp(str[i][col], str[j][col]) > 0) {
temp = str[i][col];
str[i][col] = str[j][col];
str[j][col] = temp;
}
}
}
}
我遇到的问题是我的排序算法不是交换行,而是交换该列中的字符串。 我也尝试使用insertion sort
算法,但不确定如何使用2D字符串数组实现该算法。
任何帮助,将不胜感激 :)
使用qsort
,您需要一个比较器函数,将指向要排序的两个元素的指针作为参数。 在这种情况下,要排序的元素是2D数组中的整行。 如果需要更改排序列,则还需要使保存它的变量成为全局变量(否则,您需要为每个列创建不同的比较器)。
static int sort_column = 0;
static int compare_rows(const void *a, const void *b) {
return strcmp(((const char **)a)[sort_column], ((const char **)b)[sort_column]);
}
然后,您只需使用数组和比较器函数作为参数调用qsort
:
qsort(str, ROWS, sizeof(*str), (compare_rows));
遍历行。 比较该列中的两行, i
和i + 1
。 根据需要遍历各列并交换两行。 重复直到没有交换。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 4
#define COLS 4
void print_array(char *str[][COLS]);
void sort_array(char *str[][COLS], int nrows, int ncols, int col);
int
main(void) {
char *str[ROWS][COLS] = {{"Russia", "Boxing", "Mens", "Gold"},
{"America", "Cycling", "Mens", "Gold"},
{"New Zealand", "Swimming", "Womens", "Silver"},
{"India", "Badminton", "Mens", "Bronze"}};
int col;
int result = 0;
int clean = 0;
/* array before sorting */
printf("Before: \n");
print_array(str);
/*choosing column index to sort by*/
do {
printf("\nChoose which column index you wish to sort by 0 to %d: ", COLS - 1);
if ( ( result = scanf("%d", &col)) != 1) {
printf("Invalid input\n");
if ( result == EOF) {
fprintf ( stderr, "problem getting input\n");
exit(EXIT_FAILURE);
}
//clean input stream
while ( ( clean = getchar ()) != '\n' && clean != EOF) {}
}
} while ( result != 1 || col < 0 || col >= COLS);
sort_array(str, ROWS, COLS, col);
/* array after sorting */
printf("\nAfter: \n");
print_array(str);
return 0;
}
void
print_array(char *str[][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%s ", str[i][j]);
}
printf("\n");
}
}
/*function used for sorting the array */
void
sort_array(char *str[][COLS], int nrows, int ncols, int col) {
int i = 0, j = 0, swap = 0;
char *temp;
do {
swap = 0;
for ( i = 0; i < nrows - 1; i++) {//iterate through rows
if ( strcmp( str[i][col], str[i + 1][col]) > 0) {//compare col for row i and i+1
for ( j = 0; j < ncols; j++) {//iterate through cols and swap rows
temp = str[i][j];
str[i][j] = str[i + 1][j];
str[i + 1][j] = temp;
}
swap = 1;
}
}
} while ( swap);//loop until no swaps
}
从C11开始,您可以执行以下操作:
int compare_col(const void *a, const void *b, void *ctx) {
int col = *(int*)ctx;
return strcmp(((char**)a)[col], ((char**)b)[col]);
}
/*function used for sorting the array */
void sort_array(char *str[][COLS], int nrows, int col) {
qsort_s(str, nrows, sizeof(char*)*COLS, compare_col, &col);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.