i'm trying to implement randomized version of quicksort.i have successfully used the normal version of quicksort,but now the g++ compiler says floating point exception (core dumped). How do i proceed???
#include<bits/stdc++.h>
using namespace std;
void qsort(int* a,int start,int end);
int part(int*a,int start, int end);
int main()
{
int i,n;
cout<<"enter no of elements...."<<endl;
cin>>n;
int a[n];
cout<<"enter the elements to be sorted"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
int start=0;
qsort(a,start,n-1);
cout<<"the list is..";
for(i=0;i<n;i++)
cout<<a[i]<<' ';
return 0;
}
void qsort(int* a,int start,int end)
{
if(end>start)
{
int pi=part(a,start,end);
qsort(a,start,pi-1);
qsort(a,pi+1,end);
}
}
int part(int*a,int start, int end)
{
int pi,pivot,k;
int i=start+rand()%(start+end-1);
pi=start;
pivot=a[i];
int j,t;
for(j=start;j<end;j++)
{
if(a[j]<=pivot)
{
t=a[j];
a[j]=a[pi];
a[pi]=t;
pi++;
}
}
t=a[i];
a[i]=a[pi];
a[pi]=t;
return pi;
}
Aucun commentaire:
Enregistrer un commentaire