0

I'm trying to implement a quick sort that sorts numbers and words based on the number value. I can't seem to figure out how to fix the following code to work right.

 if (high!=low&& high>low)//compares hashes and finds the number in the middle. swaps hashes and corresponding words
{

long one=hash[low];
long two=hash[high];
long three = hash[high/2];
    if((one<=two&&one>=three)||(one<=three&&one>=two))
    {
        swap(hash[low], hash[high]);
        swap(copyOfWords[low], copyOfWords[high]);
    }
    else if((three<=one&&three>=two)||(three<=two&&three>=one))
    {

        swap(hash[high/2], hash[high]);
        swap(copyOfWords[high/2], copyOfWords[high]);
    }
    else
    {

    }
    int i=low;
    int j=high-1;
    while(i!=j&&i<j)
    {

        while(hash[i]<hash[high]&&i<j)// find higher numbers and lower numbers then the middlle and swaps them
        {
            i++;
        }
        while(hash[j]>hash[high]&&i<j)
        {
            j--;
        }
        if(i==j||i>j)
        {
        }
        else
        {
            swap(hash[i],hash[j]);
            swap(copyOfWords[i],copyOfWords[j]);
            i++;
            j--;
        }
    }
        swap(hash[i],hash[high]);
    swap(copyOfWords[i], copyOfWords[high]);



    quickSort(low, j-1);//recursive
    quickSort(j+1, high);

}

}

I know the values in hash and copyOfWords are correct because when I use shell sort, it sorts them the right way. for example if there are two words, copyOfWOrds[0]="1994," and copyOfWords[1]="a" then hash[0]=549456039 and hash[1]=197000000, but the sort puts them a 1994, a instead of a 1994,. It causes more problems with more elements. Any help would be appreciated. Thanks

6
  • This would be a better question if you included example inputs and outputs. Try to describe how the code is failing, and specifically what you don't understand. Commented Feb 25, 2015 at 23:42
  • high > low implies high != low, you can remove the latter Commented Feb 25, 2015 at 23:43
  • 1
    This would be better if you used a debugger and executed each statement singly. Also, you could compare you code to existing QuickSort implementations: QuickSort implementations Commented Feb 25, 2015 at 23:50
  • 1
    Duplicate of stackoverflow.com/questions/28681643/… ? Commented Feb 25, 2015 at 23:54
  • yeah, but separate question. Commented Feb 25, 2015 at 23:58

2 Answers 2

1

Why don't you go to the quick sort wiki page and see how it's done?

Your code tries to do unnecessary stuffs and eventually trips over its own feet. Keep it simple and it will work.

And Btw Quicksort works very well on arrays, so it's a shame to make one version where the array is hard-coded.

Sign up to request clarification or add additional context in comments.

2 Comments

the array isn't hard coded, it a list of words from a text file stored into the array and converts those words to hashes. Thanks for the website.
I mean the array could be passed as an argument to quicksort. That is what hard-coded means: non configurable.
1

One bug in your code is that you have

long three = hash[high/2];

and

swap(hash[high/2], hash[high]);
swap(copyOfWords[high/2], copyOfWords[high]);

This may louse up your sort when high/2 is not in the range low..high.

You no doubt meant to write

long three = hash[(low+high)/2];

and

swap(hash[(low+high)/2], hash[high]);
swap(copyOfWords[(low+high)/2], copyOfWords[high]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.