首页 > 试题广场 >

字符串排序

[编程题]字符串排序
  • 热度指数:569793 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的由大小写字母混合构成的 n 个单词,输出按字典序从小到大排序后的结果。

【名词解释】
\hspace{15pt}从字符串的第一个字符开始逐个比较,直至发现第一个不同的位置,比较这个位置字符的 Ascii 码,Ascii 码较小(\texttt{`A'} \lt \texttt{`B'} \cdots \lt \texttt{`Z'} \lt \texttt{`a'} \lt \cdots \lt \texttt{`z'})的字符串字典序也较小。

输入描述:
\hspace{15pt}第一行输入一个整数 n \left( 1 \leqq n \leqq 10^3 \right) 代表给定的单词个数。
\hspace{15pt}此后 n 行,每行输入一个长度 1 \leqq {\rm length}(s) \leqq 100,由大小写字母构成的字符串 s,代表一个单词。


输出描述:
\hspace{15pt}一共 n 行,每行输出一个字符串,代表排序后的结果。第一行输出字典序最小的单词。
示例1

输入

11
cap
to
cat
card
two
too
up
boat
boot
AA
Aa

输出

AA
Aa
boat
boot
cap
card
cat
to
too
two
up
#include <stdio.h>
#include<string.h>
void sort(char strings[][200],int n){

    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-1-i;j++){
            if(strcmp(strings[j],strings[j+1])>0){
                char tmp[200];
                strcpy(tmp,strings[j]);
                strcpy(strings[j],strings[j+1]);
                strcpy(strings[j+1],tmp);
            }
        }
    }
    
    return;
}
int main() {
    int n;
    scanf("%d",&n);
    getchar();//清空输入输出缓冲区
    char arr[n][200];
    for(int i=0;i<n;i++){
        fgets(arr[i],200,stdin);
    }

    sort(arr,n);
    for(int i=0;i<n;i++){
        printf("%s",arr[i]);
    }
    return 0;
}

发表于 2025-05-04 21:05:27 回复(0)
用库的快排最后一个用例会超内存,故而只能简单手搓冒泡
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main() {
    uint32_t size = 0;

    scanf("%d",&size);
    char str[1001][101];
    char tmp[101];

    for(int i = 0; i < size; i++)
    {
        scanf("%s",str[i]);
    }
    // qsort(str, size, sizeof(str[0]), compar);
    for(int i = 0; i < size - 1; i++)
    {
        for(int j = 1; j < size - i; j++)
        {
            if(strcmp(str[j-1], str[j]) > 0)
            {
                strcpy(tmp, str[j-1]);
                strcpy(str[j-1], str[j]);
                strcpy(str[j], tmp);
            }
        }
    }

    for(int i = 0; i < size; i++)
    {
        printf("%s\n",str[i]);
    }

    return 0;
}


发表于 2025-03-09 13:16:03 回复(0)
#include <stdio.h>
#include <string.h>

int main()
{
    int nums,i,j;

    scanf("%d",&nums);
    char str[nums][101];
    char tempstr[101];

    for(i=0;i<nums;i++)
    {
        scanf("%s",str[i]);
    }

    for(i=0;i<nums-1;i++)
    {
        for(j=1;j<nums-i;j++)
        {
            if(strcmp(str[j], str[j-1])<0)
            {
                memset(tempstr,0,sizeof(tempstr));
                strcpy(tempstr,str[j]);
                memset(str[j],0,sizeof(str[j]));
                strcpy(str[j],str[j-1]);
                memset(str[j-1],0,sizeof(str[j-1]));
                strcpy(str[j-1],tempstr);
            }
        }
    }

    for(i=0;i<nums;i++)
    {
        printf("%s\r\n",str[i]);
    }
}
发表于 2024-12-05 22:59:59 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    int n;
    scanf("%d",&n);

    char str[n][101];
    for(int i=0; i<n;i++){
        scanf("%s",str[i]);
    }

    char temp[101];
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if (strcmp(str[i], str[j])>0){
                strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j], temp);
            }
        }
    }

    for(int i=0; i<n;i++){
        printf("%s\n",str[i]);
    }
   
    return 0;
}
发表于 2024-10-09 16:16:15 回复(0)
使用字符串指针数组和qsort函数求解

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 比较函数,用于传递给 qsort
int compare_strings(const void* a, const void* b) {
    const char* str1 = *(const char**)a;
    const char* str2 = *(const char**)b;
    return strcmp(str1, str2);
}

int main() {
    int n;
    scanf("%d", &n);

    char* strings[1000] = { 0 }; // 创建字符串指针数组

    // 输入 n 个字符串
    for (int i = 0; i < n; ++i) {
        strings[i] = (char*)malloc(101 * sizeof(char)); // 分配字符串存储空间
        scanf("%s", strings[i]);
    }

    // 使用 qsort 函数对字符串数组进行排序
    qsort(strings, n, sizeof(char*), compare_strings);

    // 输出排序后的结果
    for (int i = 0; i < n; ++i) {
        printf("%s\n", strings[i]);
        free(strings[i]); // 释放每个字符串的内存
    }

    return 0;
}

