Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm

Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array after
each step of the merge sort.

Bottom-up merge sort is a non-recursive variant of the merge sort, in which the array is sorted by a sequence of passes. During each pass, the array is divided into blocks of size m. (Initially, m1). Every two adjacent blocks are merged (as in normal merge sort), and the next pass is made with a twice larger value of m.

Here is the pseudo code for bottom-up merge sort:

Input: array a[l indexed from 0 to n-1.

while m <- n=”” do=”” 1=”” 0=”” while=”” i=”” n-m=”” do=”” merge=”” subarrays=”” a[i.=”” .i+m-1]=”” and=”” a[i+m=”” ..=”” min(i+2*m-1,n-1)]=”” in-place=”” m=”” m2=””>

For example, if the input array is 14 27 13 11 49 63 17 9, your program should output the following:

Input Array

14 27 13 11 49 63 17 9

After first Iteration

14 27 11 13 49 63 9 17

After second iteration

11 13 14 27 9 17 49 63

After third iteration

9 11 13 14 17 27 49 63

The Correct Answer and Explanation is :

Implementing a non-recursive bottom-up merge sort in MIPS assembly involves sorting an array of integers through iterative merging of subarrays. In this approach, the array is divided into blocks of increasing size, and adjacent blocks are merged in each iteration. After each merge step, the program prints the current state of the array.

MIPS Assembly Implementation:

.data
    prompt: .asciiz "Enter the number of elements: "
    newline: .asciiz "\n"
    array: .word 14, 27, 13, 11, 49, 63, 17, 9
    array_size: .word 8

.text
    .globl main

main:
    # Load the address of the array and its size
    la $a0, array
    lw $a1, array_size

    # Print the initial array
    li $v0, 4
    la $a0, prompt
    syscall

    li $v0, 1
    la $a0, array
    lw $a1, array_size
    syscall

    # Bottom-up merge sort
    li $t0, 1          # m = 1
    li $t1, 0          # i = 0

merge_sort:
    # Check if m >= n
    lw $t2, array_size
    bge $t0, $t2, end_sort

    # Merge subarrays of size m
    li $t1, 0          # i = 0

merge_loop:
    # Calculate the end index of the first subarray
    add $t3, $t1, $t0
    bge $t3, $t2, merge_done

    # Calculate the end index of the second subarray
    add $t4, $t3, $t0
    bge $t4, $t2, merge_done

    # Merge the two subarrays
    la $a0, array
    add $a1, $a0, $t1
    add $a2, $a0, $t3
    add $a3, $a0, $t4
    jal merge

    # Update i
    addi $t1, $t1, $t0
    j merge_loop

merge_done:
    # Print the array after this iteration
    li $v0, 4
    la $a0, newline
    syscall

    li $v0, 1
    la $a0, array
    lw $a1, array_size
    syscall

    # Double m
    sll $t0, $t0, 1
    j merge_sort

end_sort:
    # Exit the program
    li $v0, 10
    syscall

merge:
    # Merge two sorted subarrays
    # Arguments: $a0 = base address, $a1 = start of first subarray,
    #            $a2 = start of second subarray, $a3 = end of second subarray
    # Implementation of the merge operation
    # ...
    jr $ra

Explanation:

  1. Data Section:
  • prompt: A string prompting the user to enter the number of elements.
  • newline: A newline character for formatting output.
  • array: The array of integers to be sorted.
  • array_size: The number of elements in the array.
  1. Text Section:
  • The program begins by loading the address of the array and its size into registers $a0 and $a1, respectively.
  • It then prints the initial array.
  • The merge_sort loop starts with m = 1 and iteratively doubles m after each pass.
  • Within each iteration, the program merges adjacent subarrays of size m and prints the array after each merge.
  • The merge function is a placeholder for the actual merging logic, which involves combining two sorted subarrays into a single sorted subarray.

Note:
The merge function is not implemented in this snippet. Implementing the merge operation in MIPS assembly requires careful handling of memory addresses and data movement.

References:
For a detailed implementation of merge sort in MIPS assembly, you can refer to the following resources:

These repositories provide comprehensive implementations and explanations that can assist in understanding and completing the merge operation in MIPS assembly.

Scroll to Top