I'm trying to have my program find duplicates in an array of strings, and if it finds one, it swaps the duplicate to itemamount - 1, adds +1 to the WordAmt for that word, and moves the loop one spot down. I just am not sure how to properly do this. Everything works up to the swap function, which swaps as intended and lists all the words in alphabetical order. I just need all duplicate words to properly be swapped away and the WordAmt for that word to be added.

void sortArray(WordStruct Words[1000], int itemamount)
    int indexMax, j, first;

    for (indexMax = itemamount - 1; indexMax > 0; indexMax--)
        first = 0;
            for (j = 1; j <= indexMax; j++)
                if (Words[first].Word < Words[j].Word)
                    first = j;
        swap(Words, first, indexMax);
            if (j > 0)
                if (Words[first].Word == Words[j].Word)
                    swap(Words, first, itemamount - 1);
5 Years
Discussion Span
Last Post by VernonDozier

If the change to itemamount are expcted to be retained after the function returns, it needs to be passed by reference.

Lines 13 and on - Will j ever not be indexMax + 1? If it's indexMax + 1, it's greater than 0. Hence the test on line 14 is always true.

I'm seeing a swap on line 13 regardless of whether things are already in order?

What's the algorithm of this sort, in detail, in words? Clearly it isn't working, but if I knew what you were trying to do, in detail, I could perhaps figure out where the code is not obeying the algorithm.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.