GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Sep-2005, 18:50
dayrinni dayrinni is offline
New Member
 
Join Date: Mar 2005
Posts: 24
dayrinni is on a distinguished road

Quicksort and randomized hoare partition


Hey,

I am trying to write a quick sort lab that uses a randomized Hoare partition function. It doesn't seem to be working correctly. On some data sets, it will sort them very quickly, on others it will loop for infinity.

I am not entirely sure what I am doing wrong. I have went over the hoare partition pseudocode mulitple times, as well as traced through the numbers on paper. I've pretty much spent a good 6 hours today trying to make this work. I also can't find ANY use of a hoare parition on any quick sort code on the internet.

Any help would be great. I feel I am missing 1 key thing (or I did a stupid mistake that I can't find since I have been staring at it for so long).

Please look over my quicksort and hoare partition function and see if any problems can be found.

THANKS!

Note: This compiles and runs on linux. It *should* also work on windows.
Note 2: I passed count for general debugging. I removed all of my debug statements so it looks like count is being passed for no reason.
Note 3: I need to implement the better way of generating random numbers.

CPP / C++ / C Code:
using namespace std;
#include <iostream>
#include <ctime>
#include "stdlib.h"

// Maximum of 1 million numbers to sort.
#define MAX_SIZE 1000000

//function def's
void quicksort(int low, int high, int count);
int random_hoare_partition(int low, int high);
int generate_random_number(int min, int max);
void swap(int &first, int &second);

int array[MAX_SIZE];

main(int argc, char *argv[])
{
  	int i;
	int count = 0;
        char c;

       //seed the generator with the current time
       srand(static_cast<unsigned>(time(0)));

      while (cin >> array[count])
           ++count;


     for (i = 0; i < count; ++i)
        cout << array[i] << endl; 




        cout << "Time to sort!" << endl;
        quicksort(0, count-1, count);

        cout << "We sorted, print out the numbers:" << endl;

        for (i = 0; i < count; ++i)
                cout << array[i] << endl;

}

//PROBLEM HERE OR IN RANDOM_HOARE_PARTITION
void quicksort(int low, int high, int count)
{
        int pivot;
        if(low < high)
        {
                pivot = random_hoare_partition(low, high);
                quicksort(low, pivot, count);
                quicksort(pivot+1, high, count);
        }
}

//PROBLEM HERE OR IN QUICK SORT
int random_hoare_partition(int low, int high)
{
        int pivot;
        int i = low-1;
        int j = high+1;
        bool run = true;
        int index = generate_random_number(low, high);



        pivot = array[index];

        while(run)
        {
                do
                {
                        j = j - 1;
                }
                while (array[j] > pivot);   //find a smaller one
	
		do
                {
                	i = i + 1;
                }
                while(array[i] < pivot);     //find a larger one
		
		if(i < j)
                	swap(array[i], array[j]);
                else
                	return j;


        }

}

//this generates a random number from min to max.
int generate_random_number(int min, int max)
{
        int i;
        int num;

        num  = rand() % (max -min + 1);

        return num;

}

void swap(int &first, int &second)
{
        int temp = first;


        first = second;
        second = temp;


}
  #2  
Old 24-Sep-2005, 20:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Quicksort and randomized hoare partition


Quote:
Originally Posted by dayrinni
Hey,



I am not entirely sure what I am doing wrong. I have went over the hoare partition pseudocode mulitple times, as well as traced through the numbers on paper. I've pretty much spent a good 6 hours today trying to make this work. I also can't find ANY use of a hoare parition on any quick sort code on the internet.

Note: This compiles and runs on linux. It *should* also work on windows.
Note 2: I passed count for general debugging. I removed all of my debug statements so it looks like count is being passed for no reason.
Note 3: I need to implement the better way of generating random numbers.

[

I am a "bottom-up" kind of guy, so let's start with #3. Did you test your random number generator? Do you think it generates random number between its "low" and "high" arguments?

I just broke it out into a separate program:

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
  int generate_random_number(int min, int max);
  int i, j, k;
  int num;
  for (i = 0; i < 10; i++) {
    j = i*10;
    k = i+100;
    num = generate_random_number(j, k);
    cout << "generate_random_number(" << j 
         << ", " << k << ") = " << num << endl;
  }
  return 0;
}

//
//this is supposed to generate a random number from min to max.
//
int generate_random_number(int min, int max)
{
    int num;
    num  = rand() % (max -min + 1);
    return num;
}


Here is a sample run from gcc:

Quote:
generate_random_number(0, 100) = 0
generate_random_number(10, 101) = 41
generate_random_number(20, 102) = 23
generate_random_number(30, 103) = 46
generate_random_number(40, 104) = 29
generate_random_number(50, 105) = 52
generate_random_number(60, 106) = 30
generate_random_number(70, 107) = 12
generate_random_number(80, 108) = 18
generate_random_number(90, 109) = 16

What's wrong? In general, for non-negative integers n and m, the value of the expression n%m is an int from 0 to m-1. That's not what you need.

If you already have pseudo code for quicksort and the kind of partition that you need, I am not sure why you need to search the web, unless you just want to see if your book or class notes is/are wrong..

Anyhow, I typed hoare random partition into a search engine and got a ***lot*** of hits --- some of which were, naturally, irrelevant. However, near the top of the hit list I found this "oldie-but-goodie": http://www.comsc.ucok.edu/~mcdaniel/mcdaniel/cam/vopf

Regards,

Dave
  #3  
Old 24-Sep-2005, 21:23
dayrinni dayrinni is offline
New Member
 
Join Date: Mar 2005
Posts: 24
dayrinni is on a distinguished road

Re: Quicksort and randomized hoare partition


LOL Oh my god.

Man, I feel like an idiot now.

I swear I tested that, but I guess I didn't.

Thank you very much!! It sorts now and doesn't seem to infinity loop.

Thank you!
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.