SomeSickJoke
SomeSickJoke

Reputation: 31

Bubble Sort C Programming

Hey folks ive come across another problem. Im trying to do a bubble sort and a selection sort ive got my selection sort working. But im having trouble with my bubble sort.Here it is.

void sortBubble(int nRow, char sArr[5][10], char sArrTemp[10]) {
    int nSwaps=0;
    while(nSwaps==1) {
        nSwaps=0;
        for(nRow=1;nRow <5;nRow++) {
            if(strcmp(sArr[nRow-1],sArr[nRow])<0) {
                puts("Doing Swap");
                strcpy(sArrTemp,sArr[nRow]);
                strcpy(sArr[nRow],sArr[nRow-1]);
                strcpy(sArr[nRow-1],sArrTemp);
                nSwaps=1;
            }
        }
    }
}

Any ideas why its not working.

Upvotes: 0

Views: 553

Answers (3)

Pandrei
Pandrei

Reputation: 4951

    int nSwaps=0;
    while(nSwaps==1) {
     // sort
    }

change nSwaps to 1 and it should work...

int nSwaps=1;
while(nSwaps==1) {
 // sort
}

Upvotes: 0

mclc
mclc

Reputation: 65

You never enter the loop because of

int nSwaps=0;

Also, you should consider using another algorithm for sorting. I would recommend qsort

Upvotes: 1

Hash
Hash

Reputation: 229

The problem is

int nSwaps= 0 ; while(nSwaps== 1 )

so

0==1 which is return 0(false) so you never enter the loop try to change it

Upvotes: 0

Related Questions