samedi 30 avril 2016

Is it the best practice to use built in sort()function rather than merge sort whose complexity is always nlogn

worst case complexity of quick sort is n^2 and best n logn and heap sort and merge sort complexity is n logn both worst,best and average case.But heap sort is not stable in everywhere.Instead merge is stable.So merge is best.But built in sort() function is n logn and implementing this, is more easier than merge sort while save time and energy.Is there any case where merge sort is apllied rather than this.

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[2000];
    int n=2000;
    for(int i=0;i<2000;i++)
    {
        a[i]=rand()%3000;//taking elements randomly less than 3000
    }
    sort(a,a+n);//sort elements .is it best practice instead use of merge sort always

}

Aucun commentaire:

Enregistrer un commentaire