Thread: Quicksort problem

  1. #1

    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.

    #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!='#'){
        for (i=0;i<MAX;i++)printf("%d",array[i]);
        return 0;
    void swap(int* a, int* b)
         int temp=*a;
    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]){
               for (;;fhigh--){
                   if (farray[fhigh] < farray[pivot]){
         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?*/
    Thank you!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >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.

    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, 10: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, 05:24 PM
Website Security Test