i wrote a quicksort program using the algorithm given in the book "introduction to algorithms : cormen,leiserson,rivest,stein"
the problem is, it worked for arrays with 10, 20 and 100 elements, but when i tried it with 5000 it crashed! i tried to noob my way out and tried a different compiler , same problem!
#include<conio.h>
#include<iostream>
using namespace std;
int arr[5000];
void quicksort(int,int);
int partition(int,int);
void main()
{
/*the array is already sorted
so this should be a worst case right?*/
for(int i=0;i<5000;i++)
arr=i;
quicksort(0,4999);
getch();
}
void quicksort(int p,int r)
{
int q;
if(p<r)
{
q=partition(p,r);
quicksort(p,q-1);
quicksort(q+1,r);
}
}
int partition(int p,int r)
{
int temp;
int x=arr[r];
int i=p-1;
for(int j=p;j<=r-1;j++)
{
if(arr[j]<=x)
{
i++;
temp=arr;
arr=arr[j];
arr[j]=temp;
}
}
temp=arr[i+1];
arr[i+1]=arr[r];
arr[r]=temp;
return i+1;
}
the problem is, it worked for arrays with 10, 20 and 100 elements, but when i tried it with 5000 it crashed! i tried to noob my way out and tried a different compiler , same problem!
#include<conio.h>
#include<iostream>
using namespace std;
int arr[5000];
void quicksort(int,int);
int partition(int,int);
void main()
{
/*the array is already sorted
so this should be a worst case right?*/
for(int i=0;i<5000;i++)
arr=i;
quicksort(0,4999);
getch();
}
void quicksort(int p,int r)
{
int q;
if(p<r)
{
q=partition(p,r);
quicksort(p,q-1);
quicksort(q+1,r);
}
}
int partition(int p,int r)
{
int temp;
int x=arr[r];
int i=p-1;
for(int j=p;j<=r-1;j++)
{
if(arr[j]<=x)
{
i++;
temp=arr;
arr=arr[j];
arr[j]=temp;
}
}
temp=arr[i+1];
arr[i+1]=arr[r];
arr[r]=temp;
return i+1;
}