C 对一系列小写字母进行排序

C 对一系列小写字母进行排序,c,arrays,string,quicksort,C,Arrays,String,Quicksort,该算法可以很好地处理整数,但自从我将它们转换为字符后,它一直在为输出打印null: /* sort a series of lower case letters using quicksort algorithm. */ #include <stdio.h> #define N 10 // since c gets the ascii code when returning an int for a char variable. char quicksort(char a[]

该算法可以很好地处理整数,但自从我将它们转换为字符后,它一直在为输出打印null:

/* sort a series of lower case letters using quicksort algorithm. */ 
#include <stdio.h>

#define N 10

// since c gets the ascii code when returning an int for a char variable.

char quicksort(char a[], char low, char high);
char split(char a[], char low, char high);
int a[N];

int main(void)
{
    int i;

    printf("Enter  letters to be sorted: ");
    for (i = 0; i < N; i++)
        scanf("%d", &a[i]);
    quicksort(a, 0, N - 1);

    printf("In sorted order: ");
    for (i = 0; i < N; i++)
        printf("%s ", a[i]);
    printf("\n");

    return 0;
}
char quicksort(char a[], char low, char high)
{
    int middle;

    if (low >= high) return;
    middle = split(a, low, high);
    quicksort(a, low, middle - 1);
    quicksort(a, middle + 1, high);
}
char split(char a[], char low, char high)
{
    char part_element = a[low];

    for (;;) {
        while (low < high && part_element <= a[high])
            high--;
        if (low >= high) break;
        a[low++] = a[high];

        while (low < high && a[low] <= part_element)
            low++;
        if (low >= high) break;
        a[high--] = a[low];
    }

    a[high] = part_element;
    return high;
}
/*使用快速排序算法对一系列小写字母进行排序。*/
#包括
#定义n10
//因为c在返回字符变量的int时获取ascii码。
字符快速排序(字符a[],字符低,字符高);
字符分割(字符a[],字符低,字符高);
int a[N];
内部主(空)
{
int i;
printf(“输入要排序的字母:”);
对于(i=0;i=高)返回;
中间=分割(a、低、高);
快速排序(a、低、中-1);
快速排序(a、中+1、高);
}
字符分割(字符a[],字符低,字符高)
{
char part_元素=a[低];
对于(;;){
当(低<高和部分元素=高)中断时;
a[低++]=a[高];
当(低<高&&a[低]=高)中断时;
a[高--]=a[低];
}
a[高]=部分单元;
高回报;
}
需要
%c
格式说明符。您的
scanf
应更改为

scanf("%c", &a[i]);
因此,您应该将
a
数组重新声明为
char
,而不是
int
,三个问题:

  • a
    声明为
    int
    数组,但所有函数都处理
    char
    数组。这意味着它们不能正确地遍历数组。将其更改为
    字符a[N]
  • 要读取字符,请使用
    %c
    格式说明符来
    scanf
  • 要打印字符,请使用
    %c
    格式说明符
    printf
  • 修改后的代码仍然不起作用,我似乎找不到bug

    您的问题似乎在于
    main()
    与其他人建议使用面向
    char
    的数据而不是
    int
    的思路非常相似。有一些非致命的有问题的选择问题,比如使用
    char
    数据类型作为数组索引
    quicksort()
    声明为返回
    char
    但不返回任何内容;等等下面是您的代码的重做,主要是为了风格,包括各种建议:

    /* sort a series of letters using quicksort algorithm. */ 
    
    #include <stdio.h>
    
    #define N (10)
    
    void quicksort(char a[], int low, int high);
    int split(char a[], int low, int high);
    
    int main(void)
    {
        char a[N];
    
        printf("Enter letters to be sorted: ");
    
        for (int i = 0; i < N; i++) {
            scanf("%c", &a[i]);
        }
    
        quicksort(a, 0, N - 1);
    
        printf("In sorted order: ");
    
        for (int i = 0; i < N; i++) {
            printf("%c ", a[i]);
        }
        printf("\n");
    
        return 0;
    }
    
    void quicksort(char a[], int low, int high)
    {
        if (low < high) {
            int middle = split(a, low, high);
            quicksort(a, low, middle - 1);
            quicksort(a, middle + 1, high);
        }
    }
    
    int split(char a[], int low, int high)
    {
        char part_element = a[low];
    
        for (;;) {
            while (low < high && part_element <= a[high]) {
                high--;
            }
            if (low >= high) {
                break;
            }
            a[low++] = a[high];
    
            while (low < high && a[low] <= part_element) {
                low++;
            }
            if (low >= high) {
                break;
            }
            a[high--] = a[low];
        }
    
        a[high] = part_element;
    
        return high;
    }
    

    scanf
    中,使用“%c”读取字符。除非您只想使用快速排序算法,否则最好使用
    stdlib.h
    中的
    qsort
    。您只需编写一个比较函数,然后调用
    qsort
    传递数组、大小、元素大小和比较函数。5行代码。为什么不调试它呢?看起来split函数有问题,因为它没有显示输出中的所有字母。@arihanian——提供一个输入/输出失败的示例。我在我的答案中添加了一个用法示例——您是否在输入中添加空格或类似的内容?这对我来说很糟糕。程序运行得很好。这真的很有帮助,因为我正在快速运行c的基础知识,并且我学习了快速排序算法
    > ./a.out
    Enter letters to be sorted: aadircslne
    In sorted order: a a c d e i l n r s 
    >