0

我正在尝试按字母顺序对字符数组进行冒泡排序。我的代码如下:

#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;

            }

    }

  }
}

它没有给出任何类型的错误,但是当我运行它时它会卡住,就像它有点在无限循环中一样。但据我所见,也不是这样。你能帮我吗?

4

5 回答 5

2

修复你的代码

首先,您的代码存在一些非常严重的基本问题。不过,在我们解决这些问题之前,让我们先解决您目前所拥有的问题。您的排序循环似乎对 a 数组进行了一半排序,对 b 数组进行了一半排序。您也从未初始化 b 数组以包含任何值。这是您的代码的更正版本:

#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;
            }
        }   
    }
}

解决方法是用指向 a 的点初始化 b 数组,然后通过比较 a 数组中的相应值对 b 数组进行就地排序。


简化代码

在您的原始代码中,策略是有一个指针数组 (b) 指向 a 中的元素,然后进行排序。但这在这里是不必要的,因为字符比指针小,所以让 b 成为字符数组更节省空间和更简单。

此外,您的间距非常紧凑,有点难以阅读。这是一个使用 b 作为字符数组而不是指针的解决方案,并提供了改进的间距。此外,没有必要声明上面的函数。定义函数并声明一次就足够了。

#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;
}
于 2012-03-21T17:58:47.830 回答
1

我用gcc -gValgrind 编译并运行它,得到了这个:

==54446==  Non-existent physical address at address 0x100000000
==54446==    at 0x100000EB0: bubbleSortAWriteToB (x.c:20)
==54446==    by 0x100000DFE: main (x.c:9)

第 20 行是这样的:

*b[j] = a[j-1];

char *b[]是一个char指针数组,但你试图在指针中放入一些东西而不初始化它们。如果你真的想这样做,你需要:

b[j] = malloc(sizeof(*b[j])); // Create some space for a char
*b[j] = a[j-1]; // Put the char in that space

但是,我认为这不是你真正想要的。如果您只是将其更改为char b[],并删除所有*',则它可以正常工作。

于 2012-03-21T17:41:55.133 回答
1

s_letters未正确初始化,但您可以通过以下方式访问它:

*b[j] = a[j-1];
*b[j-1] = temp;

这是一个段错误。

于 2012-03-21T17:43:31.060 回答
0

冒泡排序

控制台:输入:“face321” 输出:“123acef”

    #include <stdio.h>

int main(){

    char c[80] = "0";
char temp = '0';
int offSet = 0;
int i = 0;
int j =0;
int count =0;

printf("Enter first string: ");
gets(c);

while (*(c + offSet) != '\0') {
    count++;
    offSet++;
}



for (i = 0; i < count; i++) {
for (j = 0; j < count - 1; j++) {


    if (c[j]>c[j + 1]) {

        temp = c[j];
        c[j] = c[j + 1];
        c[j + 1] = temp;

    }

}

}
    puts(c);
    return 0;
}
于 2016-09-29T18:15:02.160 回答
0
#include<iostream>
using namespace std;
int main(){
cout<<"Enter the size of array";
int n;
cin>>n;
cout<<"enter elements";
char arr[n];
for(int i=0;i<n;i++){
    cin>>arr[i];
}
for(int i=0;i<n-1;i++){
    for(int j=i+1;j<n;j++){
        if(arr[i]<arr[j]){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
 }
cout<<"sorted array in descending ";
for(int i=0;i<n;i++){
    cout<<arr[i]<<" ";
}
}
于 2020-08-19T09:13:59.520 回答