Help Me Debug My Merge Sort Algorithm

In summary, the conversation discusses the implementation of a merge sort algorithm in Python. The first step is to create a fusion algorithm, which takes a list A divided into two sorted sub-lists and returns a completely sorted list. Then, there is the recursive mergeSort function that splits up the array into sub-arrays until they have a size of 1, and then merges them back together. The conversation also mentions a bottom-up mergesort method, which uses iteration instead of recursion. Overall, the conversation provides helpful tips and resources for implementing a merge sort algorithm in Python.
  • #1
azerty12
15
0
Hi

I'm trying to program a merge sort algorithm on python but it just doesn't work!
I first implemented a fusion algorithm which takes as arguments a list A divided into 2 sorted sub lists A[p..q] and A[q+1..r]. Fusion then returns the list A completely sorted.
This program seems to work, but then my mergeSort which should be easy doesn't work.
Could you have a look at it please?
thanks

Code:
def fusion(A,p,q,r):
    """A[p]<=...A[q]   A[q+1]<=...A[r]"""
    n1=q-p+1
    n2=r-q
    L=[]
    R=[]
    for e in range(n1):
        L+=[A[e]]
    for e in range(n2):
        R+=[A[n2+e+1]]
    L+=[float("infinity")]
    R+=[float("infinity")]
    i=0
    j=0
    for k in range(p,r+1):
        if L[i]<=R[j]:
            A[k]=L[i]
            i+=1
        else:
            A[k]=R[j]
            j+=1
    return A        

def mergeSort(A,p,r):
    if p<r:
        q=int((p+r)/2)
        mergeSort(A,p,q)
        mergeSort(A,q+1,r)
        fusion(A,p,q,r)
    return A
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
A recursive mergesort() like the one you're trying to implement recursively splits up an array into 2 sub-arrays(), and continues to just recurse and split up sub-arrays until sub-arrays have size 1, before any merging occurs. Then the sub-arrays are merged as the call chain is followed back upwards to the original call to mergesort(). Note that the merge() function will require a second buffer, up to the size of the original array.

Example pseudo code: mergesort() splits up the arrays, merge() merges two arrays. Again note that the call to mergesort(left) or mergesort(right) will just recurse until the sub-array size is 1, and only then does merging and return back up the call chain occur.

Code:
mergesort(array){
    n = size_of(array)
    if(n <= 1){
        return(array);}
    left = array[(0) ... (n/2 - 1)];
    right = array[(n/2) ... (n-1)];
    left = mergesort(left); 
    right = mergesort(right);
    return( merge(left, right) );
}

A bottom up mergesort() uses iteration instead of recursion. At initialization, it simply considers an array as a list of n sub-arrays of size 1, by setting up indexes or pointers and sub-array size = 1. It allocates a memory buffer for a second copy of the array, then merges all pairs of the sub-arrays in the original buffer to the second buffer, (doubling sub-array size), and then merges from the second buffer back to the original buffer (doubling sub-array size again), until sub-array size is >= original array size. When completed, the mergesort() may return a pointer to which buffer ended up with the sorted data (original or second), or if the sorted data ended up in the second buffer, it could copy it back to the original buffer.

Here are web sites with pseudo code for merge sort.

http://www.algorithmist.com/index.php/Merge_sort

http://en.wikipedia.org/wiki/Merge_sort
 
Last edited:

Related to Help Me Debug My Merge Sort Algorithm

1. What is a Merge Sort Algorithm?

A Merge Sort Algorithm is a sorting algorithm that divides the input array into smaller subarrays, sorts the subarrays, and then merges them back together to produce a sorted array. It is a divide and conquer algorithm and has a time complexity of O(nlogn).

2. How do I implement a Merge Sort Algorithm?

To implement a Merge Sort Algorithm, you can follow these steps:

  • Divide the original array into two smaller subarrays.
  • Recursively sort the two subarrays using the Merge Sort Algorithm.
  • Merge the two sorted subarrays back together in the correct order.
  • Repeat until the entire array is sorted.

3. What are common errors when implementing a Merge Sort Algorithm?

Some common errors when implementing a Merge Sort Algorithm include:

  • Not properly dividing the array into subarrays.
  • Incorrectly merging the subarrays back together.
  • Not handling edge cases, such as empty arrays or arrays with only one element.
  • Using the wrong comparison operator when sorting the subarrays.

4. How can I test my Merge Sort Algorithm?

You can test your Merge Sort Algorithm by inputting various arrays of different sizes and elements. Make sure to test edge cases and arrays with duplicate elements. You can also use a debugger or print statements to track the execution of your code and identify any errors.

5. What is the best time complexity for a sorting algorithm?

The best time complexity for a sorting algorithm is O(nlogn). This means that the algorithm can sort an array of size n in nlogn time, which is the most efficient time complexity for sorting algorithms. Merge Sort, along with other divide and conquer algorithms such as Quick Sort, have this optimal time complexity.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
964
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top