Python icon indicating copy to clipboard operation
Python copied to clipboard

/sorts/merge_sort_fastest.py is NOT fast at all

Open tom5610 opened this issue 8 years ago • 12 comments

Hi @harshildarji & @yesIamHasi ,

Thanks for sharing algorithm source code and I really like them.

Just want to raise an issue on '/sorts/merge_sort_fastest.py' that it is not an merge_sort option as there are dependencies on python built-in functions - remove(), max() & min(), which makes 'merge_sort_fastest' like O(n^2) [ (n*(n+1)) / 2 * 2 as one max()/min() with remove()] instead of O(n log n).

For example, while sorting 50,000 random numbers with below list: unsorted = random.sample(range(0, 100000), 50000)

The running result will be like: (venv) $ time python3 merge_sort.py real 0m0.361s user 0m0.347s sys 0m0.011s

(venv) $ time python3 merge_sort_fastest.py real 0m49.306s user 0m49.103s sys 0m0.099s

thanks! Tom

tom5610 avatar Jul 22 '18 00:07 tom5610

Yes! @tom5610 you are right. The usage of inbuilt functions is not right here to make program faster. By the way the Python/sorts/merge_sort.py is more faster than this.

sumitkumar22299 avatar Sep 19 '18 15:09 sumitkumar22299

It's also not merge sort. It's some weird two-pronged version of selection sort.

crazymerlyn avatar Sep 19 '18 16:09 crazymerlyn

# Merges two subarrays of arr[]. 
# First subarray is arr[l..m] 
# Second subarray is arr[m+1..r] 
def merge(arr, l, m, r): 
    n1 = m - l + 1
    n2 = r- m 
  
    # create temp arrays 
    L = [0] * (n1) 
    R = [0] * (n2) 
  
    # Copy data to temp arrays L[] and R[] 
    for i in range(0 , n1): 
        L[i] = arr[l + i] 
  
    for j in range(0 , n2): 
        R[j] = arr[m + 1 + j] 
  
    # Merge the temp arrays back into arr[l..r] 
    i = 0     # Initial index of first subarray 
    j = 0     # Initial index of second subarray 
    k = l     # Initial index of merged subarray 
  
    while i < n1 and j < n2 : 
        if L[i] <= R[j]: 
            arr[k] = L[i] 
            i += 1
        else: 
            arr[k] = R[j] 
            j += 1
        k += 1
  
    # Copy the remaining elements of L[], if there 
    # are any 
    while i < n1: 
        arr[k] = L[i] 
        i += 1
        k += 1
  
    # Copy the remaining elements of R[], if there 
    # are any 
    while j < n2: 
        arr[k] = R[j] 
        j += 1
        k += 1
  
# l is for left index and r is right index of the 
# sub-array of arr to be sorted 
def mergeSort(arr,l,r): 
    if l < r: 
  
        # Same as (l+r)/2, but avoids overflow for 
        # large l and h 
        m = (l+(r-1))/2
  
        # Sort first and second halves 
        mergeSort(arr, l, m) 
        mergeSort(arr, m+1, r) 
        merge(arr, l, m, r) 

PLEASE CHECK THIS. i HOPE IT HELPS

pandyamarut avatar Sep 28 '18 16:09 pandyamarut

I think we could rename merge_sort_fastest.py to something else as it is not fast at all neither resembles merge sort. What do you think?

brunohadlich avatar Jul 14 '19 01:07 brunohadlich

@brunohadlich , I think we should have only one merge_sort file not merge_sort.py and merge_sort_fastest.py. I will inspect these two files and get back to you. What do you think?

bardia52 avatar Nov 18 '19 20:11 bardia52

I agree, it's been a long time since my last contribution to this project as I'm busy with other tasks. Feel free to discuss and apply the best solution.

brunohadlich avatar Nov 19 '19 16:11 brunohadlich

It's ok, I just started on this project. I will spend more time on it and keep it posted here.

bardia52 avatar Nov 19 '19 20:11 bardia52

Hi @brunohadlich I looked at these two files:

  • merge_sort.py
  • merge_sort_fastest.py

The one that I recommend to keep is merge_sort.py, the other one is NOT merge sort algorithm. It's something else that works fine but for the sake of algorithm education is misleading.

Let me know if there is any more work to be done on this issue, otherwise after deleting the file 'merge_sort_fastest.py' we can close this issue.

Note: @cclauss please look at this message.

bardia52 avatar Dec 02 '19 01:12 bardia52

Just raise a PR for the same

onlinejudge95 avatar Dec 03 '19 17:12 onlinejudge95

Please open a PR that renames merge_sort_fastest.py to unknown_sort.py.

cclauss avatar Dec 03 '19 17:12 cclauss

OK sure, thank you.

bardia52 avatar Dec 03 '19 17:12 bardia52

PR is created: https://github.com/TheAlgorithms/Python/pull/1612.

bardia52 avatar Dec 05 '19 03:12 bardia52