发表于 2024-06-26 17:26:49 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
	int n;
	scanf("%d", &n);
	char str[100][1000];
	for (int j = 0; j < n; j++) {
		scanf("%s", str[j]);
	}
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			if (strcmp(str[j], str[j + 1]) == 1) {
				char temp[1000];
				strcpy(temp, str[j + 1]);
				strcpy(str[j + 1], str[j]);
				strcpy(str[j], temp);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		printf("%s\n", str[i]);
	}
}
本地IDEV C运行成功,提交到在线测试里面就不行了,有没有大佬看一下
编辑于 2024-04-25 13:24:33 回复(0)
#include "string.h"
#include "stdio.h"
//注意:题里提到字符串长度小于等于100,但我的二维数组设定后一维度为100时则发生段错误,设为1000则没有这个问题,不知道咋回事。
int main()
{
   int n,i,a = 1;
   scanf("%d",&n);
   char str[1000][1000] = {},temp[1000] = {};
   for(i = 0; i < n; i++)
   {
    scanf("%s",str[i]);
   }
    for(i = 0; i < n; i++)
    {
        for(a = 1;a < n-i;a++)
        {
            if(strcmp(str[i],str[i+a]) > 0 )
            {
                strcpy(temp, str[i]);
                strcpy(str[i], str[i+a]);
                strcpy(str[i+a], temp);
            }
        }
    }
    for(i = 0;i<n;i++)
    {
        printf("%s\n",str[i]);
    }
}

发表于 2024-03-29 23:01:43 回复(0)
其实很简单  被我想复杂了

编辑于 2024-01-08 10:31:56 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main() {
    int n, i = 0;
    scanf("%d", &n);
    char word[1000][101];
    while (scanf("%s", word[i++]) != EOF) {}

    qsort(word, n, sizeof(word[0]), compar);

    for (int i = 0; i < n; i++) {
        printf("%s\n", word[i]);
    }
    return 0;
}

编辑于 2023-12-24 14:48:42 回复(0)
#  include <stdio.h>
# include<string.h>
# include<stdlib.h>
//决定用字符串数组来做 定义字符串结构体,定义结构体数组;通过比较排序;
int main() {
    int n,i = 0,j=0;
    char *p[1000],*temp;
    scanf("%d",&n);
    for (i=0; i<n; i++) {p[i] = (char *)malloc(101);scanf("%s",p[i]);}
    for(i = 0; i<n-1; i++){
        for(int k = i+1; k<n; k++){
            if(strcmp(p[i], p[k]) >0 ){temp = p[i];p[i] = p[k];p[k] = temp;}
        }
        printf("%s\n",p[i]);//puts(p[i]);
    }
    printf("%s\n",p[i]);
    return 0;
}

编辑于 2023-12-02 13:10:09 回复(0)
思路:
1.字符串存储,可使用字符指针数组+malloc存储
2.字符串排序,字典序,也就是因此字典排单词得规则,先按首字母大小排,相同则比较下一个字符,一次类推。也就是用strcmp比较大小即可。
3.多行字符串输入,考虑for+scanf接收
关键点:
    比较字符串时,进行大小替换过程中,因为这里使用指针数组+malloc形式,那么指针赋值,即改变指针指向即可。当然中间引入临时数组空间来中转,也是可以的,只是浪费空间。
注意思考:指针数组在排序后的指向情况。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
    int i,j,n;
    char *tmp;
    scanf("%d", &n);
    char *str[n];
    //接收字符串
    for(i = 0; i < n; i++)
    {
        str[i] = (char*)malloc(101);
        memset(str[i], 0, 101);
        scanf("%s", str[i]);
    }

    //排序
    for(i = 0; i < n; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if (strcmp(str[i], str[j]) > 0)
            {
                tmp = str[i];
                str[i] = str[j];
                str[j] = tmp;
            }
        }        
    }
    //输出
    for(i = 0; i < n; i++)
    {
        printf("%s\n", str[i]);
        free(str[i]);
    }
    return 0;
}


发表于 2023-11-25 18:00:15 回复(0)
#include <stdio.h>
#include <string.h>
typedef struct {
    char buf[101];
} str;

