冒泡排序C中的数组交换 [英] Bubble Sort in C array swapping

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

问题描述

我最近一直有这个问题,当我得到这个code的交换部分,所以这是什么code的作用是输入数组,并用冒泡排序方法进行排序。读取该文本文件有10个号码和姓名。像这样的:

约翰·1结果
马克·2结果
马太福音2结果
卢克3结果
伊萨克4结果
凯恩5结果
瑞安7结果
阿贝尔2结果
亚当·9结果
前夜10

然而,当它打印出来,就说明这一点:

约翰·1结果
马克·2结果
马太福音2结果
阿贝尔2结果
阿贝尔3结果
亚伯4结果
阿贝尔5结果
阿贝尔7结果
亚当·9结果
前夜10结果
对不起,问题是为什么会重复亚伯,而且我可以做什么来解决这个问题?

 无效冒泡(字Q){
    INT最后= 9;
    INT已交换= 1;
    INT I = 0;
    INT m为0;
    字符* TEMP =你好;
    的printf(Temp是:%S \\ n,温度);
    诠释TEMPA;
    而(已换== 1){
        交换= 0;
        I = 1;
        而(I<最后一个){
            的printf(%d个\\ N,I);
            如果(q.data [Ⅰ]≥q.data第[i + 1]){
                的printf(你好数:%d \\ n,I);
                //元素的副本名称
                TEMP = q.name [I]
                的strcpy(q.name [I],q.name第[i + 1]);
                的strcpy(q.name [I + 1],温度);                //对应元素的复制数据
                TEMPA = q.data [I]
                q.data [I] = q.data第[i + 1];
                q.data第[i + 1] = TEMPA;
                交换= 1;            }
            I = I + 1;
        }
        最后的最后= - 1;
    }
    最后= 9;
    而(M< =上){
        的printf(名称:%s,数据数:%d \\ n,q.name [M],q.data [M]);
        M +;
    }
}


解决方案

而不是这样的:

 的char * TEMP =你好;

您应该做的不是这样的:

 的char * TEMP =的malloc(MAX_NAME_LEN *的sizeof(字符));
//检查,如果内存分配成功或处理异常

或者,这样的:

 字符临时[MAX_NAME_LEN]

然后

 的strcpy(温度,你好);

您需要将字符串存储到一个临时变量,指向一个实际的内存,使用它在串交换操作,在code以后的部分。

和,而不是这样的:

元素的

  //复制名称
   TEMP = q.name [I]; //这里你存储一个指针的地址,而不是实际的字符串。既q.name [i]和临时指向同一存储器
   的strcpy(q.name [I],q.name第[i + 1]); //两个q.name [i]和临时指针,具有在q.name第[i + 1]的串。在q.name [I]原字符串会丢失。
   的strcpy(q.name [I + 1],温度); //你要复制同样的内容在q.name [I + 1]。你是不是抄袭q.name的原始内容[I]

做到这一点:

元素的

  //复制名称
的strcpy(温度,q.name [I]); //这里你存储的​​实际字符串
的strcpy(q.name [I],q.name [I + 1]); // q.name [i]和温度包含不同的字符串
的strcpy(q.name [I + 1],温度); //这里要复制q.name的原单串[I]

I have been having this problem lately when I get to the swapping part of this code, so what this code does is inputs an array and sorts it using the bubble sort method. The text file that reads this in has 10 numbers and names. Like this:

John 1
Mark 2
Matthew 2
Luke 3
Issac 4
Kane 5
Ryan 7
Abel 2
Adam 9
Eve 10

However when it prints it out, it shows this:

John 1
Mark 2
Matthew 2
Abel 2
Abel 3
Abel 4
Abel 5
Abel 7
Adam 9
Eve 10
Sorry, the question is why is it repeating Abel, and what can i do to fix it?

    void bubbleSort (Word q){
    int last = 9;
    int Swapped = 1; 
    int i = 0;
    int m = 0;
    char* temp = "Hello";
    printf("Temp is: %s \n", temp);
    int tempA;
    while (Swapped == 1){
        Swapped = 0;
        i = 1;
        while (i < last){
            printf("%d \n", i);
            if (q.data[i] > q.data[i+1]) {
                printf("Hello: %d \n", i);
                //Copy Name of Element
                temp = q.name[i];
                strcpy(q.name[i], q.name[i+1]);
                strcpy(q.name[i+1] , temp);

                //Copy Data of corresponding element
                tempA = q.data[i];
                q.data[i] = q.data[i+1];
                q.data[i+1] = tempA;
                Swapped = 1; 

            } 
            i = i + 1;
        }
        last = last - 1;
    }
    last = 9;
    while (m <= last){
        printf("Name: %s, Data: %d \n", q.name[m], q.data[m]);
        m++;
    }
}

解决方案

Instead of this:

char* temp = "Hello";

You should do either this:

char *temp = malloc(MAX_NAME_LEN*sizeof(char));
//check if allocation of memory was successful or handle exceptions

Or, this:

char temp[MAX_NAME_LEN];

And then

strcpy(temp, "Hello");

You need to store the string to a temp variable, pointing to an actual memory, to use it in string swapping operation, in later part of the code.

And instead of this:

   //Copy Name of Element
   temp = q.name[i];//HERE you are storing a pointer to the address, not the actual string. Both q.name[i] and temp point to the same memory
   strcpy(q.name[i], q.name[i+1]);//Both q.name[i] and the temp pointer, with the string at q.name[i+1]. the original string in q.name[i] is lost.
   strcpy(q.name[i+1] , temp);//You are copying the same content as in q.name[i+1]. You are not copying the original content of q.name[i]

do this:

//Copy Name of Element
strcpy(temp, q.name[i]);//HERE you are storing the actual string
strcpy(q.name[i], q.name[i+1]);//q.name[i] and temp contain distinct strings
strcpy(q.name[i+1], temp);//HERE you are copying the orginal string of q.name[i]

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

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