Help Me Debug My Merge Sort Algorithm

  • Thread starter Thread starter azerty12
  • Start date Start date
  • Tags Tags
    Algorithm Sort
Click For Summary
SUMMARY

The forum discussion focuses on debugging a merge sort algorithm implemented in Python. The user has successfully created a fusion function that merges two sorted sublists but encounters issues with the recursive mergeSort function. Key points include the need for a second buffer in the merge function and the correct handling of array indices during the merging process. The discussion also highlights the difference between recursive and bottom-up implementations of merge sort.

PREREQUISITES
  • Understanding of Python programming language
  • Knowledge of recursive algorithms
  • Familiarity with sorting algorithms, specifically merge sort
  • Experience with array manipulation and indexing
NEXT STEPS
  • Implement a bottom-up merge sort algorithm in Python
  • Learn about memory management in Python, particularly with buffers
  • Explore the time and space complexity of merge sort
  • Review additional resources on sorting algorithms, such as those found on Wikipedia and algorithmist.com
USEFUL FOR

Software developers, computer science students, and anyone interested in understanding and implementing sorting algorithms in Python.

azerty12
Messages
15
Reaction score
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
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K