# Quicksort problem

• 04-17-2002
Unregistered
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!
• 04-17-2002
Prelude
>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