0

I've coded a bubble sort using ARM assembly language

The code works perfectly and sorts as intended, but it keeps looping in COMP (compare) after the sort has finished. I know I need a swap flag to check, but I can't understand its logic and where it should be placed in the code

r0 = address of array in memory 
r1 = index of first number
r2 = value of first number 
r3 = index of second number 
r4 = value of second number 

Sorry for not including the code as a text because I will be submitting it through a plagiarism detecting system.

CODE

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
MxML
  • 1
  • The logic is, set the flag in `SWAP`. Check (and clear) the flag in `CHECK` :) – Jester Dec 02 '20 at 14:02
  • If you want a simple sort with an efficient early-out condition, I'd recommend Insertion Sort in the first place, not Bubble. The only reason to use Bubble Sort is that it can be pretty compact (small machine-code size) and easy to write without the early-out check. (See [Bubble Sort: An Archaeological Algorithmic Analysis](https://users.cs.duke.edu/~ola/papers/bubble.pdf) / [Why bubble sort is not efficient?](https://stackoverflow.com/q/61997897)). But if you insist on optimizing Bubble Sort, https://en.wikipedia.org/wiki/Bubble_sort#Optimizing_bubble_sort shows the algorithm. – Peter Cordes Dec 02 '20 at 14:09
  • Oh, your code is weird; there's no outer loop condition at all. You don't need to check for swap, you can just look at less and less of the array until the outer loop start or end position gets to the end or start, depending on the sort direction. (That's actually the optimization wikipedia is talking about in that link; their "unoptimized" version is still using a swap-check.) – Peter Cordes Dec 02 '20 at 14:15
  • @Jester Thank you very much ! – MxML Dec 02 '20 at 18:32
  • @PeterCordes Thank you for the valuable information. I will definitely go through it, but actually the assignment I have asks to code a bubble sort algorithm. Anyway, Thank you again :) – MxML Dec 02 '20 at 18:36

0 Answers0