this code was workin when i used srand function to fill th e array with random numbers and the array was a declared globally.But now the code is not working.I cant find anything wrong in this .Sombody pls help..

#include<iostream.h>
int *a,NUM_ITEMS;

int main()
{

    void enterdata();
    void quicksort(int,int);
    int i;
    cout<<"\n\nEnter the number of items:";
    cin>>NUM_ITEMS;
    a=new int [NUM_ITEMS];
    if(a==NULL)
    cout<<"Memory allocation error";
    cout<<"\nEnter the numbers:";
    enterdata();
    quicksort(0,NUM_ITEMS-1);
    cout<<"Done with sort.\n";
    for (i = 0; i <NUM_ITEMS; i++)
    cout<<"\n"<<a[i];
    return 0;
}
void enterdata()
{
	int i;

	for(i=0;i<NUM_ITEMS;i++)
		cin>>a[i];

}


void quicksort(int lower,int upper)
{
    int split(int,int),pivot;
    if(upper>lower)
      {
	 pivot=split(lower, upper);
	 quicksort(lower,pivot-1);
	 quicksort(pivot+1,upper);

      }
}
int split(int lower,int upper)
 {
    int i,p,q,t;
    p=lower+1;
    q=upper;
    i=a[lower];
    while(q>=p)
      {
	 while(a[p]<i)
	    p++;
	 while(a[q]>i)
	    q--;
	 if(q>p)
	  {
	    t=a[p];
	    a[p]=a[q];
	    a[q]=t;
	  }
      }
    t=a[lower];
    a[lower]=a[q];
    a[q]=t;
    return q;
}

I have feeling that there is something wrong with function split(). Right now, I am not with my computer, so I can't compile your code. However, you allocate the memory without delete it at the end of the program.

This article has been dead for over six months. Start a new discussion instead.