Skip to main content
added 2 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Is this an optimal implementation of merge sort?

I am taking Algorithman algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication so, so I wrote this function:

  1. How tocan I make this implementation more optimal (best possible performance)?
  2. When handling arrays of large lengths (1000000), what causes a segmentation fault.?

Is this an optimal implementation of merge sort

I am taking Algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication so I wrote this function:

  1. How to make this implementation more optimal (best possible performance)
  2. When handling arrays of large lengths (1000000), what causes a segmentation fault.

Is this an optimal implementation of merge sort?

I am taking an algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication, so I wrote this function:

  1. How can I make this implementation more optimal (best possible performance)?
  2. When handling arrays of large lengths (1000000), what causes a segmentation fault?
made text more readable, fixed some typos
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

is Is this aan optimal implementation forof merge sort

I am taking Algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication so I wrote this function:

I have 2 questions the first how to make this implementation more optimal the second is when handling arrays of large lengths (1000000) what causes a segmentation fault. NOTE:

  1. How to make this implementation more optimal (best possible performance)
  2. When handling arrays of large lengths (1000000), what causes a segmentation fault.

NOTE : I tried the function using array randomly generated of length 1000 and it worked.

EDIT: I MEAN BY OPTIMAT "BEST PERFORMANCE"

is this a optimal implementation for merge sort

I am taking Algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication so I wrote this function

I have 2 questions the first how to make this implementation more optimal the second is when handling arrays of large lengths (1000000) what causes a segmentation fault. NOTE : I tried the function using array randomly generated of length 1000 and it worked.

EDIT: I MEAN BY OPTIMAT "BEST PERFORMANCE"

Is this an optimal implementation of merge sort

I am taking Algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication so I wrote this function:

I have 2 questions:

  1. How to make this implementation more optimal (best possible performance)
  2. When handling arrays of large lengths (1000000), what causes a segmentation fault.

NOTE : I tried the function using array randomly generated of length 1000 and it worked.

Source Link
u185619
  • 877
  • 1
  • 7
  • 23

is this a optimal implementation for merge sort

I am taking Algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication so I wrote this function

void mergesort (int* list, int len)
{
        if(len == 1) return;
        int i = len/2, j = len-i;
        int list1[i], list2[j];
        for(int k=0;k<i;k++)
        {
                list1[k]= list[k];
                list2[k]= list[i+k];
        }
        if(len%2!=0) list2[j-1] = list[len-1];
        mergesort(list1 , i);
        mergesort(list2 , j);
        int k=0,l=0;
        // k represent counter over elements in list1
        // l represent counter over elements in list2
        // k+l represent counte over total elements in list
         while(k+l!=len)
        {
                if(k==i)
                {
                        for(;l<j;l++) list[k+l] = list2[l];
                        return;
                }
                else if (l==j)
                {
                        for(;k<i;k++) list[k+l] = list1[k];
                }
                else if(list1[k]<list2[l])
                {
                        list[k+l]=list1[k];
                        k++;
                }
                else if(list1[k]>list2[l])
                {
                        list[k+l] = list2[l];
                        l++;
                }
                else
                {
                //handles dublication
                list[k+l] = list1[k];
                k++;
                list[k+l] = list[l];
                l++;
                }
        }
}

I have 2 questions the first how to make this implementation more optimal the second is when handling arrays of large lengths (1000000) what causes a segmentation fault. NOTE : I tried the function using array randomly generated of length 1000 and it worked.

EDIT: I MEAN BY OPTIMAT "BEST PERFORMANCE"