繁体   English   中英

c中按字母顺序对字符数组进行冒泡排序

[英]bubble sort a character array in alphabetic order in c

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

#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 循环一样。 但从我所见,也并非如此。 你能帮我吗?

修复你的代码

首先,您的代码存在一些非常严重的基本问题。 在我们解决这些问题之前,让我们先解决您目前拥有的问题。 您的排序循环似乎是对 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 数组进行就地排序。


简化代码

在您的原始代码中,策略是拥有一个指向 a 中元素的指针 (b) 数组,然后进行排序。 但这在这里是不必要的,因为字符比指针小,所以让 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;
}

我用gcc -g编译它并通过 Valgrind 运行它,得到了这个:

==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[]并删除所有* ,则它可以正常工作。

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

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

这是一个段错误。

冒泡排序

控制台:输入:“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;
}
#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]<<" ";
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM