How to make a Worst case in mergesort in c? [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 19:01:14

问题


DataCount is how many times at sorting numbers.

  int* MakeMWData(int DataCount)
  {

// make array

int* Data = (int*)malloc(DataCount*sizeof(int));

int number = 2;
int count = 0;
Data[0] = 1;

// input data

int i,j;
for( i = DataCount;; i/=2)
{
    count++;
    for( j = 1; j<DataCount;j++)
    {

//merge sort worst

i think this isn't correct.

        if(j%i == 0 && j %(i * 2) != 0)
        {
            Data[j] = number;
            number++;
        }
    }
    if(i==1)
        break;
}
for( i = 0; i<DataCount ; i++)
{
    if(Data[i] ==0)
        Data[i] = number;
    number++;
}
return Data;
   }

Making worst Data in main function.

    int* MergeData = MakeMWData(DataCount[i]);

回答1:


The way mergesort works is dividing the array in two arrays, recursively (logn times), untill being able to compare pairs of elements. Then it merges the recursively created arrays also sorting them at the same time.

For some sorting algorithms (e.g. quicksort), the initial order of the elements can affect the number of operations to be done. However it doesn't make any change for mergesort as it will have to do exactly the same number of operations anyway: recursively divide into small arrays and then merge them back, in total Θ(nlogn) time.



来源:https://stackoverflow.com/questions/23042184/how-to-make-a-worst-case-in-mergesort-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!