argamanza
argamanza

Reputation: 1142

Sort algorithm swap doesn't work

I'm trying to sort an array of pointers to words lexicographically and the sort itself seems to work, except of the swap, i tried many formats and i can't make it work, hope you could help me:

    for(i=1;i<words;i++){
        for(j=i;j>0 && strcmp(dicArray[0][j-1],dicArray[0][j]) == 1;j--){
            temp = dicArray[0][j];
            dicArray[0][j-1] = dicArray[0][j];
            dicArray[0][j] = temp;
        }
    }

Upvotes: 0

Views: 113

Answers (1)

Dipto
Dipto

Reputation: 2738

Your swapping algo is not correct. If you want to swap dicArray[0][j-1] and dicArray[0][j], then do the following:

temp = dicArray[0][j];
dicArray[0][j] = dicArray[0][j-1];
dicArray[0][j-1] = temp;

Upvotes: 11

Related Questions