dimanche 27 janvier 2019

My QuickSort code does not work for 1000000+ elements (one million elements or more)

I attempted to make my own sorting algorithm (call it MySort for now) and benchmark it against the sorting times of QuickSort. I use a random number generator to make an input file containing n random numbers, then provide this file as input to both MySort and QuickSort, and use std::chrono to time the time they take individually.

(At first I used an online compiler to check the times, but when I hit the limit of 10000 characters as input, I switched to doing it myself on my PC.)

So, for the first few tries (100 elements, 1000 elements, 10000 elements, 100000 elements), everything is working fine. I am getting a proper output time for the amount of time each sorting algorithm takes, but when I try to use 1000000 elements, QuickSort just doesn't give any output (does not seem to work at all), which is strange, because MySort worked just fine. I don't think it is a space issue, since MySort uses 2n additional space and works just fine.

The implementation of QuickSort I am using is given below:

#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

void quick_sort(int[],int,int);
int partition(int[],int,int);

int main()
{
    int n,i;
    cin>>n;

    int a[n];

    for(i=0;i<n;i++)
        cin>>a[i];
    auto start = high_resolution_clock::now();        
    quick_sort(a,0,n-1);
    auto stop = high_resolution_clock::now();

    duration <double, micro> d = stop - start;
    cout<<"Time taken = "<<d.count()<<endl;
    /*
    cout<<"\nArray after sorting:";

    for(i=0;i<n;i++)
        cout<<a[i]<<endl;
    */
    return 0;        
}

void quick_sort(int a[],int l,int u)
{
    int j;
    if(l<u)
    {
        j=partition(a,l,u);
        quick_sort(a,l,j-1);
        quick_sort(a,j+1,u);
    }
}

int partition(int a[],int l,int u)
{
    int v,i,j,temp;
    v=a[l];
    i=l;
    j=u+1;

    do
    {
        do
            i++;

        while(a[i]<v&&i<=u);

        do
            j--;
        while(v<a[j]);

        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }while(i<j);

    a[l]=a[j];
    a[j]=v;

    return(j);
}

I tried looking around for solutions as to why it refuses to work for a million elements, but found nothing, besides the possibility that it may be a space issue, which seems unlikely to me considering MySort is working.

As for what exactly I get as output on feeding 1000000 elements in, when I execute both files on the command line, the output I get is (both run twice):

C:\Users\Zac\Desktop>MySortTest <output.txt                                      

Time Taken = 512129                                                                                                 

C:\Users\Zac\Desktop>MySortTest <output.txt                                      

Time Taken = 516131                                                                                                                                               

C:\Users\Zac\Desktop>QuickSortTest <output.txt                                                                                                                    

C:\Users\Zac\Desktop>QuickSortTest <output.txt                                                                                                                    

C:\Users\Zac\Desktop>    

However, if I run them both for only 100000 elements each, this is what I get:

C:\Users\Zac\Desktop>MySortTest <output.txt                                      

Time Taken = 76897.1                                                                                                                                              

C:\Users\Zac\Desktop>MySortTest <output.txt                                      

Time Taken = 74019.4                                                                                                                                              

C:\Users\Zac\Desktop>QuickSortTest <output.txt                                   

Time taken = 16880.2                                                                                                                                              

C:\Users\Zac\Desktop>QuickSortTest <output.txt                                   

Time taken = 18005.3                                                                                                                                              

C:\Users\Zac\Desktop>  

Seems to be working fine.

I am at my wits end, any suggestions would be wonderful.

Aucun commentaire:

Enregistrer un commentaire