Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have this exercise Write a program in Hack assembly to sort a given array in - place in ascending order ( smallest to largest
I have this exercise
Write a program in Hack assembly to sort a given array inplace in ascending order smallest to largest
You may implement any sorting algorithm but should aim for a complexity of On or better.
Complete the code in ArrSort.asm
Inputs:
R contains the RAM address of the first element in the array
R contains the length of the array
Outputs:
Write your True to R when your program finishes.
The correctly sorted array should replace the original array in its location.
I have this code for the ArrSort.asm file:
@R
AM getting the address of the first element @
DM
@R
MM
DM
@LENG
MD
FIRSTLOOP
@R
AM A
SECONDLOOP
@R
AM getting the address of the first element @
DM Dtrying to get the value at memory address which is
AAA
DMD Compare the current element with the next element
If D is positive or zero, the current element is greater or equal to the next element
@GREATER
D;JGE
Swap the current element with the next element
@SWAP
D;JLT less than zero
SWAP
Swap the elements in memory
@R
AM
DM
@TEMP
MD TEMP stores
@R
AM
DM D
AA
MD is replace with one
@TEMP
DM TEMP stores
@R
AM
MD
@GREATER
;JMP
GREATER
@R
MM
@LENG
MM
DM
@SECONDLOOP
D;JGT
@R
MM
@R
MM
DM
@FIRSTLOOP
D;JGT
@END
D;JEQ
END
@R
M
END
@END
;JMP
I need to pass the following tests:
Positive Integers
Duplicates
Zeros
Array SizeLocation
Negative Integers
Mixed Integers
Edge Cases
but I have not been able to pass the test with code given above
As well it seems it is rebundant and it needs to improve the performance.
I need help improving the code please
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started