DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Quicksort problems (http://www.daniweb.com/forums/thread80561.html)

Lutzee Jun 10th, 2007 11:58 am
Quicksort problems
 
Hi there everyone. I wonder if you could help me with my code. It just seems to go into an infinite loop. I would greatly appreciate some help with this. Thanks

#include<iostream>
using namespace std;

int qsrt(int array[], int first, int last)

{
   

    int over = 0;
    over = first-last;
    int g=0;

    if(over==0)
    {
    return 0;
    }
   
    int c=0;
   
    int start = first;
    int end = last;
    int pivot = array[first];
    int temp =0;
   
    while(first != last)
    {

    while((array[last]>pivot)&&(first != last))
    {
        last--;
    }

    while((array[first]<pivot)&&(first != last))
    {
        first++;
    }
   
    temp = array[last];
    array[last] = array[first];
    array[first] = temp;

   

    c = last-first;

    if(c>1)
    {
        first++;
        last--;
    }
   
 }
   
      c= 0;
    c= first-1;
   
    qsrt(array,first,end);
    qsrt(array,start,(c));

  return 0;
}

int main (void)

{
    int anarray[9] = {4,5,3,8,5,6,2,8,9};
   
   
   
    qsrt(anarray,0,8);
    system("pause");
   

    return 0;
}

Thanks once again

JRM Jun 10th, 2007 3:08 pm
Re: Quicksort problems
 
IF your intent is to sort a range of integers beginning at first and continuing to last, there are simpler ways to do it.
You don't need two loops or so many extra varaibles.

If you need to sort from both ends, shouldn't pivot be in the middle of the arrary rather than equal to array[first]?

Nichito Jun 10th, 2007 8:24 pm
Re: Quicksort problems
 
#include<iostream>
using namespace std;

int qsrt(int array[], int first, int last){
    int over = 0;
    over = first-last;
    int g=0;
    if(over==0){
        return 0;
    }int c=0;
    int start = first;
    int end = last;
    int pivot = array[first];
    int temp =0;
    while(first != last){
        while((array[last]>pivot)&&(first != last)){
            last--;
        }while((array[first]<pivot)&&(first != last)){
            first++;
        }temp = array[last];
        array[last] = array[first];
        array[first] = temp;
        c = last-first;
        if(c>1){
            first++;
            last--;
        }
    }c= 0;
    c= first-1;
    qsrt(array,first,end);
    qsrt(array,start,(c));
    return 0;
}

int main (void){
    int anarray[9] = {4,5,3,8,5,6,2,8,9};
    qsrt(anarray,0,8);
    system("pause");
    return 0;
}

first, you should learn how to format your code correctly...


second, saying this
int a=0;
a=b-c;
is the same thing as saying
int a=b-c;
when you have b and c declared...

third, what are you doing there? it seems like you are ordering numbers from least to greatest... but... JRM is right... you could do this with less variables...


All times are GMT -4. The time now is 9:16 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC