Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

sorting - Bubble Sort in C array swapping

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 
", temp);
    int tempA;
    while (Swapped == 1){
        Swapped = 0;
        i = 1;
        while (i < last){
            printf("%d 
", i);
            if (q.data[i] > q.data[i+1]) {
                printf("Hello: %d 
", 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 
", q.name[m], q.data[m]);
        m++;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...