冒泡排序中在C字母顺序的字符数组 [英] bubble sort a character array in alphabetic order in c

查看:139
本文介绍了冒泡排序中在C字母顺序的字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想冒泡排序按字母顺序的字符数组。
我的code是如下:

I'm trying to bubble sort a character array in alphabetic order. My code is as follows:

#define CLASS_SIZE 10
#include <stdio.h>

void bubbleSortAWriteToB(const char a[], char *b[]);

int main(void){
    char *s_letters[CLASS_SIZE];
    char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'};
    bubbleSortAWriteToB(letters,s_letters);
        return 0;
}

void bubbleSortAWriteToB(const char a[], char *b[]){
    char temp;
    int i,j;
    for(i=0;i<CLASS_SIZE-1;i++){
        for(j=1;j<CLASS_SIZE;j++){
            if((int)a[j-1]>(int)a[j]){
                temp = a[j];
                *b[j] = a[j-1];
                *b[j-1] = temp;

            }

    }

  }
}

它不给任何类型的错误,但是当我运行它,它卡住喜欢它有点在inifinte循环。但是从我可以看到它是不是无论是。你能帮帮我吗?

It doesn't give any kind of error but when i run it it gets stuck like it's kinda in a inifinte loop. But from what i can see it isn't that either. Can you help me out?

推荐答案

首先,有您的code一些pretty严重的根本问题。我们解决这些虽然之前,让我们刚修好你所拥有的这么远。您的排序循环似乎一半排序一个数组和排序一半的B阵列。你也从来不初始化的B数组包含任何值。这里是你的code修正版本:

Fixing your code

First of all, there are some pretty serious fundamental issues with your code. Before we tackle those though, let's just fix what you have so far. Your sorting loop seemed to be half sorting the a array and half sorting the b array. you also never initialized the b array to contain any values. Here is a corrected version of your code:

#define CLASS_SIZE 10
#include <stdio.h>

void bubbleSortAWriteToB(const char a[], char * b[]);

int main(void){
    int i;

    // initialize array
    char * s_letters[CLASS_SIZE];
    char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'};
    // sort array
    bubbleSortAWriteToB(letters,s_letters);

    // print sorted array
    for (i=0;i<CLASS_SIZE;i++){
        printf("%c\n", *s_letters[i]);
    }

    return 0;
}

void bubbleSortAWriteToB(const char a[], char * b[]){
    char * temp;
    int i,j;

    // initialize b array to hold pointers to each element in a
    for (i=0;i<CLASS_SIZE;i++){
        b[i] = (char *)(a) + i;
    }

    // in-place sort the b array
    for(i=0;i<CLASS_SIZE;i++){
        for(j=i+1;j<CLASS_SIZE-1;j++){
            if(*b[j-1]>*b[j]){
                temp = b[j];
                b[j] = b[j-1];
                b[j-1] = temp;
            }
        }   
    }
}

定盘初始化与指向一个将b数组,然后由一个阵列中比较所述相应值中就地将b数组进行排序

The fix was to initialize the b array with points to a, and then sort the b array in-place by comparing the corresponding values in the a array.

在原来的code,该战略是有指针(B),这将指向一个元素,然后得到排序的数组。这是虽然不必要在这里,因为字符比指针更小,所以让b为字符的阵列空间效率更高和更简单

In your original code, the strategy was to have an array of pointers (b) that would point to the elements in a, and then get sorted. This was unnecessary here though, because characters are smaller than pointers, so letting b be an array of characters is more space-efficient and simpler.

另外,你的间隔很是压扁在一起,有点难以阅读。下面是采用B为字符,而不是一个指针数组,并提供改进的间距的解决方案。另外,声明上述功能是不必要的。它足以定义函数,一次申报。

Also, your spacing was very squished-together and somewhat difficult to read. Here's a solution that uses b as an array of characters instead of pointers, and offers improved spacing. Also, declaring the function above was not necessary. It suffices to define the function and declare it once.

#define CLASS_SIZE 10
#include <stdio.h>

void bubbleSortAWriteToB(const char a[], char b[]){
    char temp;
    int i,j;

    // initialize b array to hold pointers to each element in a
    for (i = 0; i < CLASS_SIZE; i++){
        b[i] = a[i];
    }

    // in-place sort the b array
    for(i = 0; i < CLASS_SIZE; i++){
        for(j = i + 1; j < CLASS_SIZE - 1; j++){
            if(b[j-1] > b[j]){
                temp = b[j];
                b[j] = b[j-1];
                b[j-1] = temp;
            }
        }   
    }
}

int main(void){
    int i;

    // initialize array
    char s_letters[CLASS_SIZE];
    char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'};

    // sort array
    bubbleSortAWriteToB(letters, s_letters);

    // print sorted array
    int i;
    for (i = 0; i < CLASS_SIZE; i++){
        printf("%c\n", s_letters[i]);
    }

    return 0;
}

这篇关于冒泡排序中在C字母顺序的字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