我一直试图使这个程序按列索引对字符串的二维数组排序。
我初始化这个二维数组如下:
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
算法,但我不确定如何用二维字符串数组实现它。如有任何帮助,将不胜感激:)
最佳答案
遍历行。比较两行的列,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
}
关于c - 字符串数组排序C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39315932/