How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the 6502 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the 6502 Assembly programming language

Table of Contents

Problem Statement

A   bubble   sort is generally considered to be the simplest sorting algorithm. A   bubble   sort is also known as a   sinking   sort. Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses. Because of its abysmal O(n2) performance, it is not used often for large (or even medium-sized) datasets. The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it.   If the first value is greater than the second, their positions are switched.   Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).  
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.   A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits. This can be expressed in pseudo-code as follows (assuming 1-based indexing):

Sort an array of elements using the bubble sort algorithm.   The elements must have a total order and the index of the array can be of any discrete type.   For languages where this is not possible, sort an array of integers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the 6502 Assembly programming language

Source code in the 6502 programming language

define z_HL $00
define z_L  $00
define z_H  $01
define temp $02
define temp2 $03

set_table:
dex
txa
sta $1200,y
iny
bne set_table	;stores $ff at $1200, $fe at $1201, etc.

lda #$12
sta z_H
lda #$00
sta z_L

lda #0
tax
tay		;clear regs

JSR BUBBLESORT
BRK

BUBBLESORT:
lda (z_HL),y
sta temp
iny		;look at the next item
lda (z_HL),y
dey		;go back 1 to the "current item"
sta temp2
cmp temp
bcs doNothing

    ;we had to re-arrange an item.
    lda temp
    iny
    sta (z_HL),y   ;store the higher value at base+y+1
    inx ;sort count. If zero at the end, we're done.
    dey
    lda temp2
    sta (z_HL),y   ;store the lower value at base+y 

doNothing:
iny
cpy #$ff
bne BUBBLESORT
ldy #0
txa     ;check the value of the counter
beq DoneSorting
ldx #0	;reset the counter
jmp BUBBLESORT
DoneSorting:
rts

  

You may also check:How to resolve the algorithm Documentation step by step in the Wren programming language
You may also check:How to resolve the algorithm Cut a rectangle step by step in the C++ programming language
You may also check:How to resolve the algorithm Executable library step by step in the Ruby programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the Fermat programming language
You may also check:How to resolve the algorithm Aliquot sequence classifications step by step in the FreeBASIC programming language