钻石按MIPS / C排序

时间:2014-07-12 05:25:51

标签: c mips

假设我们有一颗钻石(13 * 13阵列中有85个元素) 每个元素都有两个参数,a / b 我们需要对钻石进行分类,以便:

  1. [a]参数在每列中增加。
  2. 每行中[b]参数增加。
  3. 我们想要得到的是:( *是未在内存中定义的部分)

    (这是一个简单的5 * 5阵列中的13个元素)

    原:

    *    *   4/3   *   *
    *   1/3  2/3  4/5  *
    1/2 3/1  2/5  3/6 2/7
    *   2/3  1/2  5/2  *
    *    *   6/1   *   *
    

    排序方法:

    *    *   1/3   *   *
    *   1/2  2/3  2/5  *
    1/2 2/3  4/5  3/6 2/7
    *   3/1  5/2  4/3  *
    *    *   6/1   *   *
    

    我的算法是对所有值进行冒泡排序(根据a),然后根据b参数对每一行进行排序。这工作得很好,但动态指令将超过80000(应该是O [n ^ 2])。泡沫排序在MIPS中很难搞,因为你需要> 10 instr。每次检查(泡泡)。

    我想知道是否有更好的算法可以减少14000以下的动态指令.BTW只使用< = 15寄存器和< = 66静态指令(66行代码)。

    findnum:    addi    $27, $27, 4
                lw  $21, 0($27)
                beq $21, $0, findnum    # find the next nonzero number
    
    
    bsort:      lw  $20, 0($25)             # load first number            
                slt $12, $21, $20       # if second < first $12=1
                bne $12, $15,  back     # $15 is constant 1
                sw  $21, 0($25)
                sw  $20, 0($27)
    back:       addi    $25, $27, 0     # now second number is first number
                bne $27, $26, findnum   # $27 not the end then continue         
    
    
    
    loop:       addi    $25, $01, 24
                addi    $27, $25, 0     # reinitialize the number
                addi    $10, $10, 1     # i++
                bne     $10, $11, findnum   # if i not equal to 85 jump back    
    

    这是我的代码的核心部分。 25美元和27美元被初始化为数组第一个值的地址。 $ 26是最后一个值的地址。

    非常感谢!

2 个答案:

答案 0 :(得分:0)

允许您证明2D结构的一种方法是转换您的值,将它们填充到一维数组中,对它们进行排序,将它们填充回2D并将它们转换回来。

因此,通过简化示例,此4/3将成为此变换的数字:403

n(x/y) = 100*x + y

将2D数组填充到1D数组是一个简单的(嵌套的)for循环,然后再返回。

[403, 103, 203, 405 ...]

然后,一旦你在一维数组中有这样的数字,只需对它进行排序,例如通过Quicksort。

要将1D复制回2D,您应该有一个规则,哪个数字属于哪一行。数字的转换将是:

(x/y) = ( floor(n/100) / n%100 )

使用一般标准对2D结构进行一般排序可能并不那么容易。但在这种情况下,这种方法将起作用。它已被用于其他场合:

Sorting with 2D Arrays

答案 1 :(得分:0)

    SortDiamond:        addi    $01, $00, Array     # set memory base
            swi 521         # create sort diamond and update memory
            addi    $06, $00, 6     # constant 6
            addi    $07, $00, 12        # constant 12
            addi    $12, $00, 0     # counter inner
            addi    $21, $00, 0     # counter in bubble
            addi    $22, $00, 0     # bs counter    

bigloop:        addi    $20, $01, 72        # find the address first element to sort(row)
            addi    $26, $20, 0
            addi    $10, $00, 1     # i starts from 1       
            addi    $11, $00, 2     # sort times
            addi    $23, $00, 2

bsrow:          lw  $17, 0($26)
            lw  $18, 4($26)
            andi    $27, $17, 0x003F    
            andi    $28, $18, 0x003F
            slt $19, $28, $27
            beq $19, $00, go
            sw  $17, 4($26)
            sw  $18, 0($26)
            addi    $22, $00, 1     # this is the indicator of bubble sort

go:         addi    $26, $26, 4
            addi    $21, $21, 1
            bne $21, $23, bsrow 

loopi:          addi    $12, $12, 1
            addi    $26, $20, 0
            addi    $23, $23, -1
            addi    $21, $00, 0
            beq $22, $00, loopj     # jump if it doesn't do bubble sort in one loop
            bne $12, $11, bsrow

loopj:          addi    $10, $10, 1     # i++       
            addi    $22, $00, 0     # reinitialize counter
            slti    $19, $10, 7     # find next row
            beq $19, $00, great     # if i<7 next address is 48+ current
            sll $11, $10, 1
            addi    $20, $20, 48
            j   less

great:          sub $11, $07, $10
            sll $11, $11, 1
            addi    $20, $20, 56

less:           addi    $23, $11, 0     
            addi    $26, $20, 0
            addi    $12, $00, 0
            bne $10, $07, bsrow                 
            addi    $20, $01, 264
            addi    $26, $20, 0
            addi    $10, $00, 1 
            addi    $23, $00, 2
            addi    $11, $00, 2

bscol:          lw  $17, 0($26)
            lw  $18, 52($26)
            slt $19, $18, $17
            beq $19, $00, go1
            sw  $17, 52($26)
            sw  $18, 0($26)
            addi    $22, $00, 1

go1:            addi    $26, $26, 52
            addi    $21, $21, 1
            bne $21, $23, bscol

loopi1:         addi    $12, $12, 1
            addi    $26, $20, 0
            addi    $23, $23, -1
            addi    $21, $00, 0
            beq $22, $00, loopj1
            bne $12, $11, bscol

loopj1:         addi    $10, $10, 1
            addi    $22, $00, 0
            slti    $19, $10, 7
            beq $19, $00, great1
            sll $11, $10, 1
            addi    $20, $20, -48
            j   less1

great1:         sub $11, $07, $10
            sll $11, $11, 1
            addi    $20, $20, 56

less1:          addi    $23, $11, 0     
            addi    $26, $20, 0
            addi    $12, $00, 0
            bne $10, $07, bscol
            swi     522
            bne $02, $00, bigloop 
            swi 523         # redisplay diamond
            jr  $31         # return to caller