int main() {
    str str[1001];
    char temp[101];
    int num, i, j, k, max = 0, len;
    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        scanf("%s", str[i].buf);
        len = strlen(str[i].buf);
        if (len > max) {
            max = len;
        }
    }

    for (i = 0; i < num - 1; i++) {
        for (j = 0; j < num - 1; j++) {
            {
                k = 0;
                if (str[j].buf[k] != str[j + 1].buf[k]) {
                    if (str[j].buf[k] > str[j + 1].buf[k]) {
                        sprintf(temp, "%s", str[j + 1].buf);
                        sprintf(str[j + 1].buf, "%s", str[j].buf);
                        sprintf(str[j].buf, "%s", temp);
                    }
                } else {
                    while (k < strlen(str[i].buf)) {
                        k++;
                        if (str[j].buf[k] > str[j + 1].buf[k]) {
                            sprintf(temp, "%s", str[j + 1].buf);
                            sprintf(str[j + 1].buf, "%s", str[j].buf);
                            sprintf(str[j].buf, "%s", temp);
                            break;
                        } else {
                            if(str[j].buf[k] < str[j + 1].buf[k]){
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    for (i = 0; i < num; i++) {
        printf("%s\n", str[i].buf);
    }
    return 0;
}

发表于 2023-09-13 20:53:27 回复(0)
#include <stdio.h>
#include <string.h>
//思路:二维数组 & 选择排序法
//将每个字符串看作一个具有大小的数,存入二位数组,采用冒泡/选择排序法实现a~z的排序
int main() {

    int n, i = 0, j;
    char dir[1001][101] = { 0 }, mid[101] = { 0 };
    scanf("%d", &n);
    while (~scanf("%s", dir[i])) {
        i++;
    }
    for (i = 0; i <= n - 1; i++) {
        for (j = i + 1; j <= n - 1; j++) {
            if (strcmp(dir[i], dir[j]) <= 0) {//"比大小"的行为使用strcmp函数实现,应当注意条件(注意该函数的返回值)
                strcpy(mid, dir[i]);
                strcpy(dir[i], dir[j]);
                strcpy(dir[j], mid);
            }
        }
    }
    while (n) {
        printf("%s\n", dir[--n]);
    }

    return 0;
}

发表于 2023-03-31 15:59:43 回复(1)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct DATA
{
    char S[200];
}data;
void swap(data *Arr,int lEN)
{
   for(int i = 0;i<lEN-1;i++)
   {
    for(int j = 0;j<lEN-i-1;j++)
    {
        if(strcmp(Arr[j].S,Arr[j+1].S)>0)
        {
            data temp = Arr[j+1];
            Arr[j+1] =Arr[j];
            Arr[j]  = temp;
        }
    }
   }
   return;
}
int main() {
    int len=0;
    scanf("%d",&len);
    data * Alldata=(data*)malloc(sizeof(data)*len);
    memset(Alldata, 0, sizeof(data)*len);
    for(int i = 0;i<len;i++)
    {
        char *str=(char*)malloc(sizeof(100));
        memset(str, 0, sizeof(100));
        scanf("%s",str);
        strcpy(Alldata[i].S,str);
    }
    swap(Alldata,len);
   
    for(int i = 0;i<len;i++)
    {
    printf("%s\n",Alldata[i].S);
    }
    return 0;
}
发表于 2023-02-13 14:20:17 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char arr[1000][101]={};
    int flag[1000]={};
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s",arr[i]);
        flag[i]=1;
    }
    char maxArr[101] ={'z'+1};
    int minLoc=-1;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(strcmp(arr[j],maxArr)<0&&flag[j]){
                strcpy(maxArr, arr[j]);
                minLoc=j;
            }
        }

        printf("%s\n",arr[minLoc]);
        flag[minLoc]=0;
        minLoc=-1;
        strcpy(maxArr, "{");
    }
    return 0;
}


每次循环找到最小的值进行输出即可,几道相似的排序题分别使用不同的方式进行处理,还可以按照qsort进行处理,这样或许更快
发表于 2023-01-01 18:14:31 回复(0)
int mycompare(void * a, void *b){
    
    return (strcmp((char *)a,(char *)b)); //strcmp是字符串比较函数
}

int main(void){
    int i,n = 0;
   
    scanf("%d",&n); //获取数组的一维坐标大小范围
    char input[n][101];  //对数组的一维坐标不要直接设定成1000,否则会报内存不足
    for(i=0;i<n;i++){
        scanf("%s",&input[i]);        
    }
    qsort(input,n,sizeof(input[0]),mycompare); //直接调用stdlib.h的比较函数
    
    for(i=0;i<n;i++){
        printf("%s\n",input[i]);        
    }   
    
    return 0;
    
}

发表于 2022-08-24 21:36:21 回复(0)
strcpy太费时间了,此方法把交换字符串优化为交换指针:
#include <stdio.h>
#include <string.h>
int main()
{
    int n;
    char  a[1000][101];
    char* b[1000];
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%s",&(a[i]));
        b[i]=&(a[i]);
    }
    for(int j=0;j<n-1;j++)
    {
        for(int k=j+1;k<n;k++)
        {
            char* p;
            if(strcmp(b[j],b[k])>0)
            {
                p=b[j];
                b[j]=b[k];
                b[k]=p;
            }
        

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

发表于 2022-07-18 22:49:39 回复(0)