S
S
Salavat Nuriyev2014-12-30 11:18:16
C++ / C#
Salavat Nuriyev, 2014-12-30 11:18:16

How to solve the problem of sorting by counting an array of strings?

Please help me understand the task and its implementation.
Write a program that sorts an array of strings alphabetically using counting sort. Use pointers to strings.
There is some code found on the Internet, but it doesn’t really work, it doesn’t give errors, but a windows window appears, requiring you to debug or close the program.
PS I am writing in C, i.e. string cannot be used?

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

int main()
{
    char a;
    char a2;
    int i, j, k;
    char *mas, *mas2;
    int *key;
    *mas=a;
    *mas2=a2;
    *key=k;
    int size=10;
    i=0;

    while (*(mas+i)!='\n')
    {
        gets(*(mas+i));
        *(key+i)=0;
        i++;
    }
    for (i=size-1;i>0;i--)
        for (j=i-1;j>=0;j--)
            if (strcmp(*(mas+i),*(mas+j))<0) key[j]++;
            else key[i]++;
    for (j=0;j<size;j++)
        mas2[key[j]]=mas[j];
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2014-12-30
@santos995

Rows, in general, do not make sense to sort by simple counting . This sorting is only effective for integers in a limited range.
You most likely want a quadratic counting sort algorithm .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question