Thread: Quicksort problem

  1. #1
    Unregistered
    Guest

    Quicksort problem

    hi, I wrote this quicksort program. It compiles fine, but gives an error when run. Thank you for any help you can give.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    void swap(int*,int*);
    int partition(int[],int,int);
    void quicksort(int[],int,int);
    
    int main()
    {
        int array[MAX];
        int i=0;
        int ch;
        printf("enter values,terminated by #");
        while (ch=fgetc(stdin) && i<MAX && (char)ch!='#'){
              array[i]=ch;i++;
        }
        quicksort(array,0,i);
        for (i=0;i<MAX;i++)printf("%d",array[i]);
        return 0;
    }
    
    
    
    
    
    
    
    void swap(int* a, int* b)
    {
         int temp=*a;
         *a=*b;
         *b=temp;
    }
    
    
    int partition(int farray[], int flow, int fhigh)
    {
         int pivot=(flow+fhigh)/2;
         int ltemp=flow;
         int htemp=fhigh;
         while (flow <= fhigh){
               for (;;flow++){
                   if (farray[flow] > farray[pivot]){
                      ltemp=flow;
                      break;
                   }
               }
    
               for (;;fhigh--){
                   if (farray[fhigh] < farray[pivot]){
                      htemp=fhigh;
                      break;
                   }
               }
    
    
               swap(&farray[fhigh],&farray[flow]);
         }
    
         if (farray[pivot] > farray[pivot+1]){swap(&farray[pivot],&farray[pivot+1]);}
         if (farray[pivot] < farray[pivot-1]){swap(&farray[pivot],&farray[pivot-1]);}
         return pivot;
    }
    
    
    void quicksort(int farray[],int low, int high)
    {
         int m;
         while (low<10){   /*how do I decide when stop recursing?*/
               m=partition(farray,low,high);
               quicksort(farray,low,m-1);
               quicksort(farray,m+1,high);
         }
    }
    Thank you!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while (low<10){ /*how do I decide when stop recursing?*/
    A loop like this in a recursive function is a bad idea. Try this instead:
    if (high <= low) return;

    At this point the only problem is the output, but you can easily step through your code and find out where that problem is, so I leave that up to you.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM